Skip to content

Commit de56b9c

Browse files
committed
tests
1 parent b580552 commit de56b9c

File tree

5 files changed

+113
-18
lines changed

5 files changed

+113
-18
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_tags.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ async def create_or_update_access_rights(
287287
write: bool,
288288
delete: bool,
289289
) -> TagAccessRightsDict:
290-
290+
# NOTE that there is no reference to the caller which implies
291+
# that we assume that the rigths to create or update these access_rights
292+
# have been checked (via has_access_rights) before calling this function
291293
async with transaction_context(self.engine, connection) as conn:
292294
result = await conn.execute(
293295
upsert_tags_access_rights_stmt(
@@ -316,6 +318,9 @@ async def delete_access_rights(
316318
tag_id: int,
317319
group_id: int,
318320
) -> bool:
321+
# NOTE that there is no reference to the caller which implies
322+
# that we assume that the rigths to delete these access_rights
323+
# have been checked (via has_access_rights) before calling this function
319324
async with transaction_context(self.engine, connection) as conn:
320325
deleted: bool = await conn.scalar(
321326
delete_tag_access_rights_stmt(tag_id=tag_id, group_id=group_id)

packages/postgres-database/tests/test_utils_tags.py

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,6 @@ async def test_tags_repo_create(
630630
name="T1",
631631
description="my first tag",
632632
color="pink",
633-
read=True,
634-
write=True,
635-
delete=True,
636633
)
637634
assert tag_1 == {
638635
"id": 1,
@@ -654,6 +651,108 @@ async def test_tags_repo_create(
654651
== user.primary_gid
655652
)
656653

654+
# Checks defaults to full ownership
655+
assert await tags_repo.has_access_rights(
656+
caller_id=user.id,
657+
tag_id=tag_1["id"],
658+
read=True,
659+
write=True,
660+
delete=True,
661+
)
662+
663+
664+
async def test_tags_repo_access_rights(
665+
asyncpg_engine: AsyncEngine,
666+
user: RowProxy,
667+
group: RowProxy,
668+
other_user: RowProxy,
669+
):
670+
tags_repo = TagsRepo(asyncpg_engine)
671+
tag = await tags_repo.create(
672+
user_id=user.id,
673+
name="T1",
674+
description="my first tag",
675+
color="pink",
676+
)
677+
678+
# check ownership
679+
tag_accesses = await tags_repo.list_access_rights(tag_id=tag["id"])
680+
assert len(tag_accesses) == 1
681+
user_access = tag_accesses[0]
682+
assert user_access == {
683+
"group_id": user.primary_gid,
684+
"tag_id": tag["id"],
685+
"read": True,
686+
"write": True,
687+
"delete": True,
688+
}
689+
690+
assert await tags_repo.has_access_rights(
691+
caller_id=user.id,
692+
tag_id=tag["id"],
693+
read=True,
694+
write=True,
695+
delete=True,
696+
)
697+
698+
# CREATE access for other_user
699+
other_user_access = await tags_repo.create_or_update_access_rights(
700+
tag_id=tag["id"],
701+
group_id=other_user.primary_gid,
702+
read=True,
703+
write=False,
704+
delete=False,
705+
)
706+
707+
assert not await tags_repo.has_access_rights(
708+
caller_id=other_user.id,
709+
tag_id=tag["id"],
710+
read=user_access["read"],
711+
write=user_access["write"],
712+
delete=user_access["delete"],
713+
)
714+
715+
assert await tags_repo.has_access_rights(
716+
caller_id=other_user.id,
717+
tag_id=tag["id"],
718+
read=other_user_access["read"],
719+
write=other_user_access["write"],
720+
delete=other_user_access["delete"],
721+
)
722+
723+
tag_accesses = await tags_repo.list_access_rights(tag_id=tag["id"])
724+
assert len(tag_accesses) == 2
725+
726+
# UPDATE access
727+
updated_access = await tags_repo.create_or_update_access_rights(
728+
tag_id=tag["id"],
729+
group_id=other_user.primary_gid,
730+
read=False, # <--
731+
write=False,
732+
delete=False,
733+
)
734+
assert updated_access != other_user_access
735+
736+
# checks partial
737+
assert await tags_repo.has_access_rights(
738+
caller_id=other_user.id,
739+
tag_id=tag["id"],
740+
read=False,
741+
)
742+
743+
assert not await tags_repo.has_access_rights(
744+
caller_id=other_user.id, tag_id=tag["id"], write=True
745+
)
746+
747+
# DELETE access to other-user
748+
await tags_repo.delete_access_rights(
749+
tag_id=tag["id"],
750+
group_id=other_user.primary_gid,
751+
)
752+
753+
tag_accesses = await tags_repo.list_access_rights(tag_id=tag["id"])
754+
assert len(tag_accesses) == 1
755+
657756

658757
def test_building_tags_sql_statements():
659758
def _check(func_smt, **kwargs):

services/web/server/src/simcore_service_webserver/api/v0/openapi.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14069,22 +14069,12 @@ components:
1406914069
delete:
1407014070
type: boolean
1407114071
title: Delete
14072-
created:
14073-
type: string
14074-
format: date-time
14075-
title: Created
14076-
modified:
14077-
type: string
14078-
format: date-time
14079-
title: Modified
1408014072
type: object
1408114073
required:
1408214074
- gid
1408314075
- read
1408414076
- write
1408514077
- delete
14086-
- created
14087-
- modified
1408814078
title: TagGroupGet
1408914079
TagUpdate:
1409014080
properties:

services/web/server/src/simcore_service_webserver/tags/_service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
async def create_tag(
2121
app: web.Application, user_id: UserID, new_tag: TagCreate
2222
) -> TagGet:
23+
"""Creates tag and user_id takes ownership"""
2324
engine: AsyncEngine = get_async_engine(app)
2425

2526
repo = TagsRepo(engine)
@@ -30,7 +31,7 @@ async def create_tag(
3031
delete=True,
3132
**new_tag.model_dump(exclude_unset=True),
3233
)
33-
return TagGet.from_db(tag)
34+
return TagGet.from_model(tag)
3435

3536

3637
async def list_tags(
@@ -40,7 +41,7 @@ async def list_tags(
4041
engine: AsyncEngine = get_async_engine(app)
4142
repo = TagsRepo(engine)
4243
tags = await repo.list_all(user_id=user_id)
43-
return [TagGet.from_db(t) for t in tags]
44+
return [TagGet.from_model(t) for t in tags]
4445

4546

4647
async def update_tag(
@@ -54,7 +55,7 @@ async def update_tag(
5455
tag_id=tag_id,
5556
**tag_updates.model_dump(exclude_unset=True),
5657
)
57-
return TagGet.from_db(tag)
58+
return TagGet.from_model(tag)
5859

5960

6061
async def delete_tag(app: web.Application, user_id: UserID, tag_id: IdInt):

services/web/server/src/simcore_service_webserver/tags/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class TagGet(OutputSchema):
5555
access_rights: TagAccessRights = Field(..., alias="accessRights")
5656

5757
@classmethod
58-
def from_db(cls, tag: TagDict) -> "TagGet":
58+
def from_model(cls, tag: TagDict) -> Self:
5959
# NOTE: cls(access_rights=tag, **tag) would also work because of Config
6060
return cls(
6161
id=tag["id"],

0 commit comments

Comments
 (0)