From 789e5aa6e9eeddeb29f1ab0dcfa2be0c13b2cca6 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 12 Apr 2023 07:44:23 +0200 Subject: [PATCH] tests: add more --- test-requirements.txt | 1 + tests/test_admin.py | 110 +++++++++++++++++++++++++++++++++++++++++- tests/test_models.py | 25 ++++++++-- 3 files changed, 131 insertions(+), 5 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index 4b91698..c167e4a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,3 +4,4 @@ pytest-django freezegun django-webtest responses +pyquery diff --git a/tests/test_admin.py b/tests/test_admin.py index f115f4d..fc74b2e 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -1,5 +1,111 @@ # ANTS-Hub - Copyright (C) Entr'ouvert +import pytest +from django.core.management import call_command +from django.db import transaction -def test_dummy(): - pass + +def table_text(table): + content = [] + for row in table.find('tr').items(): + content.append([cell.text() for cell in (row.find('td, th')).items()]) + return content + + +@pytest.fixture(autouse=True, scope='module') +def setup(django_db_blocker): + with django_db_blocker.unblock(): + with transaction.atomic(): + call_command('loaddata', 'fixtures/admin.json') + call_command('loaddata', 'fixtures/example1.json') + with django_db_blocker.block(): + yield + transaction.set_rollback(True) + + +@pytest.fixture(autouse=True) +def freezer(freezer): + freezer.move_to('2023-04-12T12:00:00+02:00') + return freezer + + +def test_login(django_app, db): + resp = django_app.get('/').maybe_follow() + resp.form.set('username', 'admin') + resp.form.set('password', 'admin') + resp = resp.form.submit() + + +class TestLogged: + @pytest.fixture + def app(self, django_app, db): + test_login(django_app, db) + return django_app + + def test_create_raccordement(self, app): + resp = app.get('/').maybe_follow() + resp = resp.click('Ajouter', href='raccordement') + resp.form.set('name', 'Plateforme Y') + resp.form.set('apikey', 'a' * 32) + resp.form.set('notes', 'Penser à facturer.') + resp = resp.form.submit('_save').follow() + assert table_text(resp.pyquery('#result_list')) == [ + ['', 'Nom', 'API key', 'Création', 'Dernière mise à jour'], + ['', 'Plateforme X', '01a5…', '3 avril 2023 12:14', '3 avril 2023 19:57'], + ['', 'Plateforme Y', 'aaaa…', '12 avril 2023 12:00', '12 avril 2023 12:00'], + ] + + def test_collectivite(self, app): + resp = app.get('/').maybe_follow() + resp = resp.click('Collectivités') + assert table_text(resp.pyquery('#result_list')) == [ + [ + '', + 'Raccordement', + 'Nom', + 'URL du portail', + 'Identifiant de la collectivité à la source', + 'Création', + 'Dernière mise à jour', + ], + [ + '', + 'Plateforme X', + 'Saint-Didier', + 'https://saint-didier.fr/', + 'saint-didier', + '3 avril 2023 12:14', + '3 avril 2023 12:14', + ], + ] + + def test_lieux(self, app): + resp = app.get('/').maybe_follow() + resp = resp.click('Lieux') + assert table_text(resp.pyquery('#result_list')) == [ + ['', 'Collectivité', 'Nom', 'Création', 'Dernière mise à jour'], + ['', 'Saint-Didier', 'Mairie', '3 avril 2023 12:15', '3 avril 2023 12:15'], + ] + + def test_plages(self, app): + resp = app.get('/').maybe_follow() + resp = resp.click('Lieux') + assert table_text(resp.pyquery('#result_list')) == [ + ['', 'Collectivité', 'Nom', 'Création', 'Dernière mise à jour'], + ['', 'Saint-Didier', 'Mairie', '3 avril 2023 12:15', '3 avril 2023 12:15'], + ] + + def test_rdvs(self, app): + resp = app.get('/').maybe_follow() + resp = resp.click('Rendez-vous') + assert table_text(resp.pyquery('#result_list')) == [ + ['', '1\nDate', 'Lieu', '2\nIdentifiant de prédemande', 'Création', 'Dernière mise à jour'], + [ + '', + '3 avril 2023 12:15', + 'Mairie / 2 rue du four / Saint-Didier', + 'abcd', + '3 avril 2023 12:16', + '3 avril 2023 12:16', + ], + ] diff --git a/tests/test_models.py b/tests/test_models.py index a96e7a9..406d968 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,11 +1,22 @@ # ANTS-Hub - Copyright (C) Entr'ouvert -from ants_hub.data.models import Raccordement, TypeDeRdv +import pytest +from django.core.exceptions import ValidationError + +from ants_hub.data.models import Config, Raccordement, TypeDeRdv def test_raccordement(db): - apikey = Raccordement.objects.create(name='Example') - assert len(apikey.apikey) >= 32 + raccordement = Raccordement.objects.create(name='Example') + assert len(raccordement.apikey) >= 32 + old_apikey = raccordement.apikey + raccordement.apikey = 'NEW' + raccordement.clean() + assert len(raccordement.apikey) >= 32 and raccordement.apikey != old_apikey + + raccordement.apikey = 'abcd' + with pytest.raises(ValidationError): + raccordement.clean() def test_type_de_rdv(): @@ -20,3 +31,11 @@ def test_type_de_rdv(): assert TypeDeRdv.from_ants_name('CNI') == TypeDeRdv.CNI assert TypeDeRdv.from_ants_name('PASSPORT') == TypeDeRdv.PASSPORT assert TypeDeRdv.from_ants_name('CNI-PASSPORT') == TypeDeRdv.CNI_PASSPORT + + +def test_config(db): + Config.set(Config.REQUEST_FROM_ANTS_AUTH_TOKEN, 'xyz') + assert Config.get(Config.REQUEST_FROM_ANTS_AUTH_TOKEN) == 'xyz' + with pytest.raises(ValueError): + Config.set('unknown', '1') + assert 'authentification' in str(Config.objects.get())