Browse Source

feat(tests): ajoute des test sur un vote de base

pull/43/head
François Poulain 11 months ago
committed by Cliss XXI - dev
parent
commit
04cb87f187
  1. 3
      Makefile
  2. 63
      gvot/base/tests/conftest.py
  3. 163
      gvot/base/tests/test_views.py

3
Makefile

@ -142,6 +142,9 @@ serve: ### démarre un serveur local pour l'application
test: ### lance les tests de l'application
$(PYTHON) -m pytest --cov --cov-report=term:skip-covered
devtest: ### lance les tests échoués de l'application
$(PYTHON) -m pytest --last-failed -s
cov: test ### vérifie la couverture de code
$(PYTHON) -m coverage html
@echo open htmlcov/index.html

63
gvot/base/tests/conftest.py

@ -1,7 +1,14 @@
import datetime
from django.contrib.auth.models import User
import pytest
from wagtail.core.models import Site
from wagtail.documents.models import get_document_model
from wagtail.images.tests.utils import Image, get_test_image_file
from ..models import Scrutin, ScrutinIndex
@pytest.fixture
def document(django_db_setup, django_db_blocker):
@ -13,3 +20,59 @@ def document(django_db_setup, django_db_blocker):
@pytest.fixture
def image(django_db_setup, django_db_blocker):
return Image.objects.create(title="Test image", file=get_test_image_file())
@pytest.fixture
def admin(django_db_setup, django_db_blocker):
return User.objects.get_or_create(
username="admin",
defaults={
'first_name': 'Super',
'last_name': 'Admin',
'email': 'admin@amin.admin',
'is_staff': True,
},
)[0]
@pytest.fixture
def liste_scrutin(admin):
instance = ScrutinIndex(
live=False, owner=admin, title="Liste des scrutins"
)
site = Site.objects.get(is_default_site=True)
site.root_page.add_child(instance=instance)
instance.save_revision(user=admin)
instance.revisions.last().publish()
return instance
@pytest.fixture
def scrutin(admin, liste_scrutin):
instance = Scrutin(
live=False,
owner=admin,
title="Assemblée générale !",
confirmation="Merci de votre participation.",
peremption=datetime.date.today() + datetime.timedelta(days=30),
ouvert=True,
)
liste_scrutin.add_child(instance=instance)
instance.form_fields.create(
label="Approbation du rapport",
required=True,
choices='Oui,Non',
default_value='Oui',
help_text='Vous devez choisir « <em>Oui</em> » ou « <em>Non</em> ».',
field_type='radio',
)
instance.save_revision(user=admin)
instance.revisions.last().publish()
return instance
@pytest.fixture
def pouvoir(scrutin):
return scrutin.pouvoir_set.create(
prenom="Jean", nom="Bon", courriel="jean@bon.fr"
)

163
gvot/base/tests/test_views.py

@ -59,3 +59,166 @@ class TestSitePage:
assert count_text_in_content(response, "Titre de la page")
assert SitePage.objects.count() == 1
@pytest.mark.django_db
class TestScrutinIndex:
def test_get_should_fail(self, client, liste_scrutin):
url = liste_scrutin.full_url
response = client.get(url)
assert response.status_code == 404
@pytest.mark.django_db
class TestScrutin:
def test_get_should_fail(self, client, scrutin):
url = scrutin.full_url
response = client.get(url)
assert response.status_code == 404
@pytest.mark.django_db
class TestPouvoir:
def test_get_closed_scrutin(self, admin, client, pouvoir):
pouvoir.scrutin.ouvert = False
pouvoir.scrutin.save_revision(user=admin)
pouvoir.scrutin.revisions.last().publish()
url = '/{}'.format(pouvoir.uuid)
response = client.get(url, follow=True)
assert response.status_code == 200
assert count_text_in_content(response, "Assemblée générale !")
assert count_text_in_content(response, "Approbation du rapport")
assert count_text_in_content(
response, "<em>Oui</em> » ou « <em>Non</em>"
)
assert count_text_in_content(
response,
'value="Oui" class="form-control" required '
'id="id_approbation-du-rapport_0" checked>',
)
assert count_text_in_content(
response,
'value="Non" class="form-control" required '
'id="id_approbation-du-rapport_1">',
)
assert count_text_in_content(
response, "Ce scrutin n'est pas encore ouvert."
)
def test_post_closed_scrutin(self, admin, client, pouvoir, mailoutbox):
pouvoir.scrutin.ouvert = False
pouvoir.scrutin.save_revision(user=admin)
pouvoir.scrutin.revisions.last().publish()
url = '/{}'.format(pouvoir.uuid)
response = client.get(url, follow=True)
assert response.status_code == 200
url = response.redirect_chain[-1][0]
response = client.post(url, data={'approbation-du-rapport': 'Non'})
assert response.status_code == 200
assert count_text_in_content(
response, "Ce scrutin n'est pas encore ouvert."
)
assert count_text_in_content(response, "Assemblée générale !")
assert not count_text_in_content(response, "Approbation du rapport")
assert count_text_in_content(response, "Merci de votre participation.")
assert count_text_in_content(response, "Retour à l'accueil")
# ... et on a n'a pas de courriel.
assert len(mailoutbox) == 0
# Après le vote
response = client.get(url, follow=True)
assert response.status_code == 200
assert count_text_in_content(response, "Assemblée générale !")
assert count_text_in_content(response, "Approbation du rapport")
assert count_text_in_content(
response, "<em>Oui</em> » ou « <em>Non</em>"
)
assert count_text_in_content(
response,
'value="Oui" class="form-control" required '
'id="id_approbation-du-rapport_0" checked>',
)
assert count_text_in_content(
response,
'value="Non" class="form-control" required '
'id="id_approbation-du-rapport_1">',
)
assert count_text_in_content(
response, "Ce scrutin n'est pas encore ouvert."
)
assert not count_text_in_content(
response, "Merci de votre participation."
)
assert not count_text_in_content(response, "Retour à l'accueil")
def test_get_scrutin(self, client, pouvoir):
url = '/{}'.format(pouvoir.uuid)
response = client.get(url, follow=True)
assert response.status_code == 200
assert count_text_in_content(response, "Assemblée générale !")
assert count_text_in_content(response, "Approbation du rapport")
assert count_text_in_content(
response, "<em>Oui</em> » ou « <em>Non</em>"
)
assert count_text_in_content(
response,
'value="Oui" class="form-control" required '
'id="id_approbation-du-rapport_0" checked>',
)
assert count_text_in_content(
response,
'value="Non" class="form-control" required '
'id="id_approbation-du-rapport_1">',
)
def test_post_scrutin(self, client, pouvoir, mailoutbox):
url = '/{}'.format(pouvoir.uuid)
response = client.get(url, follow=True)
assert response.status_code == 200
url = response.redirect_chain[-1][0]
response = client.post(url, data={'approbation-du-rapport': 'Non'})
assert response.status_code == 200
assert count_text_in_content(response, "Assemblée générale !")
assert not count_text_in_content(response, "Approbation du rapport")
assert count_text_in_content(response, "Merci de votre participation.")
assert count_text_in_content(response, "Retour à l'accueil")
# ... et on a un courriel.
assert len(mailoutbox) == 1
assert (
"Confirmation de votre participation au scrutin « {} »".format(
pouvoir.scrutin.title
)
== mailoutbox[0].subject
)
assert (
"Nous avons bien enregistré votre vote pour le scrutin"
in mailoutbox[0].body
)
# Après le vote
response = client.get(url, follow=True)
assert response.status_code == 200
assert count_text_in_content(
response, "Il semble que vous ayez déjà voté."
)
assert count_text_in_content(response, "Assemblée générale !")
assert count_text_in_content(response, "Approbation du rapport")
assert count_text_in_content(
response, "<em>Oui</em> » ou « <em>Non</em>"
)
assert count_text_in_content(
response,
'value="Oui" class="form-control" required '
'id="id_approbation-du-rapport_0">',
)
assert count_text_in_content(
response,
'value="Non" class="form-control" required '
'id="id_approbation-du-rapport_1" checked>',
)

Loading…
Cancel
Save
Map all the world