toulouse-foederis: add attach-degree endpoint (#80565)
gitea/passerelle/pipeline/head This commit looks good Details

This commit is contained in:
Corentin Sechet 2023-08-25 15:53:43 +02:00
parent 659ba18a00
commit 8cdf3dcae2
2 changed files with 81 additions and 3 deletions

View File

@ -31,13 +31,13 @@ from passerelle.utils.json import datasource_array_schema, datasource_schema, re
ATTACHMENT_SCHEMA = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': _('Attachment data.'),
'title': _('Attachment and degree data.'),
'description': '',
'required': ['application_id', 'name', 'file'],
'type': 'object',
'properties': {
'application_id': {
'description': _('ID of the application to which to attach the file.'),
'description': _('ID of the application to which to attach the file or degree.'),
'oneOf': [
{
'type': 'string',
@ -49,7 +49,7 @@ ATTACHMENT_SCHEMA = {
],
},
'name': {
'description': _('Name of the attachment.'),
'description': _('Name of the attachment or label of the degree.'),
'type': 'string',
},
'file': {
@ -663,6 +663,38 @@ class Resource(BaseResource, HTTPResource):
return {'err': 0}
@endpoint(
name='attach-degree',
post={
'description': _('Attach a degree to an application.'),
'request_body': {'schema': {'application/json': ATTACHMENT_SCHEMA}},
},
)
def attach_degree(self, request, post_data):
application_id = post_data['application_id']
degree_label = post_data['name']
file = post_data['file']
degree_data = self.http_request(
'POST',
'data/diplome2?viewIntegrationName=api_publik',
json={'intitule_diplome': degree_label, 'R1258215': application_id},
)
degree_id = degree_data[0]['id']
self.http_request(
'POST',
f'data/diplome2/{degree_id}/fields/justificatif_diplome?viewIntegrationName=api_publik',
json={
'contentType': file['content_type'],
'value': file['content'],
'fileName': file['filename'],
},
)
return {'err': 0}
@endpoint(
description=_('List announces'),
long_description=_(

View File

@ -548,6 +548,52 @@ class TestEndpoints:
assert response.json['err'] == 0
def test_attach_degree(self, resource, app):
@httmock.urlmatch(path=r'^.*/data/diplome2$')
def create_degree_handler(url, request):
assert request.headers['content-type'] == 'application/json'
assert request.headers['api-key'] == APIKEY
payload = json.loads(request.body)
assert payload == {
'intitule_diplome': 'DUT anarchisme',
'R1258215': '424242',
}
return httmock.response(200, json.dumps({'code': 200, 'results': [{'id': 'DEGREE_ID'}]}))
@httmock.urlmatch(path=r'^.*/data/diplome2/DEGREE_ID/fields/justificatif_diplome$')
def degree_file_handler(url, request):
assert request.headers['content-type'] == 'application/json'
assert request.headers['api-key'] == APIKEY
payload = json.loads(request.body)
assert payload == {
'contentType': 'application/pdf',
'value': 'base 64 content',
'fileName': 'cv.pdf',
}
return httmock.response(200, json.dumps({'code': 200, 'results': [{'id': 'DEGREE_ID'}]}))
@httmock.urlmatch()
def error_handler(url, request):
assert False, 'should not be reached'
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': {
'content_type': 'application/pdf',
'content': 'base 64 content',
'filename': 'cv.pdf',
},
},
)
assert response.json['err'] == 0
def test_migration_0003_no_null_no_charfield(migration):
with connection.cursor() as cur: