Projet

Général

Profil

0001-misc-split-api-tests.patch

Lauréline Guérin, 25 janvier 2022 16:18

Télécharger (4,64 ko)

Voir les différences:

Subject: [PATCH 1/3] misc: split api tests

 tests/api/test_all.py          | 33 ----------------------------
 tests/api/test_subscription.py | 39 ++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 33 deletions(-)
 create mode 100644 tests/api/test_subscription.py
tests/api/test_all.py
16 16
    Event,
17 17
    MeetingType,
18 18
    Resource,
19
    Subscription,
20 19
    TimePeriodException,
21 20
    VirtualMember,
22 21
)
......
767 766

  
768 767
    resp = app.get('/api/agendas/datetimes/?agendas=%s' % agenda.slug)
769 768
    assert 'data' in resp.json
770

  
771

  
772
def test_api_create_subscription(app, user):
773
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
774

  
775
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, status=401)
776

  
777
    app.authorization = ('Basic', ('john.doe', 'password'))
778

  
779
    params = {'user_external_id': 'xxx', 'date_start': '2021-09-01', 'date_end': '2021-10-01'}
780
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params)
781
    subscription = Subscription.objects.get(pk=resp.json['id'])
782
    assert subscription.agenda == agenda
783
    assert subscription.user_external_id == 'xxx'
784
    assert subscription.date_start == datetime.date(year=2021, month=9, day=1)
785
    assert subscription.date_end == datetime.date(year=2021, month=10, day=1)
786

  
787
    # check errors
788
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params={}, status=400)
789
    assert resp.json['err_class'] == 'invalid payload'
790
    for field in ('user_external_id', 'date_start', 'date_end'):
791
        assert 'required' in resp.json['errors'][field][0]
792

  
793
    params = {'user_external_id': 'xxx', 'date_start': 'wrong-format', 'date_end': 'wrong-format'}
794
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
795
    assert resp.json['err_class'] == 'invalid payload'
796
    assert 'wrong format' in resp.json['errors']['date_start'][0]
797
    assert 'wrong format' in resp.json['errors']['date_end'][0]
798

  
799
    params = {'user_external_id': 'xxx', 'date_start': '2021-10-01', 'date_end': '2021-09-01'}
800
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
801
    assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
tests/api/test_subscription.py
1
import datetime
2

  
3
import pytest
4

  
5
from chrono.agendas.models import Agenda, Subscription
6

  
7
pytestmark = pytest.mark.django_db
8

  
9

  
10
def test_api_create_subscription(app, user):
11
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
12

  
13
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, status=401)
14

  
15
    app.authorization = ('Basic', ('john.doe', 'password'))
16

  
17
    params = {'user_external_id': 'xxx', 'date_start': '2021-09-01', 'date_end': '2021-10-01'}
18
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params)
19
    subscription = Subscription.objects.get(pk=resp.json['id'])
20
    assert subscription.agenda == agenda
21
    assert subscription.user_external_id == 'xxx'
22
    assert subscription.date_start == datetime.date(year=2021, month=9, day=1)
23
    assert subscription.date_end == datetime.date(year=2021, month=10, day=1)
24

  
25
    # check errors
26
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params={}, status=400)
27
    assert resp.json['err_class'] == 'invalid payload'
28
    for field in ('user_external_id', 'date_start', 'date_end'):
29
        assert 'required' in resp.json['errors'][field][0]
30

  
31
    params = {'user_external_id': 'xxx', 'date_start': 'wrong-format', 'date_end': 'wrong-format'}
32
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
33
    assert resp.json['err_class'] == 'invalid payload'
34
    assert 'wrong format' in resp.json['errors']['date_start'][0]
35
    assert 'wrong format' in resp.json['errors']['date_end'][0]
36

  
37
    params = {'user_external_id': 'xxx', 'date_start': '2021-10-01', 'date_end': '2021-09-01'}
38
    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
39
    assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
0
-