Skip to content

Commit

Permalink
fix: [AAP-23882] Add unique together constraint for organization and …
Browse files Browse the repository at this point in the history
…team name
  • Loading branch information
Dostonbek1 committed Jan 17, 2025
1 parent b5d1035 commit 349a577
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/aap_eda/api/serializers/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from aap_eda.api.serializers.organization import OrganizationRefSerializer
from aap_eda.core import models, validators
Expand Down Expand Up @@ -56,6 +57,14 @@ class Meta:
"description",
"organization_id",
]
validators = [
UniqueTogetherValidator(
queryset=models.Team.objects.all(),
fields=["organization_id", "name"],
message="Team with this name already exists in "
"the organization.",
)
]

def validate(self, data):
self.validate_shared_resource()
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/api/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ def test_create_team_forbidden(
assert response.status_code == status.HTTP_403_FORBIDDEN


@pytest.mark.django_db
def test_create_team_unique_name_constraint(
use_shared_resource_setting,
default_organization: models.Organization,
default_team: models.Team,
admin_client: APIClient,
):
data_in = {
"name": default_team.name,
"description": "Test Team",
"organization_id": default_organization.id,
}
response = admin_client.post(f"{api_url_v1}/teams/", data=data_in)
assert response.status_code == status.HTTP_400_BAD_REQUEST


@pytest.mark.django_db
def test_retrieve_team(default_team: models.Team, admin_client: APIClient):
response = admin_client.get(f"{api_url_v1}/teams/{default_team.id}/")
Expand Down

0 comments on commit 349a577

Please sign in to comment.