From 098027423bc20927422159102f49ea9430cbd0ef Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Fri, 27 Sep 2019 11:51:17 +0200
Subject: [PATCH] gesbac: make mandatory street rivoli code or invariant number
 (#36480)

---
 passerelle/apps/gesbac/models.py |  5 ++++-
 tests/test_gesbac.py             | 16 ++++++++++++----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/passerelle/apps/gesbac/models.py b/passerelle/apps/gesbac/models.py
index 81ce06d0..eb8a7e8c 100644
--- a/passerelle/apps/gesbac/models.py
+++ b/passerelle/apps/gesbac/models.py
@@ -26,6 +26,7 @@ from django.http import Http404
 from passerelle.base.models import BaseResource
 from passerelle.utils.api import endpoint, APIError
 from passerelle.utils import SFTPField
+from passerelle.views import WrongParameter
 
 from jsonfield import JSONField
 
@@ -62,7 +63,7 @@ APPLICANT_SCHEMA = OrderedDict((
     }),
     ("street_rivoli_code", {
         "type": "string",
-        "required": True
+        "required": False,
     }),
     ("street_name", {
         "type": "string",
@@ -371,6 +372,8 @@ class Gesbac(BaseResource):
               }
     )
     def create_demand(self, request, post_data):
+        if not (post_data.get('street_rivoli_code') or post_data.get('invariant_number')):
+            raise WrongParameter(['street_rivoli_code', 'invariant_number'], [])
         form_id = post_data['form_id']
 
         form = Form.objects.create(resource=self, form_id=form_id)
diff --git a/tests/test_gesbac.py b/tests/test_gesbac.py
index f4393f13..8a3a49d1 100644
--- a/tests/test_gesbac.py
+++ b/tests/test_gesbac.py
@@ -57,7 +57,6 @@ def test_create_demand(app, resource, freezer, sftpserver):
         'producer_code': 1,
         'city_insee_code': '75114',
         'street_name': 'Château',
-        'street_rivoli_code': 'xxxx',
         'producer_social_reason': 'SCOP',
         'producer_last_name': 'Bar',
         'producer_first_name': 'Foo',
@@ -73,6 +72,9 @@ def test_create_demand(app, resource, freezer, sftpserver):
         'card_demand_purpose': 1,
         'cards_quantity': 1
     }
+    response = app.post_json('/gesbac/test/create-demand/', params=payload, status=400)
+
+    payload['street_rivoli_code'] = 'xxxx'
     response = app.post_json('/gesbac/test/create-demand/', params=payload)
     assert resource.form_set.filter(status='new').count() == 1
     form = resource.form_set.get(status='new')
@@ -81,20 +83,26 @@ def test_create_demand(app, resource, freezer, sftpserver):
                                               timestamp.strftime('%y%m%d-%H%M%S'))
     assert response.json['data']['filename'] == expected_filename
     assert resource.form_set.filter(status='new').count() == 1
+
+    del payload['street_rivoli_code']
+    payload['invariant_number'] = 'yyy'
+    response = app.post_json('/gesbac/test/create-demand/', params=payload)
+    assert resource.form_set.filter(status='new').count() == 2
+
     with sftpserver.serve_content({'output': {expected_filename: 'content'}}):
         resource.jobs()
-    assert resource.form_set.filter(status='sent').count() == 1
+    assert resource.form_set.filter(status='sent').count() == 2
 
     payload['owner_email'] = 'bar@example.com'
     response = app.post_json('/gesbac/test/create-demand/', params=payload)
     assert resource.form_set.filter(status='new').count() == 1
     form = resource.form_set.get(status='new')
     assert len(form.demand_data) == 2
-    assert response.json['data']['filename'] == '%s%s-4200242.csv' % (resource.output_files_prefix,
+    assert response.json['data']['filename'] == '%s%s-4200342.csv' % (resource.output_files_prefix,
                                                                       timestamp.strftime('%y%m%d-%H%M%S'))
     with sftpserver.serve_content({'output': {response.json['data']['filename']: 'content'}}):
         resource.jobs()
-    assert resource.form_set.filter(status='sent').count() == 2
+    assert resource.form_set.filter(status='sent').count() == 3
 
 
 def test_get_demand_response(app, resource, freezer, sftpserver):
-- 
2.23.0

