From 3f357c7e5450b69d180ba56d2fba402d123bfda7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 25 Jan 2022 15:50:31 +0100
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

diff --git a/tests/api/test_all.py b/tests/api/test_all.py
index 54b1943..ef728bb 100644
--- a/tests/api/test_all.py
+++ b/tests/api/test_all.py
@@ -16,7 +16,6 @@ from chrono.agendas.models import (
     Event,
     MeetingType,
     Resource,
-    Subscription,
     TimePeriodException,
     VirtualMember,
 )
@@ -767,35 +766,3 @@ def test_add_agenda(app, user, settings):
 
     resp = app.get('/api/agendas/datetimes/?agendas=%s' % agenda.slug)
     assert 'data' in resp.json
-
-
-def test_api_create_subscription(app, user):
-    agenda = Agenda.objects.create(label='Foo bar', kind='events')
-
-    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, status=401)
-
-    app.authorization = ('Basic', ('john.doe', 'password'))
-
-    params = {'user_external_id': 'xxx', 'date_start': '2021-09-01', 'date_end': '2021-10-01'}
-    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params)
-    subscription = Subscription.objects.get(pk=resp.json['id'])
-    assert subscription.agenda == agenda
-    assert subscription.user_external_id == 'xxx'
-    assert subscription.date_start == datetime.date(year=2021, month=9, day=1)
-    assert subscription.date_end == datetime.date(year=2021, month=10, day=1)
-
-    # check errors
-    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params={}, status=400)
-    assert resp.json['err_class'] == 'invalid payload'
-    for field in ('user_external_id', 'date_start', 'date_end'):
-        assert 'required' in resp.json['errors'][field][0]
-
-    params = {'user_external_id': 'xxx', 'date_start': 'wrong-format', 'date_end': 'wrong-format'}
-    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
-    assert resp.json['err_class'] == 'invalid payload'
-    assert 'wrong format' in resp.json['errors']['date_start'][0]
-    assert 'wrong format' in resp.json['errors']['date_end'][0]
-
-    params = {'user_external_id': 'xxx', 'date_start': '2021-10-01', 'date_end': '2021-09-01'}
-    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
-    assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
diff --git a/tests/api/test_subscription.py b/tests/api/test_subscription.py
new file mode 100644
index 0000000..4f4f73b
--- /dev/null
+++ b/tests/api/test_subscription.py
@@ -0,0 +1,39 @@
+import datetime
+
+import pytest
+
+from chrono.agendas.models import Agenda, Subscription
+
+pytestmark = pytest.mark.django_db
+
+
+def test_api_create_subscription(app, user):
+    agenda = Agenda.objects.create(label='Foo bar', kind='events')
+
+    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, status=401)
+
+    app.authorization = ('Basic', ('john.doe', 'password'))
+
+    params = {'user_external_id': 'xxx', 'date_start': '2021-09-01', 'date_end': '2021-10-01'}
+    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params)
+    subscription = Subscription.objects.get(pk=resp.json['id'])
+    assert subscription.agenda == agenda
+    assert subscription.user_external_id == 'xxx'
+    assert subscription.date_start == datetime.date(year=2021, month=9, day=1)
+    assert subscription.date_end == datetime.date(year=2021, month=10, day=1)
+
+    # check errors
+    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params={}, status=400)
+    assert resp.json['err_class'] == 'invalid payload'
+    for field in ('user_external_id', 'date_start', 'date_end'):
+        assert 'required' in resp.json['errors'][field][0]
+
+    params = {'user_external_id': 'xxx', 'date_start': 'wrong-format', 'date_end': 'wrong-format'}
+    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
+    assert resp.json['err_class'] == 'invalid payload'
+    assert 'wrong format' in resp.json['errors']['date_start'][0]
+    assert 'wrong format' in resp.json['errors']['date_end'][0]
+
+    params = {'user_external_id': 'xxx', 'date_start': '2021-10-01', 'date_end': '2021-09-01'}
+    resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
+    assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
-- 
2.34.1

