diff --git a/passerelle/contrib/toulouse_foederis/models.py b/passerelle/contrib/toulouse_foederis/models.py index f0bce36a..20ec114c 100644 --- a/passerelle/contrib/toulouse_foederis/models.py +++ b/passerelle/contrib/toulouse_foederis/models.py @@ -35,7 +35,7 @@ ATTACHMENT_SCHEMA = { '$schema': 'http://json-schema.org/draft-04/schema#', 'title': _('Attachment and degree data.'), 'description': '', - 'required': ['application_id', 'name', 'file'], + 'required': ['application_id', 'name'], 'type': 'object', 'properties': { 'application_id': { @@ -56,22 +56,27 @@ ATTACHMENT_SCHEMA = { }, 'file': { 'description': _('File to attach.'), - 'type': 'object', - 'required': ['filename', 'content_type', 'content'], - 'properties': { - 'filename': { - 'description': _('File name'), - 'type': 'string', + 'oneOf': [ + { + 'type': 'object', + 'required': ['filename', 'content_type', 'content'], + 'properties': { + 'filename': { + 'description': _('File name'), + 'type': 'string', + }, + 'content_type': { + 'description': _('MIME type'), + 'type': 'string', + }, + 'content': { + 'description': _('Content'), + 'type': 'string', + }, + }, }, - 'content_type': { - 'description': _('MIME type'), - 'type': 'string', - }, - 'content': { - 'description': _('Content'), - 'type': 'string', - }, - }, + {'type': 'null'}, + ], }, }, } @@ -705,7 +710,7 @@ class Resource(BaseResource, HTTPResource): def attach_degree(self, request, post_data): application_id = post_data['application_id'] degree_label = post_data['name'] - file = post_data['file'] + file = post_data.get('file') degree_data = self.http_request( 'POST', @@ -715,17 +720,18 @@ class Resource(BaseResource, HTTPResource): degree_id = degree_data[0]['id'] - self.http_request( - 'POST', - f'data/diplome2/{degree_id}/fields/justificatif_diplome?viewIntegrationName=api_publik', - data={ - 'contentType': file['content_type'], - 'fileName': file['filename'], - }, - files={ - 'value': (None, file['content'], None), - }, - ) + if file is not None: + self.http_request( + 'POST', + f'data/diplome2/{degree_id}/fields/justificatif_diplome?viewIntegrationName=api_publik', + data={ + 'contentType': file['content_type'], + 'fileName': file['filename'], + }, + files={ + 'value': (None, file['content'], None), + }, + ) return {'err': 0} diff --git a/tests/test_toulouse_foederis.py b/tests/test_toulouse_foederis.py index 04f65b2d..5f7c7e6e 100644 --- a/tests/test_toulouse_foederis.py +++ b/tests/test_toulouse_foederis.py @@ -665,7 +665,19 @@ class TestEndpoints: }, ) - assert response.json['err'] == 0 + assert response.json['err'] == 0 + + with httmock.HTTMock(create_degree_handler, degree_file_handler, error_handler): + response = app.post_json( + '/toulouse-foederis/foederis/attach-degree', + params={ + 'application_id': '424242', + 'name': 'DUT anarchisme', + 'file': None, + }, + ) + + assert response.json['err'] == 0 def test_migration_0003_no_null_no_charfield(migration):