Skip to content

Commit

Permalink
(surveys): add factory and test
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Dec 19, 2023
1 parent e2611e9 commit 5862910
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lacommunaute/surveys/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import factory

from lacommunaute.surveys.models import DSP
from lacommunaute.users.factories import UserFactory


class DSPFactory(factory.django.DjangoModelFactory):
class Meta:
model = DSP

user = factory.SubFactory(UserFactory)
recommendations = {
"Recommandations": "Parcours SIAE",
}
Empty file.
85 changes: 85 additions & 0 deletions lacommunaute/surveys/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import pytest # noqa
from django.urls import reverse
from pytest_django.asserts import assertContains, assertNotContains

from lacommunaute.surveys.factories import DSPFactory
from lacommunaute.surveys.models import DSP
from lacommunaute.users.factories import UserFactory


dsp_choices_list = [
"work_capacity",
"language_skills",
"housing",
"rights_access",
"mobility",
"resources",
"judicial",
"availability",
]


class TestDSPCreateView:
def test_login_required(self, db, client):
url = reverse("surveys:dsp_create")
response = client.get(url)
assert response.status_code == 302

def test_form_fields(self, db, client):
url = reverse("surveys:dsp_create")
client.force_login(UserFactory())
response = client.get(url)
assert response.status_code == 200
assert "form" in response.context
for field in dsp_choices_list:
assert field in response.context["form"].fields

def test_form_valid(self, db, client):
url = reverse("surveys:dsp_create")
client.force_login(UserFactory())
choices = {key: "0" for key in dsp_choices_list}
response = client.post(url, choices)
assert response.status_code == 302

dsp = DSP.objects.get()
assert response.url == reverse("surveys:dsp_detail", kwargs={"pk": dsp.pk})
assert dsp.recommendations is not None


class TestDSPDetailView:
def test_login_required(self, db, client):
url = reverse("surveys:dsp_detail", kwargs={"pk": 1})
response = client.get(url)
assert response.status_code == 302

def test_user_cannot_view_others_surveys(self, db, client):
dsp = DSPFactory()
client.force_login(UserFactory())
url = reverse("surveys:dsp_detail", kwargs={"pk": dsp.pk})
response = client.get(url)
assert response.status_code == 404

def test_user_can_view_own_survey(self, db, client):
dsp = DSPFactory()
client.force_login(dsp.user)
url = reverse("surveys:dsp_detail", kwargs={"pk": dsp.pk})
response = client.get(url)
assert response.status_code == 200
assert "form" in response.context
assert "recommendations" in response.context


class TestHomeView:
def test_link_to_dsp(self, db, client):
user = UserFactory()
client.force_login(user)
url = reverse("pages:home")
response = client.get(url)
assert response.status_code == 200
assertNotContains(response, reverse("surveys:dsp_create"))

user.is_superuser = True
user.save()
response = client.get(url)
assert response.status_code == 200
assertContains(response, reverse("surveys:dsp_create"))

0 comments on commit 5862910

Please sign in to comment.