|
| 1 | +from datetime import date |
| 2 | + |
| 3 | +from django.conf import settings |
| 4 | +from django.test import TestCase, override_settings |
| 5 | +from django.urls import reverse |
| 6 | + |
| 7 | +from simple_history.tests.custom_user.models import CustomUser |
| 8 | +from simple_history.tests.models import Poll |
| 9 | + |
| 10 | +overridden_settings = { |
| 11 | + 'MIDDLEWARE': settings.MIDDLEWARE + |
| 12 | + ['simple_history.middleware.HistoryRequestMiddleware'], |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +class MiddlewareTest(TestCase): |
| 17 | + def setUp(self): |
| 18 | + self.user = CustomUser.objects.create_superuser( |
| 19 | + 'user_login', |
| 20 | + |
| 21 | + 'pass' |
| 22 | + ) |
| 23 | + |
| 24 | + @override_settings(**overridden_settings) |
| 25 | + def test_user_is_set_on_create_view_when_logged_in(self): |
| 26 | + self.client.force_login(self.user) |
| 27 | + data = { |
| 28 | + 'question': 'Test question', |
| 29 | + 'pub_date': '2010-01-01' |
| 30 | + } |
| 31 | + self.client.post(reverse('poll-add'), data=data) |
| 32 | + polls = Poll.objects.all() |
| 33 | + self.assertEqual(polls.count(), 1) |
| 34 | + |
| 35 | + poll_history = polls.first().history.all() |
| 36 | + |
| 37 | + self.assertListEqual([ph.history_user_id for ph in poll_history], |
| 38 | + [self.user.id]) |
| 39 | + |
| 40 | + @override_settings(**overridden_settings) |
| 41 | + def test_user_is_not_set_on_create_view_not_logged_in(self): |
| 42 | + data = { |
| 43 | + 'question': 'Test question', |
| 44 | + 'pub_date': '2010-01-01' |
| 45 | + } |
| 46 | + self.client.post(reverse('poll-add'), data=data) |
| 47 | + polls = Poll.objects.all() |
| 48 | + self.assertEqual(polls.count(), 1) |
| 49 | + |
| 50 | + poll_history = polls.first().history.all() |
| 51 | + |
| 52 | + self.assertListEqual([ph.history_user_id for ph in poll_history], |
| 53 | + [None]) |
| 54 | + |
| 55 | + @override_settings(**overridden_settings) |
| 56 | + def test_user_is_set_on_update_view_when_logged_in(self): |
| 57 | + self.client.force_login(self.user) |
| 58 | + poll = Poll.objects.create( |
| 59 | + question='Test question', |
| 60 | + pub_date=date.today() |
| 61 | + ) |
| 62 | + data = { |
| 63 | + 'question': 'Test question updated', |
| 64 | + 'pub_date': '2010-01-01' |
| 65 | + } |
| 66 | + self.client.post(reverse('poll-update', args=[poll.pk]), data=data) |
| 67 | + |
| 68 | + polls = Poll.objects.all() |
| 69 | + self.assertEqual(polls.count(), 1) |
| 70 | + |
| 71 | + poll = polls.first() |
| 72 | + self.assertEqual(poll.question, 'Test question updated') |
| 73 | + |
| 74 | + poll_history = poll.history.all() |
| 75 | + |
| 76 | + self.assertListEqual([ph.history_user_id for ph in poll_history], |
| 77 | + [self.user.id, None]) |
| 78 | + |
| 79 | + @override_settings(**overridden_settings) |
| 80 | + def test_user_is_not_set_on_update_view_when_not_logged_in(self): |
| 81 | + poll = Poll.objects.create( |
| 82 | + question='Test question', |
| 83 | + pub_date=date.today() |
| 84 | + ) |
| 85 | + data = { |
| 86 | + 'question': 'Test question updated', |
| 87 | + 'pub_date': '2010-01-01' |
| 88 | + } |
| 89 | + self.client.post(reverse('poll-update', args=[poll.pk]), data=data) |
| 90 | + |
| 91 | + polls = Poll.objects.all() |
| 92 | + self.assertEqual(polls.count(), 1) |
| 93 | + |
| 94 | + poll = polls.first() |
| 95 | + self.assertEqual(poll.question, 'Test question updated') |
| 96 | + |
| 97 | + poll_history = poll.history.all() |
| 98 | + |
| 99 | + self.assertListEqual([ph.history_user_id for ph in poll_history], |
| 100 | + [None, None]) |
| 101 | + |
| 102 | + @override_settings(**overridden_settings) |
| 103 | + def test_user_is_unset_on_update_view_after_logging_out(self): |
| 104 | + self.client.force_login(self.user) |
| 105 | + poll = Poll.objects.create( |
| 106 | + question='Test question', |
| 107 | + pub_date=date.today() |
| 108 | + ) |
| 109 | + data = { |
| 110 | + 'question': 'Test question updated', |
| 111 | + 'pub_date': '2010-01-01' |
| 112 | + } |
| 113 | + self.client.post(reverse('poll-update', args=[poll.pk]), data=data) |
| 114 | + |
| 115 | + polls = Poll.objects.all() |
| 116 | + self.assertEqual(polls.count(), 1) |
| 117 | + |
| 118 | + poll = polls.first() |
| 119 | + self.assertEqual(poll.question, 'Test question updated') |
| 120 | + |
| 121 | + self.client.logout() |
| 122 | + |
| 123 | + new_data = { |
| 124 | + 'question': 'Test question updated part 2', |
| 125 | + 'pub_date': '2010-01-01' |
| 126 | + } |
| 127 | + self.client.post(reverse('poll-update', args=[poll.pk]), data=new_data) |
| 128 | + |
| 129 | + polls = Poll.objects.all() |
| 130 | + self.assertEqual(polls.count(), 1) |
| 131 | + |
| 132 | + poll = polls.first() |
| 133 | + self.assertEqual(poll.question, 'Test question updated part 2') |
| 134 | + |
| 135 | + poll_history = poll.history.all() |
| 136 | + |
| 137 | + self.assertListEqual([ph.history_user_id for ph in poll_history], |
| 138 | + [None, self.user.id, None]) |
| 139 | + |
| 140 | + @override_settings(**overridden_settings) |
| 141 | + def test_user_is_set_on_delete_view_when_logged_in(self): |
| 142 | + self.client.force_login(self.user) |
| 143 | + poll = Poll.objects.create( |
| 144 | + question='Test question', |
| 145 | + pub_date=date.today() |
| 146 | + ) |
| 147 | + |
| 148 | + self.client.post(reverse('poll-delete', args=[poll.pk])) |
| 149 | + |
| 150 | + polls = Poll.objects.all() |
| 151 | + self.assertEqual(polls.count(), 0) |
| 152 | + |
| 153 | + poll_history = poll.history.all() |
| 154 | + |
| 155 | + self.assertListEqual([ph.history_user_id for ph in poll_history], |
| 156 | + [self.user.id, None]) |
| 157 | + |
| 158 | + @override_settings(**overridden_settings) |
| 159 | + def test_user_is_not_set_on_delete_view_when_not_logged_in(self): |
| 160 | + poll = Poll.objects.create( |
| 161 | + question='Test question', |
| 162 | + pub_date=date.today() |
| 163 | + ) |
| 164 | + |
| 165 | + self.client.post(reverse('poll-delete', args=[poll.pk])) |
| 166 | + |
| 167 | + polls = Poll.objects.all() |
| 168 | + self.assertEqual(polls.count(), 0) |
| 169 | + |
| 170 | + poll_history = poll.history.all() |
| 171 | + |
| 172 | + self.assertListEqual([ph.history_user_id for ph in poll_history], |
| 173 | + [None, None]) |
0 commit comments