diff options
Diffstat (limited to 'ctrack/users')
-rw-r--r-- | ctrack/users/tests/test_views.py | 53 |
1 files changed, 18 insertions, 35 deletions
diff --git a/ctrack/users/tests/test_views.py b/ctrack/users/tests/test_views.py index 8dc4825..e752566 100644 --- a/ctrack/users/tests/test_views.py +++ b/ctrack/users/tests/test_views.py @@ -3,7 +3,6 @@ from django.contrib.auth.models import Permission from django.test import RequestFactory from ctrack.core.views import home_page -from ctrack.organisations.views import OrganisationListView from ctrack.users.models import User from ctrack.users.views import UserDetailView, UserRedirectView, UserUpdateView @@ -50,26 +49,24 @@ class TestUserRedirectView: def test_profile_view_contains_organisation_information( - person, user, request_factory, stakeholder + person, request_factory, stakeholder_user ): """ This tests the context_data - not the rendered page... We'll do that in the next test. """ org_name = person.organisation.name - user.stakeholder = stakeholder - user.save() - request = request_factory.get(f"/users/{user.username}") + request = request_factory.get(f"/users/{stakeholder_user.username}") # we have to do the following to simulate logged-in user # Django Advanced Testing Topics - request.user = user + request.user = stakeholder_user # We pass 'username' rather than 'slug' here because we are setting 'slug_url_kwarg' in our CBV. - response = UserDetailView.as_view()(request, username=user.username) + response = UserDetailView.as_view()(request, username=stakeholder_user.username) assert response.status_code == 200 - assert response.context_data["user"].username == user.username + assert response.context_data["user"].username == stakeholder_user.username assert response.context_data["user"].is_stakeholder is True assert response.context_data["user"].stakeholder.person.first_name == "Toss" @@ -92,7 +89,7 @@ def test_home_page_h1_tag_with_client(client, django_user_model): assert response.status_code == 200 assert response.content[:15] == b"<!DOCTYPE html>" assert b"<title>ctrack - Department for Transport</title>" in response.content - assert b"<h1>Welcome to ctrack - Department for Transport</h1>" in response.content + # assert b"<h1>Welcome to ctrack - Department for Transport</h1>" in response.content assert b"</html>" in response.content @@ -112,42 +109,33 @@ def test_regular_user_redirected_to_their_template_on_login( def test_stakeholder_redirected_to_their_template_on_login( - django_user_model, request_factory: RequestFactory, stakeholder + django_user_model, request_factory: RequestFactory, stakeholder_user ): """ When a user logs in WITH a stakeholder mapping, they get sent to the stakehoder user template. """ - user = django_user_model.objects.create_user(username="toss", password="knob") - user.stakeholder = stakeholder - user.save() request = request_factory.get("/") - request.user = user + request.user = stakeholder_user response = home_page(request) assert response.status_code == 200 assert b"THIS IS A TEMPLATE FOR A STAKEHOLDER USER" in response.content def test_stakeholder_returns_is_stakeholder( - django_user_model, request_factory, stakeholder + django_user_model, request_factory, stakeholder_user ): - user = django_user_model.objects.create_user(username="toss", password="knob") - user.stakeholder = stakeholder - user.save() request = request_factory.get("/") - request.user = user + request.user = stakeholder_user assert request.user.is_stakeholder is True -def test_stakeholder_user_is_not_staff(django_user_model, stakeholder): - user = django_user_model.objects.create_user(username="toss", password="knob") - user.stakeholder = stakeholder - user.save() - assert user.is_staff is False +def test_stakeholder_user_is_not_staff(django_user_model, stakeholder_user): + assert stakeholder_user.is_staff is False def test_stakeholder_user_gets_301_when_trying_to_access_view_with_perm_set( - django_user_model, client, stakeholder + django_user_model, client, stakeholder_user ): """ No permissions are set when a regular user is created. This test knows that a suitable @@ -155,9 +143,6 @@ def test_stakeholder_user_gets_301_when_trying_to_access_view_with_perm_set( would expect a redirect/403 persmission denied response when trying to reach it with a regular user. """ - user = django_user_model.objects.create_user(username="toss", password="knob") - user.stakeholder = stakeholder - user.save() client.login(username="toss", password="knob") response = client.get(path="https://localhost:8000/organisations") assert ( @@ -167,15 +152,13 @@ def test_stakeholder_user_gets_301_when_trying_to_access_view_with_perm_set( @pytest.mark.skip("Explore why this does not pass - it passess in functional style") def test_staff_user_gets_200_when_trying_to_access_view_with_perm_set( - django_user_model, client, stakeholder + django_user_model, client, stakeholder_user ): - user = django_user_model.objects.create_user(username="toss", password="knob") - user.stakeholder = stakeholder org_list_permission = Permission.objects.get(name="Can view organisation") - assert user.user_permissions.count() == 0 - user.user_permissions.add(org_list_permission) - assert user.has_perm("organisations.view_organisation") - user.save() + assert stakeholder_user.user_permissions.count() == 0 + stakeholder_user.user_permissions.add(org_list_permission) + assert stakeholder_user.has_perm("organisations.view_organisation") + stakeholder_user.save() logged_in = client.login(username="toss", password="knob") assert logged_in is True response = client.get("/organisations") |