Skip to content

Commit

Permalink
Add tests for EditTeamMemberAPIView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 12, 2023
1 parent b6bca11 commit 3c9c409
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion django/thunderstore/api/cyberstorm/tests/test_team.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json

import pytest
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient

from thunderstore.account.factories import ServiceAccountFactory
from thunderstore.core.types import UserType
from thunderstore.repository.factories import TeamFactory, TeamMemberFactory
from thunderstore.repository.models.team import Team
from thunderstore.repository.models.team import Team, TeamMember

User = get_user_model()

Expand Down Expand Up @@ -223,3 +225,69 @@ def test_team_service_accounts_api_view__for_member__sorts_results(
assert result[0]["name"] == alice.first_name
assert result[1]["name"] == bob.first_name
assert result[2]["name"] == charlie.first_name


@pytest.mark.django_db
def test_team_members_edit__when_changing_role__succeeds(
api_client: APIClient,
user: UserType,
team: Team,
):
TeamMemberFactory(team=team, user=user, role="owner")
api_client.force_authenticate(user)
just_a_member = TeamMemberFactory(team=team, role="owner")

response = api_client.post(
f"/api/cyberstorm/team/{team.name}/members/edit/",
json.dumps({"username": just_a_member.user.username, "role": "member"}),
content_type="application/json",
)

response_json = response.json()
assert response.status_code == 200
assert response_json["team_name"] == team.name
assert response_json["username"] == just_a_member.user.username
assert response_json["role"] == "member"
assert TeamMember.objects.get(pk=just_a_member.pk).role == "member"


@pytest.mark.django_db
def test_team_members_edit__when_changing_role__fails_because_user_is_not_in_the_team(
api_client: APIClient,
user: UserType,
team: Team,
):
TeamMemberFactory(team=team, user=user, role="owner")
api_client.force_authenticate(user)
another_team = TeamFactory()
member_in_another_team = TeamMemberFactory(team=another_team, role="owner")

response = api_client.post(
f"/api/cyberstorm/team/{team.name}/members/edit/",
json.dumps(
{"username": member_in_another_team.user.username, "role": "member"}
),
content_type="application/json",
)

assert response.status_code == 404
response_json = response.json()
assert response_json["detail"] == "Not found."


@pytest.mark.django_db
def test_team_members_edit__when_changing_role__fails_because_team_does_not_exist(
api_client: APIClient,
user: UserType,
):
api_client.force_authenticate(user)

response = api_client.post(
f"/api/cyberstorm/team/GhostTeam/members/edit/",
json.dumps({"username": user.username, "role": "member"}),
content_type="application/json",
)

assert response.status_code == 404
response_json = response.json()
assert response_json["detail"] == "Not found."

0 comments on commit 3c9c409

Please sign in to comment.