Skip to content

Commit

Permalink
add RemoteProject uniqueness validation (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jun 17, 2024
1 parent 4e3c840 commit 7f91013
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Changed
- Move project app link logic in ``AppLinkContent`` (#1380)
- Move user dropdown link logic in ``AppLinkContent`` (#1381, #1413)
- Do not recreate ``AppSetting`` objects on remote sync update (#1409)
- Enforce project and site uniqueness in ``RemoteProject`` model (#1433)
- **Sodarcache**
- Rewrite REST API views (#498, #1389)
- Raise ``update_cache()`` exception for ``synccache`` in debug mode (#1375)
Expand Down
12 changes: 12 additions & 0 deletions projectroles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
)
CAT_PUBLIC_ACCESS_MSG = 'Public guest access is not allowed for categories'
ADD_EMAIL_ALREADY_SET_MSG = 'Email already set as {email_type} email for user'
REMOTE_PROJECT_UNIQUE_MSG = (
'RemoteProject with the same project UUID and site anready exists'
)


# Project ----------------------------------------------------------------------
Expand Down Expand Up @@ -1261,6 +1264,15 @@ class RemoteProject(models.Model):
class Meta:
ordering = ['site__name', 'project_uuid']

def save(self, *args, **kwargs):
# NOTE: Can't use unique constraint with foreign key relation
rp = self.__class__.objects.filter(
project_uuid=self.project_uuid, site=self.site
).first()
if rp and rp.id != self.id:
raise ValidationError(REMOTE_PROJECT_UNIQUE_MSG)
super().save(*args, **kwargs)

def __str__(self):
return '{}: {} ({})'.format(
self.site.name, str(self.project_uuid), self.site.mode
Expand Down
10 changes: 10 additions & 0 deletions projectroles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,16 @@ def test_get_project_no_foreign_key(self):
self.remote_project.save()
self.assertEqual(self.remote_project.get_project(), self.project)

def test_create_duplicate(self):
"""Test creating duplicate remote project for site (should fail)"""
with self.assertRaises(ValidationError):
self.make_remote_project(
project_uuid=self.project.sodar_uuid,
site=self.site,
level=SODAR_CONSTANTS['REMOTE_LEVEL_READ_ROLES'],
project=self.project,
)


class TestSODARUser(TestCase):
"""Tests for SODARUser"""
Expand Down

0 comments on commit 7f91013

Please sign in to comment.