From 0826331cafbc0cec407f79084cc34ce86faacc17 Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Tue, 13 Jun 2017 18:51:17 +0200 Subject: [PATCH] add civil status common api (#16768) --- passerelle/civilstatus/__init__.py | 55 ++++++++++++++++++++++++++++++++++++++ tests/test_civilstatus.py | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 passerelle/civilstatus/__init__.py create mode 100644 tests/test_civilstatus.py diff --git a/passerelle/civilstatus/__init__.py b/passerelle/civilstatus/__init__.py new file mode 100644 index 0000000..dcf4c4d --- /dev/null +++ b/passerelle/civilstatus/__init__.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Passerelle - uniform access to data and services +# Copyright (C) 2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +import json + +from django.utils.translation import ugettext_lazy as _ + +from passerelle.utils.api import endpoint +from passerelle.utils.jsonresponse import APIError + +MANDATORY_KEYS = [ + 'display_id', 'receipt_time', 'certificate_type', 'document_type', + 'applicant_lastname', 'applicant_firstnames', 'concerned_lastname', + 'concerned_firstnames', 'concerned_birthdate', 'concerned_birthcountry', + 'concerned_birthcity' +] + + +class CivilStatusMixin(object): + + category = _('Civil Status') + + def check_keys(self, formdata): + for key in MANDATORY_KEYS: + if (key not in formdata): + raise APIError('<%s> is required.' % key) + + def create_demand(self, formdata, *args, **kwargs): + raise NotImplementedError + + def get_status(self, demand_id, **kwargs): + raise NotImplementedError + + @endpoint(perm='can_access', methods=['post']) + def create(self, request, *args, **kwargs): + formdata = json.loads(request.body) + self.check_keys(formdata) + return self.create_demand(formdata, *args, **kwargs) + + @endpoint(perm='can_access', methods=['get']) + def status(self, request, demand_id, **kwargs): + return self.get_status(demand_id) diff --git a/tests/test_civilstatus.py b/tests/test_civilstatus.py new file mode 100644 index 0000000..67ffeda --- /dev/null +++ b/tests/test_civilstatus.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +import json +import mock +import pytest + +from passerelle.civilstatus import CivilStatusMixin +from passerelle.utils.jsonresponse import APIError + +from django.test import RequestFactory + + +C_DEMAND = {'demand_id': '123-NAIS-0'} +S_DEMAND = {'closed': True, 'status': 'rejected'} + + +def build_post_request(data): + request = RequestFactory() + return request.post('/', content_type='application/json', + data=json.dumps(data)) + + +@mock.patch('passerelle.civilstatus.CivilStatusMixin.create_demand', + side_effect=lambda x: C_DEMAND) +@mock.patch('passerelle.civilstatus.CivilStatusMixin.get_status', + side_effect=lambda x: S_DEMAND) +def test_civilstatus_mixin(mock_cvs_status, mock_cvs_create, app): + payload = { + "display_id": "123", + "receipt_time": "2017-06-11T10:30:25", + "certificate_type": "Acte de Naissance", + "certificate_type_raw": "NAIS", + "applicant_is_concerned": True, + "document_type": "Copie Intégrale", + "document_type_raw": "CPI", + "applicant_firstnames": "Johhny Jumper", + "applicant_lastname": "Doe", + "concerned_firstnames": "Johnny Jumper", + "concerned_birthdate": "1980-07-07", + "concerned_birthcity": "Nantes", + "concerned_birthcountry": "France", + } + + mixin = CivilStatusMixin() + with pytest.raises(APIError) as exc: + mixin.create(build_post_request(payload)) + + assert exc.value.message == ' is required.' + payload['concerned_lastname'] = 'Doe' + request = build_post_request(payload) + resp_json = mixin.create(request) + assert resp_json['demand_id'] == '123-NAIS-0' + resp_json = mixin.status(request, '123-NAIS-0') + assert resp_json['status'] == 'rejected' + assert resp_json['closed'] is True -- 2.11.0