Projet

Général

Profil

0001-planitech-create-users-under-the-hood-29514.patch

Emmanuel Cazenave, 10 janvier 2019 12:26

Télécharger (10,6 ko)

Voir les différences:

Subject: [PATCH] planitech: create users under the hood (#29514)

 functests/planitech/test_planitech.py         | 11 ++-
 .../planitech/migrations/0003_pairing.py      | 30 +++++++
 passerelle/contrib/planitech/models.py        | 51 +++++++++++-
 tests/test_planitech.py                       | 83 ++++++++++++++++---
 4 files changed, 160 insertions(+), 15 deletions(-)
 create mode 100644 passerelle/contrib/planitech/migrations/0003_pairing.py
functests/planitech/test_planitech.py
1
import pprint
1 2
import random
2 3
import urllib
3 4

  
......
50 51
    # create reservation
51 52
    params = {
52 53
        'date': chosen_date, 'start_time': '10:00', 'end_time': '11:00',
53
        'place_id': place, 'price': 200
54
        'place_id': place, 'price': 200, 'name_id': 'john-doe',
55
        'first_name': 'jon', 'last_name': 'doe',
56
        'email': 'jon.doe@localhost'
54 57
    }
58
    print('Create reservation parameters \n')
59
    pprint.pprint(params)
60
    print('\n')
55 61
    url = conn + '/createreservation'
56 62
    resp = requests.post(url, json=params)
57 63
    resp.raise_for_status()
58 64
    res = resp.json()
65
    print('Create reservation response \n')
66
    pprint.pprint(res)
67
    print('\n')
59 68
    assert res['err'] == 0
60 69
    data = res['data']
61 70
    assert 'reservation_id' in data
passerelle/contrib/planitech/migrations/0003_pairing.py
1
# -*- coding: utf-8 -*-
2
# Generated by Django 1.11.17 on 2019-01-10 11:08
3
from __future__ import unicode_literals
4

  
5
from django.db import migrations, models
6
import django.db.models.deletion
7

  
8

  
9
class Migration(migrations.Migration):
10

  
11
    dependencies = [
12
        ('planitech', '0002_planitechconnector_custom_fields'),
13
    ]
14

  
15
    operations = [
16
        migrations.CreateModel(
17
            name='Pairing',
18
            fields=[
19
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20
                ('name_id', models.CharField(max_length=256)),
21
                ('external_id', models.CharField(max_length=256)),
22
                ('created', models.DateTimeField(auto_now_add=True)),
23
                ('resource', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='planitech.PlanitechConnector')),
24
            ],
25
        ),
26
        migrations.AlterUniqueTogether(
27
            name='pairing',
28
            unique_together=set([('resource', 'name_id'), ('resource', 'external_id')]),
29
        ),
30
    ]
passerelle/contrib/planitech/models.py
19 19
import json
20 20
import re
21 21
import urlparse
22
import uuid
22 23

  
23 24
from django.core.cache import cache
24
from django.db import models
25
from django.db import models, transaction
25 26
from django.utils import dateformat
26 27
from django.utils import dateparse
27 28
from django.utils.translation import ugettext_lazy as _
......
64 65
            "description": "Price",
65 66
            "type": "number",
66 67
            "required": True
68
        },
69
        "name_id": {
70
            "description": "Publik user nameID",
71
            "type": "string",
72
            "required": True
73
        },
74
        "first_name": {
75
            "description": "First name",
76
            "type": "string",
77
            "required": True
78
        },
79
        "last_name": {
80
            "description": "Last name",
81
            "type": "string",
82
            "required": True
83
        },
84
        "email": {
85
            "description": "Email",
86
            "type": "string",
87
            "required": True
67 88
        }
68 89
    }
69 90
}
......
278 299
        end_datetime = combine_date_time(post_data['date'], post_data['end_time'])
279 300
        request_date = datetime.now()
280 301

  
302
        with transaction.atomic():
303
            pairing, created = Pairing.objects.get_or_create(
304
                resource=self, name_id=post_data['name_id'],
305
                defaults={'external_id': uuid.uuid4().get_hex()})
306
            if created:
307
                params = {
308
                    "externalUserIdentifier": pairing.external_id,
309
                    "name": post_data['last_name'],
310
                    "firstName": post_data['first_name'],
311
                    "mail": post_data['email']
312
                }
313
                data = self._call_planitech(self.requests.post, 'createPerson', params)
314
                if data.get('creationStatus') != 'OK':
315
                    raise APIError("Person creation failed: %s" % data.get('creationStatus'))
316

  
281 317
        params = {
282 318
            "activityID": mste.Uint32(6),  # relaxation FIXME
283
            "contractorExternalIdentifier": "test-entrouvert",  # FIXME
319
            "contractorExternalIdentifier": pairing.external_id,
284 320
            "end": end_datetime,
285 321
            "isWeekly": False,
286 322
            "object": "reservation  test entouvert",  # FIXME
......
513 549
                'raw_data': data
514 550
            }
515 551
        }
552

  
553

  
554
class Pairing(models.Model):
555

  
556
    class Meta:
557
        unique_together = (('resource', 'name_id'), ('resource', 'external_id'),)
558

  
559
    resource = models.ForeignKey(PlanitechConnector)
560
    name_id = models.CharField(blank=False, max_length=256)
561
    external_id = models.CharField(blank=False, max_length=256)
562
    created = models.DateTimeField(auto_now_add=True)
tests/test_planitech.py
9 9

  
10 10
from passerelle.base.models import ApiUser, AccessRight
11 11
from passerelle.contrib.planitech import mste
12
from passerelle.contrib.planitech.models import PlanitechConnector
12
from passerelle.contrib.planitech.models import PlanitechConnector, Pairing
13 13
from passerelle.utils.jsonresponse import APIError
14 14

  
15 15

  
......
141 141

  
142 142

  
143 143
def test_create_reservation(app, connector, monkeypatch):
144
    mock_call_planitech = mock_planitech(
145
        monkeypatch, return_value={
144
    side_effect = [
145
        {
146
            'creationStatus': 'OK',
147
            'externalUserIdentifier': '22b9c0d91fdc4f379d1356a4aaa9d38b',
148
            'requestDate': datetime(2019, 1, 9, 15, 41),
149
            'requestName': 'createPerson',
150
            'responseDate': datetime(2019, 1, 9, 15, 41),
151
            'userIdentifier': 13826.0
152
        },
153
        {
146 154
            'creationStatus': 'OK',
147 155
            'reservationIdentifier': 1
148
        })
156
        }
157
    ]
158
    mock_call_planitech = mock_planitech(monkeypatch, side_effect=side_effect)
149 159
    response = app.post_json(
150 160
        '/planitech/slug-planitech/createreservation',
151 161
        params={
......
153 163
            'start_time': '10:00',
154 164
            'end_time': '11:00',
155 165
            'place_id': 1,
156
            'price': 10
166
            'price': 10,
167
            'name_id': 'john-doe',
168
            'first_name': 'jon',
169
            'last_name': 'doe',
170
            'email': 'jon.doe@localhost'
157 171
        }
158 172
    )
159 173
    json_resp = response.json
160 174
    assert json_resp['err'] == 0
161 175
    assert json_resp['data']['reservation_id'] == 1
162
    call_params = mock_call_planitech.call_args[0][2]
163
    assert call_params['start'] == datetime(2018, 11, 11, 10, 0)
164
    assert call_params['end'] == datetime(2018, 11, 11, 11, 0)
165
    assert call_params['places'] == [1]
166
    assert call_params['price'] == 10
176

  
177
    person_args = mock_call_planitech.call_args_list[0][0]
178
    assert person_args[1] == 'createPerson'
179
    person_args = person_args[2]
180
    assert person_args['mail'] == 'jon.doe@localhost'
181
    assert person_args['firstName'] == 'jon'
182
    assert person_args['name'] == 'doe'
183
    external_id = person_args['externalUserIdentifier']
184
    assert Pairing.objects.get(resource=connector, name_id='john-doe').external_id == external_id
185

  
186
    reservation_args = mock_call_planitech.call_args_list[1][0]
187
    assert reservation_args[1] == 'createReservation'
188
    reservation_args = reservation_args[2]
189
    assert reservation_args['start'] == datetime(2018, 11, 11, 10, 0)
190
    assert reservation_args['end'] == datetime(2018, 11, 11, 11, 0)
191
    assert reservation_args['places'] == [1]
192
    assert reservation_args['price'] == 10
193

  
194
    # Second reservation for same user : no planitech person created
195
    mock_call_planitech = mock_planitech(monkeypatch, return_value=side_effect[1])
196
    response = app.post_json(
197
        '/planitech/slug-planitech/createreservation',
198
        params={
199
            'date': '2018-11-11',
200
            'start_time': '10:00',
201
            'end_time': '11:00',
202
            'place_id': 1,
203
            'price': 10,
204
            'name_id': 'john-doe',
205
            'first_name': 'jon',
206
            'last_name': 'doe',
207
            'email': 'jon.doe@localhost'
208
        }
209
    )
210
    json_resp = response.json
211
    assert json_resp['err'] == 0
212
    assert json_resp['data']['reservation_id'] == 1
213

  
214
    reservation_args = mock_call_planitech.call_args_list[0][0]
215
    assert reservation_args[1] == 'createReservation'
216
    assert Pairing.objects.count() == 1
167 217

  
168 218
    # Create reservation failed
169 219
    mock_call_planitech = mock_planitech(
......
178 228
            'start_time': '10:00',
179 229
            'end_time': '11:00',
180 230
            'place_id': 1,
181
            'price': 10
231
            'price': 10,
232
            'name_id': 'john-doe',
233
            'first_name': 'jon',
234
            'last_name': 'doe',
235
            'email': 'jon.doe@localhost'
236

  
182 237
        }
183 238
    )
184 239
    json_resp = response.json
......
197 252
            'start_time': '10:00',
198 253
            'end_time': '11:00',
199 254
            'place_id': 1,
200
            'price': 10
255
            'price': 10,
256
            'name_id': 'john-doe',
257
            'first_name': 'jon',
258
            'last_name': 'doe',
259
            'email': 'jon.doe@localhost'
201 260
        }
202 261
    )
203 262
    json_resp = response.json
204
-