From 3e183739c59cc985ad87e4675a4316dc56b5e0a4 Mon Sep 17 00:00:00 2001 From: Oksamies Date: Mon, 11 Dec 2023 15:10:46 +0200 Subject: [PATCH] Add tests for TeamCreateAPIView --- .../api/cyberstorm/tests/test_team.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/django/thunderstore/api/cyberstorm/tests/test_team.py b/django/thunderstore/api/cyberstorm/tests/test_team.py index 0c84b4670..7c4c94b0a 100644 --- a/django/thunderstore/api/cyberstorm/tests/test_team.py +++ b/django/thunderstore/api/cyberstorm/tests/test_team.py @@ -1,3 +1,5 @@ +import json + import pytest from django.contrib.auth import get_user_model from rest_framework.test import APIClient @@ -223,3 +225,45 @@ 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_create__when_creating_a_team__succeeds( + api_client: APIClient, + user: UserType, +): + api_client.force_authenticate(user) + + response = api_client.post( + "/api/cyberstorm/teams/create/", + json.dumps({"name": "CoolestTeamNameEver"}), + content_type="application/json", + ) + + assert response.status_code == 200 + response_json = response.json() + assert response_json["name"] == "CoolestTeamNameEver" + assert len( + Team.objects.get(name="CoolestTeamNameEver").members.filter( + user__username=user.username + ) + ) + + +@pytest.mark.django_db +def test_team_create__when_creating_a_team__fails_because_team_with_provided_name_exists( + api_client: APIClient, + user: UserType, + team: Team, +): + api_client.force_authenticate(user) + + response = api_client.post( + "/api/cyberstorm/teams/create/", + json.dumps({"name": team.name}), + content_type="application/json", + ) + + assert response.status_code == 400 + response_json = response.json() + assert "A team with the provided name already exists" in response_json["name"]