|
| 1 | +from django.test import TestCase, RequestFactory |
| 2 | +from django.contrib.auth.models import User |
| 3 | +from django.urls import reverse |
| 4 | + |
| 5 | + |
| 6 | +from tests.market.admin import InventoryAdmin, ShoppingMallAdmin |
| 7 | +from tests.market.models import Item, Inventory, ShoppingMall |
| 8 | +from tests.factories import ItemFactory, ShopFactory, InventoryFactory |
| 9 | + |
| 10 | + |
| 11 | +class TestConfirmChangeAndAddM2MField(TestCase): |
| 12 | + @classmethod |
| 13 | + def setUpTestData(cls): |
| 14 | + cls.superuser = User.objects.create_superuser( |
| 15 | + username="super", email="[email protected]", password="pass" |
| 16 | + ) |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + self.client.force_login(self.superuser) |
| 20 | + self.factory = RequestFactory() |
| 21 | + |
| 22 | + def test_post_add_without_confirm_add_m2m(self): |
| 23 | + shops = [ShopFactory() for i in range(3)] |
| 24 | + |
| 25 | + data = {"name": "name", "shops": [s.id for s in shops]} |
| 26 | + response = self.client.post(reverse("admin:market_shoppingmall_add"), data) |
| 27 | + # Redirects to changelist and is added |
| 28 | + self.assertEqual(response.status_code, 302) |
| 29 | + self.assertEqual(response.url, "/admin/market/shoppingmall/") |
| 30 | + self.assertEqual(ShoppingMall.objects.count(), 1) |
| 31 | + |
| 32 | + def test_post_add_with_confirm_add_m2m(self): |
| 33 | + ShoppingMallAdmin.confirmation_fields = ["shops"] |
| 34 | + shops = [ShopFactory() for i in range(3)] |
| 35 | + |
| 36 | + data = {"name": "name", "shops": [s.id for s in shops], "_confirm_add": True} |
| 37 | + response = self.client.post(reverse("admin:market_shoppingmall_add"), data) |
| 38 | + |
| 39 | + # Ensure not redirected (confirmation page does not redirect) |
| 40 | + self.assertEqual(response.status_code, 200) |
| 41 | + expected_templates = [ |
| 42 | + "admin/market/shoppingmall/change_confirmation.html", |
| 43 | + "admin/market/change_confirmation.html", |
| 44 | + "admin/change_confirmation.html", |
| 45 | + ] |
| 46 | + self.assertEqual(response.template_name, expected_templates) |
| 47 | + form_data = [("name", "name")] + [("shops", s.id) for s in shops] |
| 48 | + for k, v in form_data: |
| 49 | + self.assertIn( |
| 50 | + f'<input type="hidden" name="{ k }" value="{ v }">', |
| 51 | + response.rendered_content, |
| 52 | + ) |
| 53 | + |
| 54 | + # Should not have been added yet |
| 55 | + self.assertEqual(ShoppingMall.objects.count(), 0) |
| 56 | + |
| 57 | + def test_m2m_field_post_change_with_confirm_change(self): |
| 58 | + shops = [ShopFactory() for i in range(10)] |
| 59 | + shopping_mall = ShoppingMall.objects.create(name="My Mall") |
| 60 | + shopping_mall.shops.set(shops) |
| 61 | + # Currently ShoppingMall configured with confirmation_fields = ['name'] |
| 62 | + data = { |
| 63 | + "name": "Not My Mall", |
| 64 | + "shops": "1", |
| 65 | + "id": shopping_mall.id, |
| 66 | + "_confirm_change": True, |
| 67 | + "csrfmiddlewaretoken": "fake token", |
| 68 | + "_save": True, |
| 69 | + } |
| 70 | + response = self.client.post( |
| 71 | + f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data |
| 72 | + ) |
| 73 | + # Ensure not redirected (confirmation page does not redirect) |
| 74 | + self.assertEqual(response.status_code, 200) |
| 75 | + expected_templates = [ |
| 76 | + "admin/market/shoppingmall/change_confirmation.html", |
| 77 | + "admin/market/change_confirmation.html", |
| 78 | + "admin/change_confirmation.html", |
| 79 | + ] |
| 80 | + self.assertEqual(response.template_name, expected_templates) |
| 81 | + form_data = { |
| 82 | + "name": "Not My Mall", |
| 83 | + "shops": "1", |
| 84 | + "id": str(shopping_mall.id), |
| 85 | + } |
| 86 | + |
| 87 | + for k, v in form_data.items(): |
| 88 | + self.assertIn( |
| 89 | + f'<input type="hidden" name="{ k }" value="{ v }">', |
| 90 | + response.rendered_content, |
| 91 | + ) |
| 92 | + |
| 93 | + # Hasn't changed item yet |
| 94 | + shopping_mall.refresh_from_db() |
| 95 | + self.assertEqual(shopping_mall.name, "My Mall") |
| 96 | + |
| 97 | + def test_m2m_field_post_change_with_confirm_change_multiple_selected(self): |
| 98 | + shops = [ShopFactory() for i in range(10)] |
| 99 | + shopping_mall = ShoppingMall.objects.create(name="My Mall") |
| 100 | + shopping_mall.shops.set(shops) |
| 101 | + # Currently ShoppingMall configured with confirmation_fields = ['name'] |
| 102 | + data = { |
| 103 | + "name": "Not My Mall", |
| 104 | + "shops": ["1", "2", "3"], |
| 105 | + "id": shopping_mall.id, |
| 106 | + "_confirm_change": True, |
| 107 | + "csrfmiddlewaretoken": "fake token", |
| 108 | + "_save": True, |
| 109 | + } |
| 110 | + response = self.client.post( |
| 111 | + f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data |
| 112 | + ) |
| 113 | + # Ensure not redirected (confirmation page does not redirect) |
| 114 | + self.assertEqual(response.status_code, 200) |
| 115 | + expected_templates = [ |
| 116 | + "admin/market/shoppingmall/change_confirmation.html", |
| 117 | + "admin/market/change_confirmation.html", |
| 118 | + "admin/change_confirmation.html", |
| 119 | + ] |
| 120 | + self.assertEqual(response.template_name, expected_templates) |
| 121 | + form_data = [ |
| 122 | + ("name", "Not My Mall"), |
| 123 | + ("shops", "1"), |
| 124 | + ("shops", "2"), |
| 125 | + ("shops", "3"), |
| 126 | + ("id", str(shopping_mall.id)), |
| 127 | + ] |
| 128 | + |
| 129 | + for k, v in form_data: |
| 130 | + self.assertIn( |
| 131 | + f'<input type="hidden" name="{ k }" value="{ v }">', |
| 132 | + response.rendered_content, |
| 133 | + ) |
| 134 | + |
| 135 | + # Hasn't changed item yet |
| 136 | + shopping_mall.refresh_from_db() |
| 137 | + self.assertEqual(shopping_mall.name, "My Mall") |
| 138 | + |
| 139 | + def test_post_change_without_confirm_change_m2m_value(self): |
| 140 | + # make the m2m the confirmation_field |
| 141 | + ShoppingMallAdmin.confirmation_fields = ["shops"] |
| 142 | + shops = [ShopFactory() for i in range(3)] |
| 143 | + shopping_mall = ShoppingMall.objects.create(name="name") |
| 144 | + shopping_mall.shops.set(shops) |
| 145 | + assert shopping_mall.shops.count() == 3 |
| 146 | + |
| 147 | + data = {"name": "name", "id": str(shopping_mall.id), "shops": ["1"]} |
| 148 | + response = self.client.post( |
| 149 | + f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data |
| 150 | + ) |
| 151 | + # Redirects to changelist |
| 152 | + self.assertEqual(response.status_code, 302) |
| 153 | + self.assertEqual(response.url, "/admin/market/shoppingmall/") |
| 154 | + # Shop has changed |
| 155 | + shopping_mall.refresh_from_db() |
| 156 | + self.assertEqual(shopping_mall.shops.count(), 1) |
| 157 | + |
| 158 | + def test_form_invalid_m2m_value(self): |
| 159 | + ShoppingMallAdmin.confirmation_fields = ["shops"] |
| 160 | + shopping_mall = ShoppingMall.objects.create(name="name") |
| 161 | + |
| 162 | + data = { |
| 163 | + "id": shopping_mall.id, |
| 164 | + "name": "name", |
| 165 | + "shops": ["1", "2", "3"], # These shops don't exist |
| 166 | + "_confirm_change": True, |
| 167 | + "csrfmiddlewaretoken": "fake token", |
| 168 | + } |
| 169 | + response = self.client.post( |
| 170 | + f"/admin/market/shoppingmall/{shopping_mall.id}/change/", data |
| 171 | + ) |
| 172 | + |
| 173 | + # Form invalid should show errors on form |
| 174 | + self.assertEqual(response.status_code, 200) |
| 175 | + self.assertIsNotNone(response.context_data.get("errors")) |
| 176 | + self.assertTrue( |
| 177 | + str(response.context_data["errors"][0][0]).startswith( |
| 178 | + "Select a valid choice." |
| 179 | + ) |
| 180 | + ) |
| 181 | + # Should not have updated inventory |
| 182 | + shopping_mall.refresh_from_db() |
| 183 | + self.assertEqual(shopping_mall.shops.count(), 0) |
0 commit comments