From 11c48fe5640bfddedd661c40a97c0be322d18668 Mon Sep 17 00:00:00 2001 From: Michal Charemza Date: Tue, 11 Feb 2025 13:45:52 +0000 Subject: [PATCH] test: fix API tests There are some broken tests from some legacy work. This fixes just some of them: the ones in test_api_views.py There were 4 issues causing the tests to fail that this change fixes: - An incorrect assumption on the form of an error message in the case of a 403 - Using a now-removed date_joined field - Passing an email address into model creation, where is also determined the email address based on username. - Assuming that a regular user can access the view-message endpoint, when they (correctly) cannot. --- django_app/redbox_app/redbox_core/models.py | 1 + django_app/tests/conftest.py | 17 ++++------------- django_app/tests/views/test_api_views.py | 6 +++--- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/django_app/redbox_app/redbox_core/models.py b/django_app/redbox_app/redbox_core/models.py index 997ac96e9..98002d058 100644 --- a/django_app/redbox_app/redbox_core/models.py +++ b/django_app/redbox_app/redbox_core/models.py @@ -194,6 +194,7 @@ def _create_user(self, username, password, **extra_fields): msg = "The given email must be set" raise ValueError(msg) # email = self.normalize_email(email) + extra_fields.pop("email", None) user = self.model(email=username, **extra_fields) user.set_password(password) user.save(using=self._db) diff --git a/django_app/tests/conftest.py b/django_app/tests/conftest.py index ef0269ae4..588e59ef6 100644 --- a/django_app/tests/conftest.py +++ b/django_app/tests/conftest.py @@ -73,7 +73,6 @@ def default_ai_settings(db): # noqa: ARG001 def create_user(): def _create_user( email, - date_joined_iso, username, is_staff=False, grade=User.UserGrade.DIRECTOR, @@ -81,10 +80,8 @@ def _create_user( profession=User.Profession.IA, ai_experience=User.AIExperienceLevel.EXPERIENCED_NAVIGATOR, ): - date_joined = datetime.fromisoformat(date_joined_iso).astimezone(UTC) return User.objects.create_user( email=email, - date_joined=date_joined, is_staff=is_staff, grade=grade, business_unit=business_unit, @@ -98,9 +95,7 @@ def _create_user( @pytest.fixture() def alice(create_user): - return create_user( - email="alice@cabinetoffice.gov.uk", date_joined_iso="2000-01-01", username="alice@cabinetoffice.gov.uk" - ) + return create_user(email="alice@cabinetoffice.gov.uk", username="alice@cabinetoffice.gov.uk") @pytest.fixture() @@ -110,7 +105,7 @@ def chat_with_alice(alice): @pytest.fixture() def bob(create_user): - return create_user(email="bob@example.com", date_joined_iso="2000-01-01", username="bob@example.com") + return create_user(email="bob@example.com", username="bob@example.com") @pytest.fixture() @@ -135,16 +130,12 @@ def user_with_demographic_data() -> User: @pytest.fixture() def staff_user(create_user): - return create_user( - email="staff@example.com", date_joined_iso="2000-01-01", is_staff=True, username="staff@example.com" - ) + return create_user(email="staff@example.com", is_staff=True, username="staff@example.com") @pytest.fixture() def superuser() -> User: - return User.objects.create_superuser( - email="super@example.com", date_joined_iso="2000-01-01", username="super@example.com" - ) + return User.objects.create_superuser(email="super@example.com", username="super@example.com") @pytest.fixture() diff --git a/django_app/tests/views/test_api_views.py b/django_app/tests/views/test_api_views.py index de87ea289..8cd411ef7 100644 --- a/django_app/tests/views/test_api_views.py +++ b/django_app/tests/views/test_api_views.py @@ -12,9 +12,9 @@ @pytest.mark.django_db() -def test_api_view(user_with_chats_with_messages_over_time: User, client: Client): +def test_api_view(superuser: User, user_with_chats_with_messages_over_time: User, client: Client): # Given - client.force_login(user_with_chats_with_messages_over_time) + client.force_login(superuser) # When url = reverse("user-view") @@ -35,4 +35,4 @@ def test_api_view_fail(client: Client): # Then assert response.status_code == HTTPStatus.FORBIDDEN - assert response.json() == {"detail": "Authentication credentials were not provided."} + assert response.json() == {"detail": "No API key provided"}