Projet

Général

Profil

0001-add-civil-status-common-api-16768.patch

Josué Kouka, 07 juillet 2017 13:59

Télécharger (5,16 ko)

Voir les différences:

Subject: [PATCH] add civil status common api (#16768)

 passerelle/civilstatus/__init__.py | 66 ++++++++++++++++++++++++++++++++++++++
 tests/test_civilstatus.py          | 58 +++++++++++++++++++++++++++++++++
 2 files changed, 124 insertions(+)
 create mode 100644 passerelle/civilstatus/__init__.py
 create mode 100644 tests/test_civilstatus.py
passerelle/civilstatus/__init__.py
1
# -*- coding: utf-8 -*-
2
# Passerelle - uniform access to data and services
3
# Copyright (C) 2017  Entr'ouvert
4
#
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Affero General Public License as published
7
# by the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
import json
18

  
19
from django.utils.translation import ugettext_lazy as _
20

  
21
from passerelle.utils.api import endpoint
22
from passerelle.utils.jsonresponse import APIError
23

  
24
MANDATORY_KEYS = [
25
    'display_id', 'receipt_time', 'certificate_type_raw', 'document_type_raw',
26
    'applicant_lastname', 'applicant_firstnames', 'concerned_lastname',
27
    'concerned_firstnames', 'concerned_birthdate', 'concerned_birthcountry',
28
    'concerned_birthcity'
29
]
30

  
31

  
32
def flatten_payload(*subs):
33
    result = {}
34
    for sub in subs:
35
        if not isinstance(sub, (dict,)):
36
            continue
37
        result.update(sub)  # priority to the last sub dict
38
    return result
39

  
40

  
41
class CivilStatusMixin(object):
42

  
43
    category = _('Civil Status')
44

  
45
    def check_keys(self, formdata):
46
        for key in MANDATORY_KEYS:
47
            if key not in formdata:
48
                raise APIError('<%s> is required.' % key)
49
        return formdata
50

  
51
    def create_demand(self, formdata, *args, **kwargs):
52
        raise NotImplementedError
53

  
54
    def get_status(self, demand_id, **kwargs):
55
        raise NotImplementedError
56

  
57
    @endpoint(perm='can_access', methods=['post'])
58
    def create(self, request, *args, **kwargs):
59
        formdata = json.loads(request.body)
60
        formdata.update(flatten_payload(formdata.pop('fields'), formdata.pop('extra')))
61
        formdata = self.check_keys(formdata)
62
        return self.create_demand(formdata, *args, **kwargs)
63

  
64
    @endpoint(perm='can_access', methods=['get'])
65
    def status(self, request, demand_id, **kwargs):
66
        return self.get_status(demand_id)
tests/test_civilstatus.py
1
# -*- coding: utf-8 -*-
2
import json
3
import mock
4
import pytest
5

  
6
from passerelle.civilstatus import CivilStatusMixin
7
from passerelle.utils.jsonresponse import APIError
8

  
9
from django.test import RequestFactory
10

  
11

  
12
C_DEMAND = {'demand_id': '123-NAIS-0'}
13
S_DEMAND = {'closed': True, 'status': 'rejected'}
14

  
15

  
16
def build_post_request(data):
17
    request = RequestFactory()
18
    return request.post('/', content_type='application/json',
19
                        data=json.dumps(data))
20

  
21

  
22
@mock.patch('passerelle.civilstatus.CivilStatusMixin.create_demand',
23
            side_effect=lambda x: C_DEMAND)
24
@mock.patch('passerelle.civilstatus.CivilStatusMixin.get_status',
25
            side_effect=lambda x: S_DEMAND)
26
def test_civilstatus_mixin(mock_cvs_status, mock_cvs_create, app):
27
    payload = {
28
        "display_id": "123",
29
        "receipt_time": "2017-06-11T10:30:25",
30
        "extra": {
31
            "certificate_type": "Acte de Naissance",
32
            "certificate_type_raw": "NAIS"
33
        },
34
        "fields": {
35
            "applicant_is_concerned": True,
36
            "document_type": "Copie Intégrale",
37
            "document_type_raw": "CPI",
38
            "applicant_firstnames": "Johhny Jumper",
39
            "applicant_lastname": "Doe",
40
            "concerned_firstnames": "Johnny Jumper",
41
            "concerned_birthdate": "1980-07-07",
42
            "concerned_birthcity": "Nantes",
43
            "concerned_birthcountry": "France",
44
        }
45
    }
46

  
47
    mixin = CivilStatusMixin()
48
    with pytest.raises(APIError) as exc:
49
        mixin.create(build_post_request(payload))
50

  
51
    assert exc.value.message == '<concerned_lastname> is required.'
52
    payload['fields']['concerned_lastname'] = 'Doe'
53
    request = build_post_request(payload)
54
    resp_json = mixin.create(request)
55
    assert resp_json['demand_id'] == '123-NAIS-0'
56
    resp_json = mixin.status(request, '123-NAIS-0')
57
    assert resp_json['status'] == 'rejected'
58
    assert resp_json['closed'] is True
0
-