|
1 | 1 | from datetime import date
|
| 2 | +from unittest import skipIf |
2 | 3 |
|
| 4 | +import django |
3 | 5 | from django.test import TestCase, override_settings
|
4 | 6 | from django.urls import reverse
|
5 | 7 |
|
@@ -145,3 +147,91 @@ def test_bucket_member_is_set_on_create_view_when_logged_in(self):
|
145 | 147 | history = bucket_datas.first().history.all()
|
146 | 148 |
|
147 | 149 | self.assertListEqual([h.history_user_id for h in history], [member1.id])
|
| 150 | + |
| 151 | + |
| 152 | +@override_settings(**middleware_override_settings) |
| 153 | +class MiddlewareBulkOpsTest(TestCase): |
| 154 | + def setUp(self): |
| 155 | + self.user = CustomUser.objects.create_superuser( |
| 156 | + "user_login", "[email protected]", "pass" |
| 157 | + ) |
| 158 | + |
| 159 | + def test_user_is_set_on_bulk_create_view_when_logged_in(self): |
| 160 | + self.client.force_login(self.user) |
| 161 | + self.client.post(reverse("poll-bulk-create"), data={}) |
| 162 | + polls = Poll.objects.all() |
| 163 | + self.assertEqual(len(polls), 2) |
| 164 | + |
| 165 | + poll_history = Poll.history.all() |
| 166 | + |
| 167 | + self.assertCountEqual( |
| 168 | + [ph.history_user_id for ph in poll_history], [self.user.id, self.user.id] |
| 169 | + ) |
| 170 | + |
| 171 | + def test_user_is_not_set_on_bulk_create_view_not_logged_in(self): |
| 172 | + self.client.post(reverse("poll-bulk-create"), data={}) |
| 173 | + |
| 174 | + polls = Poll.objects.all() |
| 175 | + self.assertEqual(polls.count(), 2) |
| 176 | + |
| 177 | + poll_history = Poll.history.all() |
| 178 | + |
| 179 | + self.assertListEqual([ph.history_user_id for ph in poll_history], [None, None]) |
| 180 | + |
| 181 | + def test_request_user_is_overwritten_by_default_user_on_bulk_create_view(self,): |
| 182 | + self.client.force_login(self.user) |
| 183 | + self.client.post(reverse("poll-bulk-create-with-default-user"), data={}) |
| 184 | + |
| 185 | + polls = Poll.objects.all() |
| 186 | + self.assertEqual(len(polls), 2) |
| 187 | + |
| 188 | + poll_history = Poll.history.all() |
| 189 | + |
| 190 | + self.assertFalse(any(ph.history_user_id == self.user.id for ph in poll_history)) |
| 191 | + self.assertFalse(any(ph.history_user_id is None for ph in poll_history)) |
| 192 | + |
| 193 | + @skipIf(django.VERSION < (2, 2,), reason="bulk_update does not exist before 2.2") |
| 194 | + def test_user_is_set_on_bulk_update_view_when_logged_in(self): |
| 195 | + self.client.force_login(self.user) |
| 196 | + poll_1 = Poll.objects.create(question="Test question 1", pub_date=date.today()) |
| 197 | + poll_2 = Poll.objects.create( |
| 198 | + question="Test question 2", pub_date=date(2020, 1, 1) |
| 199 | + ) |
| 200 | + |
| 201 | + self.client.post(reverse("poll-bulk-update"), data={}) |
| 202 | + |
| 203 | + polls = Poll.objects.all() |
| 204 | + self.assertEqual(2, len(polls)) |
| 205 | + |
| 206 | + self.assertEqual("1", poll_1.history.latest("history_date").question) |
| 207 | + self.assertEqual("0", poll_2.history.latest("history_date").question) |
| 208 | + self.assertEqual( |
| 209 | + self.user.id, poll_1.history.latest("history_date").history_user_id |
| 210 | + ) |
| 211 | + self.assertEqual( |
| 212 | + self.user.id, poll_2.history.latest("history_date").history_user_id |
| 213 | + ) |
| 214 | + |
| 215 | + @skipIf(django.VERSION < (2, 2,), reason="bulk_update does not exist before 2.2") |
| 216 | + def test_user_is_not_set_on_bulk_update_view_when_not_logged_in(self): |
| 217 | + poll_1 = Poll.objects.create(question="Test question 1", pub_date=date.today()) |
| 218 | + poll_2 = Poll.objects.create( |
| 219 | + question="Test question 2", pub_date=date(2020, 1, 1) |
| 220 | + ) |
| 221 | + |
| 222 | + self.client.post(reverse("poll-bulk-update"), data={}) |
| 223 | + |
| 224 | + self.assertIsNone(poll_1.history.latest("history_date").history_user_id) |
| 225 | + self.assertIsNone(poll_2.history.latest("history_date").history_user_id) |
| 226 | + |
| 227 | + @skipIf(django.VERSION < (2, 2,), reason="bulk_update does not exist before 2.2") |
| 228 | + def test_request_user_is_overwritten_by_default_user_on_bulk_update(self): |
| 229 | + self.client.force_login(self.user) |
| 230 | + poll = Poll.objects.create(pub_date=date(2020, 1, 1), question="123") |
| 231 | + |
| 232 | + self.client.post(reverse("poll-bulk-update-with-default-user"), data={}) |
| 233 | + |
| 234 | + self.assertIsNotNone(poll.history.latest("history_date").history_user_id) |
| 235 | + self.assertNotEqual( |
| 236 | + self.user.id, poll.history.latest("history_date").history_user_id |
| 237 | + ) |
0 commit comments