|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+from http import client as http_client
|
|
|
2
|
+
|
|
|
3
|
+from django.conf import settings
|
|
|
4
|
+from django.contrib.messages import ERROR, INFO
|
|
|
5
|
+from django.urls import reverse
|
|
|
6
|
+from django.utils.text import slugify
|
|
|
7
|
+from django.utils.translation import gettext
|
|
|
8
|
+
|
|
|
9
|
+from oscar.core.loading import get_class, get_model
|
|
|
10
|
+from oscar.test.factories import (
|
|
|
11
|
+ OptionFactory, ProductClassFactory, create_product)
|
|
|
12
|
+from oscar.test.testcases import WebTestCase
|
|
|
13
|
+
|
|
|
14
|
+
|
|
|
15
|
+Option = get_model('catalogue', 'Option')
|
|
|
16
|
+OptionForm = get_class('dashboard.catalogue.forms', 'OptionForm')
|
|
|
17
|
+
|
|
|
18
|
+
|
|
|
19
|
+class OptionCreateMixin(object):
|
|
|
20
|
+
|
|
|
21
|
+ def _set_up_display_create_form_vars(self):
|
|
|
22
|
+ self.url_name = 'dashboard:catalogue-option-create'
|
|
|
23
|
+ self.title = gettext("Add a new Option")
|
|
|
24
|
+
|
|
|
25
|
+ def _test_display_create_form_response(self):
|
|
|
26
|
+ response = self.response
|
|
|
27
|
+
|
|
|
28
|
+ self.assertEqual(response.status_code, http_client.OK)
|
|
|
29
|
+ self.assertTemplateUsed(response, 'dashboard/catalogue/option_form.html')
|
|
|
30
|
+ self.assertInContext(response, 'form')
|
|
|
31
|
+ self.assertIsInstance(response.context['form'], OptionForm)
|
|
|
32
|
+ self.assertTrue(response.context['form'].instance._state.adding)
|
|
|
33
|
+ self.assertInContext(response, 'title')
|
|
|
34
|
+ self.assertEqual(response.context['title'], self.title)
|
|
|
35
|
+
|
|
|
36
|
+ def _set_up_create_vars(self):
|
|
|
37
|
+ self.url_name = 'dashboard:catalogue-option-create'
|
|
|
38
|
+ self.option_name = 'Test Option'
|
|
|
39
|
+
|
|
|
40
|
+ def _set_up_create_success_vars(self):
|
|
|
41
|
+ self.success_url_name = 'dashboard:catalogue-option-list'
|
|
|
42
|
+ self.success_message = gettext("Option created successfully")
|
|
|
43
|
+
|
|
|
44
|
+ def _test_creation_of_objects(self):
|
|
|
45
|
+ # Test the creation of the option
|
|
|
46
|
+ self.assertEqual(1, Option.objects.all().count())
|
|
|
47
|
+ option = Option.objects.first()
|
|
|
48
|
+ self.assertEqual(option.name, self.option_name)
|
|
|
49
|
+
|
|
|
50
|
+
|
|
|
51
|
+class OptionUpdateMixin(object):
|
|
|
52
|
+
|
|
|
53
|
+ def _set_up_display_update_form_vars(self):
|
|
|
54
|
+ url_name = 'dashboard:catalogue-option-update'
|
|
|
55
|
+ self.url = reverse(url_name, kwargs={'pk': self.option.pk})
|
|
|
56
|
+ self.title = gettext("Update Option '%s'") % self.option.name
|
|
|
57
|
+
|
|
|
58
|
+ def _test_display_update_form_response(self):
|
|
|
59
|
+ response = self.response
|
|
|
60
|
+
|
|
|
61
|
+ self.assertEqual(response.status_code, http_client.OK)
|
|
|
62
|
+ self.assertTemplateUsed(response, 'dashboard/catalogue/option_form.html')
|
|
|
63
|
+ self.assertInContext(response, 'form')
|
|
|
64
|
+ self.assertIsInstance(response.context['form'], OptionForm)
|
|
|
65
|
+ self.assertEqual(response.context['form'].instance, self.option)
|
|
|
66
|
+ self.assertInContext(response, 'title')
|
|
|
67
|
+ self.assertEqual(response.context['title'], self.title)
|
|
|
68
|
+
|
|
|
69
|
+ def _set_up_update_vars(self):
|
|
|
70
|
+ url_name = 'dashboard:catalogue-option-update'
|
|
|
71
|
+ self.url = reverse(url_name, kwargs={'pk': self.option.pk})
|
|
|
72
|
+ self.option_name = 'Test Option'
|
|
|
73
|
+
|
|
|
74
|
+ def _set_up_update_success_vars(self):
|
|
|
75
|
+ self.success_url_name = 'dashboard:catalogue-option-list'
|
|
|
76
|
+ self.success_message = gettext("Option updated successfully")
|
|
|
77
|
+
|
|
|
78
|
+ def _test_update_of_objects(self):
|
|
|
79
|
+ # Test the update of the option
|
|
|
80
|
+ option = Option.objects.first()
|
|
|
81
|
+ self.assertEqual(option.name, self.option_name)
|
|
|
82
|
+
|
|
|
83
|
+
|
|
|
84
|
+class OptionDeleteMixin(object):
|
|
|
85
|
+
|
|
|
86
|
+ def _set_up_display_delete_form_vars(self):
|
|
|
87
|
+ url_name = 'dashboard:catalogue-option-delete'
|
|
|
88
|
+ self.url = reverse(url_name, kwargs={'pk': self.option.pk})
|
|
|
89
|
+
|
|
|
90
|
+ def _set_up_display_delete_form_allowed_vars(self):
|
|
|
91
|
+ self.title = gettext("Delete Option '%s'") % self.option.name
|
|
|
92
|
+
|
|
|
93
|
+ def _set_up_display_delete_form_disallowed_objects(self):
|
|
|
94
|
+ product_class = ProductClassFactory()
|
|
|
95
|
+ product = create_product(product_class=product_class)
|
|
|
96
|
+ product_class.options.add(self.option)
|
|
|
97
|
+ product.product_options.add(self.option)
|
|
|
98
|
+
|
|
|
99
|
+ def _set_up_display_delete_form_disallowed_vars(self):
|
|
|
100
|
+ self.title = gettext("Unable to delete '%s'") % self.option.name
|
|
|
101
|
+ self.error_product_message = gettext("1 products are still assigned to this option")
|
|
|
102
|
+ self.error_class_message = gettext("1 product classes are still assigned to this option")
|
|
|
103
|
+
|
|
|
104
|
+ def _test_display_delete_form_response(self):
|
|
|
105
|
+ response = self.response
|
|
|
106
|
+
|
|
|
107
|
+ self.assertEqual(response.status_code, http_client.OK)
|
|
|
108
|
+ self.assertTemplateUsed(response, 'dashboard/catalogue/option_delete.html')
|
|
|
109
|
+ self.assertInContext(response, 'title')
|
|
|
110
|
+ self.assertEqual(response.context['title'], self.title)
|
|
|
111
|
+
|
|
|
112
|
+ def _test_display_delete_disallowed_response(self):
|
|
|
113
|
+ response = self.response
|
|
|
114
|
+
|
|
|
115
|
+ self.assertInContext(response, 'disallow')
|
|
|
116
|
+ self.assertTrue(response.context['disallow'])
|
|
|
117
|
+ messages = list(response.context['messages'])
|
|
|
118
|
+ self.assertEqual(len(messages), 2)
|
|
|
119
|
+ self.assertEqual(messages[0].level, ERROR)
|
|
|
120
|
+ self.assertEqual(messages[1].level, ERROR)
|
|
|
121
|
+ self.assertEqual(messages[0].message, self.error_product_message)
|
|
|
122
|
+ self.assertEqual(messages[1].message, self.error_class_message)
|
|
|
123
|
+
|
|
|
124
|
+ def _set_up_delete_vars(self):
|
|
|
125
|
+ url_name = 'dashboard:catalogue-option-delete'
|
|
|
126
|
+ self.url = reverse(url_name, kwargs={'pk': self.option.pk})
|
|
|
127
|
+
|
|
|
128
|
+ def _set_up_delete_success_vars(self):
|
|
|
129
|
+ self.success_url_name = 'dashboard:catalogue-option-list'
|
|
|
130
|
+ self.success_message = gettext("Option deleted successfully")
|
|
|
131
|
+
|
|
|
132
|
+ def _test_deletion_of_objects(self):
|
|
|
133
|
+ # Test the deletion of the option
|
|
|
134
|
+ option_exists = Option.objects.exists()
|
|
|
135
|
+ self.assertFalse(option_exists)
|
|
|
136
|
+
|
|
|
137
|
+
|
|
|
138
|
+class TestResponseOptionMixin(object):
|
|
|
139
|
+
|
|
|
140
|
+ def _test_success_response(self):
|
|
|
141
|
+ response = self.response
|
|
|
142
|
+
|
|
|
143
|
+ self.assertEqual(response.status_code, http_client.FOUND)
|
|
|
144
|
+ self.assertRedirectsTo(response, self.success_url_name)
|
|
|
145
|
+ messages = list(response.follow().context['messages'])
|
|
|
146
|
+ self.assertEqual(len(messages), 1)
|
|
|
147
|
+ self.assertEqual(messages[0].level, INFO)
|
|
|
148
|
+ self.assertEqual(messages[0].message, self.success_message)
|
|
|
149
|
+
|
|
|
150
|
+
|
|
|
151
|
+class TestOptionCreateView(OptionCreateMixin,
|
|
|
152
|
+ TestResponseOptionMixin,
|
|
|
153
|
+ WebTestCase):
|
|
|
154
|
+ is_staff = True
|
|
|
155
|
+
|
|
|
156
|
+ def test_display_create_form(self):
|
|
|
157
|
+ self._set_up_display_create_form_vars()
|
|
|
158
|
+
|
|
|
159
|
+ self.response = self.get(reverse(self.url_name))
|
|
|
160
|
+
|
|
|
161
|
+ # Test the response
|
|
|
162
|
+ self._test_display_create_form_response()
|
|
|
163
|
+
|
|
|
164
|
+ def test_create_option(self):
|
|
|
165
|
+ self._set_up_create_vars()
|
|
|
166
|
+ self._set_up_create_success_vars()
|
|
|
167
|
+
|
|
|
168
|
+ form = self.get(reverse(self.url_name)).form
|
|
|
169
|
+ form['name'] = self.option_name
|
|
|
170
|
+ self.response = form.submit()
|
|
|
171
|
+
|
|
|
172
|
+ # Test the creation of the option
|
|
|
173
|
+ self._test_creation_of_objects()
|
|
|
174
|
+
|
|
|
175
|
+ # Test the response
|
|
|
176
|
+ self._test_success_response()
|
|
|
177
|
+
|
|
|
178
|
+
|
|
|
179
|
+class TestOptionUpdateView(OptionUpdateMixin,
|
|
|
180
|
+ TestResponseOptionMixin,
|
|
|
181
|
+ WebTestCase):
|
|
|
182
|
+ is_staff = True
|
|
|
183
|
+
|
|
|
184
|
+ def setUp(self):
|
|
|
185
|
+ super().setUp()
|
|
|
186
|
+
|
|
|
187
|
+ self.option = OptionFactory()
|
|
|
188
|
+
|
|
|
189
|
+ def test_display_update_form(self):
|
|
|
190
|
+ self._set_up_display_update_form_vars()
|
|
|
191
|
+
|
|
|
192
|
+ self.response = self.get(self.url)
|
|
|
193
|
+
|
|
|
194
|
+ # Test the response
|
|
|
195
|
+ self._test_display_update_form_response()
|
|
|
196
|
+
|
|
|
197
|
+ def test_update_option(self):
|
|
|
198
|
+ self._set_up_update_vars()
|
|
|
199
|
+ self._set_up_update_success_vars()
|
|
|
200
|
+
|
|
|
201
|
+ form = self.get(self.url).form
|
|
|
202
|
+ form['name'] = self.option_name
|
|
|
203
|
+ self.response = form.submit()
|
|
|
204
|
+
|
|
|
205
|
+ # Test the update of the attribute option
|
|
|
206
|
+ self._test_update_of_objects()
|
|
|
207
|
+
|
|
|
208
|
+ # Test the response
|
|
|
209
|
+ self._test_success_response()
|
|
|
210
|
+
|
|
|
211
|
+
|
|
|
212
|
+class TestOptionListView(WebTestCase):
|
|
|
213
|
+ is_staff = True
|
|
|
214
|
+
|
|
|
215
|
+ def test_display_pagination_navigation(self):
|
|
|
216
|
+ url_name = 'dashboard:catalogue-option-list'
|
|
|
217
|
+ per_page = settings.OSCAR_DASHBOARD_ITEMS_PER_PAGE
|
|
|
218
|
+ option_name = 'Test Option #%d'
|
|
|
219
|
+
|
|
|
220
|
+ for i in range(0, int(1.5 * per_page)):
|
|
|
221
|
+ name = option_name % i
|
|
|
222
|
+ OptionFactory(name=name, code=slugify(name))
|
|
|
223
|
+
|
|
|
224
|
+ page = self.get(reverse(url_name))
|
|
|
225
|
+
|
|
|
226
|
+ # Test the pagination
|
|
|
227
|
+ self.assertContains(page, 'Page 1 of 2')
|
|
|
228
|
+
|
|
|
229
|
+
|
|
|
230
|
+class TestOptionDeleteView(OptionDeleteMixin,
|
|
|
231
|
+ TestResponseOptionMixin,
|
|
|
232
|
+ WebTestCase):
|
|
|
233
|
+ is_staff = True
|
|
|
234
|
+
|
|
|
235
|
+ def setUp(self):
|
|
|
236
|
+ super().setUp()
|
|
|
237
|
+
|
|
|
238
|
+ self.option = OptionFactory()
|
|
|
239
|
+
|
|
|
240
|
+ def test_display_delete_form(self):
|
|
|
241
|
+ self._set_up_display_delete_form_vars()
|
|
|
242
|
+ self._set_up_display_delete_form_allowed_vars()
|
|
|
243
|
+
|
|
|
244
|
+ self.response = self.get(self.url)
|
|
|
245
|
+
|
|
|
246
|
+ # Test the response
|
|
|
247
|
+ self._test_display_delete_form_response()
|
|
|
248
|
+
|
|
|
249
|
+ def test_display_disallowed_delete_via_regular_window(self):
|
|
|
250
|
+ self._set_up_display_delete_form_vars()
|
|
|
251
|
+ self._set_up_display_delete_form_disallowed_vars()
|
|
|
252
|
+ self._set_up_display_delete_form_disallowed_objects()
|
|
|
253
|
+
|
|
|
254
|
+ self.response = self.get(self.url)
|
|
|
255
|
+
|
|
|
256
|
+ # Test the response
|
|
|
257
|
+ self._test_display_delete_form_response()
|
|
|
258
|
+ self._test_display_delete_disallowed_response()
|
|
|
259
|
+
|
|
|
260
|
+ def test_delete_option(self):
|
|
|
261
|
+ self._set_up_delete_vars()
|
|
|
262
|
+ self._set_up_delete_success_vars()
|
|
|
263
|
+
|
|
|
264
|
+ form = self.get(self.url).form
|
|
|
265
|
+ self.response = form.submit()
|
|
|
266
|
+
|
|
|
267
|
+ # Test the deletion of the option
|
|
|
268
|
+ self._test_deletion_of_objects()
|
|
|
269
|
+
|
|
|
270
|
+ # Test the response
|
|
|
271
|
+ self._test_success_response()
|