Skip to content

Commit f444a59

Browse files
committed
Add tests for TeamCreateAPIView
1 parent 4a22183 commit f444a59

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

django/thunderstore/api/cyberstorm/tests/test_team.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,63 @@ def test_team_member_add_api_view__when_adding_a_member__fails_because_user_is_n
318318
.count()
319319
== 0
320320
)
321+
322+
323+
@pytest.mark.django_db
324+
def test_team_create__when_creating_a_team__succeeds(
325+
api_client: APIClient,
326+
user: UserType,
327+
):
328+
api_client.force_authenticate(user)
329+
330+
response = api_client.post(
331+
"/api/cyberstorm/teams/create/",
332+
json.dumps({"name": "CoolestTeamNameEver"}),
333+
content_type="application/json",
334+
)
335+
336+
assert response.status_code == 200
337+
response_json = response.json()
338+
assert response_json["name"] == "CoolestTeamNameEver"
339+
assert (
340+
Team.objects.get(name="CoolestTeamNameEver")
341+
.members.filter(user__username=user.username)
342+
.count()
343+
== 1
344+
)
345+
346+
347+
@pytest.mark.django_db
348+
def test_team_create__when_creating_a_team__fails_because_user_is_not_authenticated(
349+
api_client: APIClient,
350+
user: UserType,
351+
):
352+
response = api_client.post(
353+
"/api/cyberstorm/teams/create/",
354+
json.dumps({"name": "CoolestTeamNameEver"}),
355+
content_type="application/json",
356+
)
357+
358+
assert response.status_code == 401
359+
response_json = response.json()
360+
assert response_json["detail"] == "Authentication credentials were not provided."
361+
assert Team.objects.filter(name="CoolestTeamNameEver").count() == 0
362+
363+
364+
@pytest.mark.django_db
365+
def test_team_create__when_creating_a_team__fails_because_team_with_provided_name_exists(
366+
api_client: APIClient,
367+
user: UserType,
368+
team: Team,
369+
):
370+
api_client.force_authenticate(user)
371+
372+
response = api_client.post(
373+
"/api/cyberstorm/teams/create/",
374+
json.dumps({"name": team.name}),
375+
content_type="application/json",
376+
)
377+
378+
assert response.status_code == 400
379+
response_json = response.json()
380+
assert "A team with the provided name already exists" in response_json["name"]

0 commit comments

Comments
 (0)