Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for RemoveTeamMemberAPIView #944

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion django/thunderstore/api/cyberstorm/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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 @@ -183,6 +183,89 @@ def test_team_member_list_api_view__for_member__sorts_results(
assert result[4]["username"] == erin.username


@pytest.mark.django_db
def test_team_members_remove__when_removing_member__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="member")

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

assert response.status_code == 200
response_json = response.json()
assert response_json["team_name"] == team.name
assert response_json["username"] == just_a_member.user.username


@pytest.mark.django_db
def test_team_members_remove__when_removing_member__fails_because_user_is_not_a_member_in_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/remove/",
json.dumps({"username": member_in_another_team.user.username}),
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_remove__when_removing_member__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/remove/",
json.dumps({"username": user.username}),
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_remove__when_removing_member__fails_because_user_is_not_authenticated(
api_client: APIClient,
user: UserType,
team: Team,
):
TeamMemberFactory(team=team, user=user, role="owner")
just_a_member = TeamMemberFactory(team=team, role="member")

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

assert response.status_code == 401
response_json = response.json()
assert response_json["detail"] == "Authentication credentials were not provided."
assert TeamMember.objects.filter(team=team, user=user).count() == 1


@pytest.mark.django_db
def test_team_service_account_list_api_view__for_member__returns_only_service_accounts(
api_client: APIClient,
Expand Down
Loading