Skip to content

Commit 7f69286

Browse files
authored
Merge pull request #4275 from betagouv/raphodn/api-diagnostic-create-year-field-mandatory
API : Rendre le champ Diagnostic.year obligatoire à la création
2 parents 48e52e0 + 9129b3b commit 7f69286

2 files changed

Lines changed: 56 additions & 18 deletions

File tree

api/serializers/diagnostic.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979

8080
FIELDS = META_FIELDS + SIMPLE_APPRO_FIELDS + COMPLETE_APPRO_FIELDS + NON_APPRO_FIELDS
8181

82+
REQUIRED_FIELDS = ("year",)
83+
8284

8385
class DiagnosticSerializer(serializers.ModelSerializer):
8486
def to_representation(self, instance):
@@ -189,7 +191,10 @@ class Meta:
189191
def __init__(self, *args, **kwargs):
190192
action = kwargs.pop("action", None)
191193
super().__init__(*args, **kwargs)
192-
if action != "create":
194+
if action == "create":
195+
for field in REQUIRED_FIELDS:
196+
self.fields[field].required = True
197+
else:
193198
self.fields.pop("creation_mtm_source")
194199
self.fields.pop("creation_mtm_campaign")
195200
self.fields.pop("creation_mtm_medium")

api/tests/test_diagnostics.py

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ def test_unauthenticated_create_diagnostic_call(self):
1616
When calling this API unathenticated we expect a 403
1717
"""
1818
canteen = CanteenFactory.create()
19-
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}), {})
19+
payload = {"year": 2020}
20+
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}), payload)
2021
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
2122

2223
@authenticate
2324
def test_diagnostic_missing_canteen(self):
2425
"""
2526
When calling this API on an unexistent canteen we expect a 404
2627
"""
27-
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": 999}), {})
28+
payload = {"year": 2020}
29+
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": 999}), payload)
2830
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
2931

3032
@authenticate
@@ -34,11 +36,41 @@ def test_diagnostic_forbidden_canteen(self):
3436
we expect a 403
3537
"""
3638
canteen = CanteenFactory.create()
37-
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}), {})
39+
payload = {"year": 2020}
40+
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}), payload)
3841
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
3942

4043
@authenticate
41-
def test_create_diagnostic(self):
44+
def test_create_empty_diagnostic_error(self):
45+
"""
46+
When calling this API on a canteen that the user manages
47+
we need to provide the required field(s)
48+
"""
49+
canteen = CanteenFactory.create()
50+
canteen.managers.add(authenticate.user)
51+
52+
payload = {}
53+
54+
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}), payload)
55+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
56+
57+
@authenticate
58+
def test_create_minimal_diagnostic(self):
59+
"""
60+
When calling this API on a canteen that the user manages
61+
we expect a diagnostic to be created
62+
(minimal required fields)
63+
"""
64+
canteen = CanteenFactory.create()
65+
canteen.managers.add(authenticate.user)
66+
67+
payload = {"year": 2020}
68+
69+
response = self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}), payload)
70+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
71+
72+
@authenticate
73+
def test_create_full_diagnostic(self):
4274
"""
4375
When calling this API on a canteen that the user manages
4476
we expect a diagnostic to be created
@@ -249,17 +281,13 @@ def test_create_duplicate_diagnostic(self):
249281
"""
250282
canteen = CanteenFactory.create()
251283
canteen.managers.add(authenticate.user)
252-
self.client.post(
253-
reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}),
254-
{"year": 2020, "value_bio_ht": 10},
255-
)
256284

257-
payload = {
258-
"year": 2020,
259-
"value_bio_ht": 1000,
260-
}
285+
payload = {"year": 2020, "value_bio_ht": 10}
286+
self.client.post(reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}), payload)
287+
261288
try:
262289
with transaction.atomic():
290+
payload = {"year": 2020, "value_bio_ht": 1000}
263291
response = self.client.post(
264292
reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}),
265293
payload,
@@ -291,10 +319,10 @@ def test_create_diagnostic_bad_total(self):
291319
@authenticate
292320
def test_edit_diagnostic_unauthorized(self):
293321
"""
294-
The user can only edit diagnostics of canteens they
295-
manage
322+
The user can only edit diagnostics of canteens they manage
296323
"""
297-
diagnostic = DiagnosticFactory.create()
324+
diagnostic = DiagnosticFactory.create(year=2019)
325+
298326
payload = {"year": 2020}
299327

300328
response = self.client.patch(
@@ -304,7 +332,10 @@ def test_edit_diagnostic_unauthorized(self):
304332
),
305333
payload,
306334
)
335+
diagnostic.refresh_from_db()
336+
307337
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
338+
self.assertEqual(diagnostic.year, 2019)
308339

309340
@authenticate
310341
def test_edit_diagnostic(self):
@@ -313,6 +344,7 @@ def test_edit_diagnostic(self):
313344
"""
314345
diagnostic = DiagnosticFactory.create(year=2019)
315346
diagnostic.canteen.managers.add(authenticate.user)
347+
316348
payload = {"year": 2020}
317349

318350
response = self.client.patch(
@@ -327,10 +359,11 @@ def test_edit_diagnostic(self):
327359
self.assertEqual(response.status_code, status.HTTP_200_OK)
328360
self.assertEqual(diagnostic.year, 2020)
329361

330-
def test_modify_diagnostic_via_oauth2(self):
362+
def test_edit_diagnostic_via_oauth2(self):
331363
user, token = get_oauth2_token("canteen:write")
332364
diagnostic = DiagnosticFactory.create(year=2019)
333365
diagnostic.canteen.managers.add(user)
366+
334367
payload = {"year": 2020}
335368

336369
self.client.credentials(Authorization=f"Bearer {token}")
@@ -447,7 +480,6 @@ def test_edit_cancelled_diagnostic(self):
447480
),
448481
payload,
449482
)
450-
451483
diagnostic.refresh_from_db()
452484

453485
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -579,6 +611,7 @@ def test_total_leftovers_conversion_create_diagnostic(self):
579611
canteen.managers.add(authenticate.user)
580612

581613
payload = {
614+
"year": 2020,
582615
"total_leftovers": 1234.56,
583616
}
584617
response = self.client.post(

0 commit comments

Comments
 (0)