Skip to content

Commit 235b7eb

Browse files
authored
Merge pull request #80 from mapswipe/feature/validation-team-on-project
Add validation check for team on project
2 parents 6206d6b + d624848 commit 235b7eb

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

apps/project/serializers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from rest_framework import serializers
77

88
from apps.common.serializers import ArchivableResourceSerializer, UserResourceSerializer
9+
from apps.contributor.models import ContributorTeam
910
from apps.project.firebase import push_organization_to_firebase
1011
from apps.tutorial.models import Tutorial
1112
from project_types.store import get_project_property
@@ -61,6 +62,13 @@ class Meta: # type: ignore[reportIncompatibleVariableOverride]
6162
"team",
6263
)
6364

65+
def validate_team(self, team: ContributorTeam | None) -> ContributorTeam | None:
66+
if team and team.is_archived:
67+
raise serializers.ValidationError(
68+
gettext("Cannot use archived team on a project."),
69+
)
70+
return team
71+
6472

6573
# NOTE: Make sure this matches with the strawberry Input ./graphql/inputs.py
6674
class ProjectUpdateSerializer(UserResourceSerializer[Project]):
@@ -90,6 +98,13 @@ class Meta: # type: ignore[reportIncompatibleVariableOverride]
9098
"team",
9199
)
92100

101+
def validate_team(self, team: ContributorTeam | None) -> ContributorTeam | None:
102+
if team and team.is_archived:
103+
raise serializers.ValidationError(
104+
gettext("Cannot use archived team on a project."),
105+
)
106+
return team
107+
93108
def validate_status(self, new_status: Project.Status | int) -> Project.Status:
94109
assert self.instance is not None, "Project does not exist."
95110

@@ -246,6 +261,13 @@ class Meta: # type: ignore[reportIncompatibleVariableOverride]
246261
"team",
247262
)
248263

264+
def validate_team(self, team: ContributorTeam | None) -> ContributorTeam | None:
265+
if team and team.is_archived:
266+
raise serializers.ValidationError(
267+
gettext("Cannot use archived team on a project."),
268+
)
269+
return team
270+
249271
def validate_status(self, new_status: Project.Status | int):
250272
assert self.instance is not None, "Project does not exist."
251273

apps/project/tests/mutation_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.core.files.temp import NamedTemporaryFile
77
from ulid import ULID
88

9+
from apps.contributor.factories import ContributorTeamFactory
910
from apps.project.factories import OrganizationFactory, ProjectFactory
1011
from apps.project.models import (
1112
Project,
@@ -496,6 +497,16 @@ def test_project_create(self):
496497
response = content["data"]["createProject"]
497498
assert response["errors"] is not None, content
498499

500+
# Creating project with archived team
501+
# Fails as team is archived
502+
archived_team = ContributorTeamFactory.create(
503+
**self.user_resource_kwargs,
504+
is_archived=True,
505+
)
506+
project_data["team"] = archived_team.pk
507+
response = content["data"]["createProject"]
508+
assert response["errors"] is not None, content
509+
499510
latest_project = Project.objects.get(pk=resp_data["result"]["id"])
500511
assert latest_project.created_by_id == self.user.pk
501512
assert latest_project.modified_by_id == self.user.pk
@@ -613,6 +624,16 @@ def test_project_update(self, mock_requests):
613624
),
614625
), content
615626

627+
# Updating project with archived team
628+
# fails as team is archived
629+
archived_team = ContributorTeamFactory.create(
630+
**self.user_resource_kwargs,
631+
is_archived=True,
632+
)
633+
project_data["team"] = archived_team.pk
634+
content = self._update_project_mutation(str(latest_project.pk), project_data)
635+
assert content["data"]["updateProject"]["errors"] is not None, content
636+
616637
# Updating project with archived Organization
617638
# Fails as organization is archived
618639
archived_organization = OrganizationFactory.create(
@@ -819,6 +840,16 @@ def test_project_update(self, mock_requests):
819840
assert content["data"]["updateProcessedProject"]["errors"] is None, content
820841
assert content["data"]["updateProcessedProject"]["result"]["status"] == self.genum(ProjectStatusEnum.ARCHIVED)
821842

843+
# Updating project with archived team
844+
# fails as team is archived
845+
archived_team = ContributorTeamFactory.create(
846+
**self.user_resource_kwargs,
847+
is_archived=True,
848+
)
849+
project_data["team"] = archived_team.pk
850+
content = self._update_processed_project_mutation(str(latest_project.pk), project_data)
851+
assert content["data"]["updateProcessedProject"]["errors"] is not None, content
852+
822853

823854
class TestProjectTypeMutation(TestCase):
824855
@typing.override

0 commit comments

Comments
 (0)