Skip to content

Commit

Permalink
test: fix API tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michalc committed Feb 11, 2025
1 parent 322a79f commit 11c48fe
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
1 change: 1 addition & 0 deletions django_app/redbox_app/redbox_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 4 additions & 13 deletions django_app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,15 @@ 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,
business_unit=User.BusinessUnit.DIGITAL_DATA_AND_TECHNOLOGY,
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,
Expand All @@ -98,9 +95,7 @@ def _create_user(

@pytest.fixture()
def alice(create_user):
return create_user(
email="[email protected]", date_joined_iso="2000-01-01", username="[email protected]"
)
return create_user(email="[email protected]", username="[email protected]")


@pytest.fixture()
Expand All @@ -110,7 +105,7 @@ def chat_with_alice(alice):

@pytest.fixture()
def bob(create_user):
return create_user(email="[email protected]", date_joined_iso="2000-01-01", username="[email protected]")
return create_user(email="[email protected]", username="[email protected]")


@pytest.fixture()
Expand All @@ -135,16 +130,12 @@ def user_with_demographic_data() -> User:

@pytest.fixture()
def staff_user(create_user):
return create_user(
email="[email protected]", date_joined_iso="2000-01-01", is_staff=True, username="[email protected]"
)
return create_user(email="[email protected]", is_staff=True, username="[email protected]")


@pytest.fixture()
def superuser() -> User:
return User.objects.create_superuser(
email="[email protected]", date_joined_iso="2000-01-01", username="[email protected]"
)
return User.objects.create_superuser(email="[email protected]", username="[email protected]")


@pytest.fixture()
Expand Down
6 changes: 3 additions & 3 deletions django_app/tests/views/test_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"}

0 comments on commit 11c48fe

Please sign in to comment.