Projet

Général

Profil

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

Josué Kouka, 07 juillet 2017 16:13

Télécharger (4,88 ko)

Voir les différences:

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

 passerelle/civilstatus/__init__.py | 56 ++++++++++++++++++++++++++++++++++++++
 tests/test_civilstatus.py          | 56 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 112 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
class CivilStatusMixin(object):
33

  
34
    category = _('Civil Status')
35

  
36
    def check_keys(self, formdata):
37
        for key in MANDATORY_KEYS:
38
            if (key not in formdata) and (key not in formdata.get('fields', {})):
39
                raise APIError('<%s> is required.' % key)
40
        return formdata
41

  
42
    def create_demand(self, formdata, *args, **kwargs):
43
        raise NotImplementedError
44

  
45
    def get_status(self, demand_id, **kwargs):
46
        raise NotImplementedError
47

  
48
    @endpoint(perm='can_access', methods=['post'])
49
    def create(self, request, *args, **kwargs):
50
        formdata = json.loads(request.body)
51
        formdata = self.check_keys(formdata)
52
        return self.create_demand(formdata, *args, **kwargs)
53

  
54
    @endpoint(perm='can_access', methods=['get'])
55
    def status(self, request, demand_id, **kwargs):
56
        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
        "fields": {
31
            "certificate_type": "Acte de Naissance",
32
            "certificate_type_raw": "NAIS",
33
            "applicant_is_concerned": True,
34
            "document_type": "Copie Intégrale",
35
            "document_type_raw": "CPI",
36
            "applicant_firstnames": "Johhny Jumper",
37
            "applicant_lastname": "Doe",
38
            "concerned_firstnames": "Johnny Jumper",
39
            "concerned_birthdate": "1980-07-07",
40
            "concerned_birthcity": "Nantes",
41
            "concerned_birthcountry": "France",
42
        }
43
    }
44

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

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