Skip to content

Commit

Permalink
test(permission-db): add unittests for special user and groups cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mabw-rte committed Feb 19, 2024
1 parent f311f32 commit b86bff0
Showing 1 changed file with 139 additions and 12 deletions.
151 changes: 139 additions & 12 deletions tests/study/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,16 @@ def test_get_all__study_tags_filter(
"30",
"31",
"32",
"34",
},
),
(1, ["1"], {"1", "7", "8", "9", "17", "23", "24", "25"}),
(1, ["2"], {"2", "5", "6", "7", "8", "9", "18", "21", "22", "23", "24", "25"}),
(1, ["1", "2"], {"1", "2", "5", "6", "7", "8", "9", "17", "18", "21", "22", "23", "24", "25"}),
(1, ["2"], {"2", "5", "6", "7", "8", "9", "18", "21", "22", "23", "24", "25", "34"}),
(1, ["3"], set()),
(1, ["1", "2"], {"1", "2", "5", "6", "7", "8", "9", "17", "18", "21", "22", "23", "24", "25", "34"}),
(1, ["1", "3"], {"1", "7", "8", "9", "17", "23", "24", "25"}),
(1, ["2", "3"], {"2", "5", "6", "7", "8", "9", "18", "21", "22", "23", "24", "25", "34"}),
(1, ["1", "2", "3"], {"1", "2", "5", "6", "7", "8", "9", "17", "18", "21", "22", "23", "24", "25", "34"}),
(
2,
[],
Expand Down Expand Up @@ -761,15 +766,32 @@ def test_get_all__study_tags_filter(
"30",
"31",
"32",
"33",
},
),
(2, ["1"], {"1", "3", "4", "7", "8", "9", "17", "19", "20", "23", "24", "25"}),
(2, ["1"], {"1", "3", "4", "7", "8", "9", "17", "19", "20", "23", "24", "25", "33"}),
(2, ["2"], {"5", "7", "8", "9", "21", "23", "24", "25"}),
(2, ["1", "2"], {"1", "3", "4", "5", "7", "8", "9", "17", "19", "20", "21", "23", "24", "25"}),
(2, ["3"], set()),
(2, ["1", "2"], {"1", "3", "4", "5", "7", "8", "9", "17", "19", "20", "21", "23", "24", "25", "33"}),
(2, ["1", "3"], {"1", "3", "4", "7", "8", "9", "17", "19", "20", "23", "24", "25", "33"}),
(2, ["2", "3"], {"5", "7", "8", "9", "21", "23", "24", "25"}),
(2, ["1", "2", "3"], {"1", "3", "4", "5", "7", "8", "9", "17", "19", "20", "21", "23", "24", "25", "33"}),
(3, [], {"13", "14", "15", "16", "29", "30", "31", "32", "33", "34", "35", "36"}),
(3, ["1"], {"33"}),
(3, ["2"], {"34"}),
(3, ["3"], set()),
(3, ["1", "2"], {"33", "34"}),
(3, ["1", "3"], {"33"}),
(3, ["2", "3"], {"34"}),
(3, ["1", "2", "3"], {"33", "34"}),
(None, [], set()),
(None, ["1"], set()),
(None, ["2"], set()),
(None, ["3"], set()),
(None, ["1", "2"], set()),
(None, ["1", "3"], set()),
(None, ["2", "3"], set()),
(None, ["1", "2", "3"], set()),
],
)
def test_get_all__non_admin_permissions_filter(
Expand All @@ -783,12 +805,15 @@ def test_get_all__non_admin_permissions_filter(

user_1 = User(id=1, name="user1")
user_2 = User(id=2, name="user2")
user_3 = User(id=3, name="user3")

group_1 = Group(id=1, name="group1")
group_2 = Group(id=2, name="group2")
group_3 = Group(id=3, name="group3")

user_groups_mapping = {1: [group_2.id], 2: [group_1.id]}
user_groups_mapping = {1: [group_2.id], 2: [group_1.id], 3: []}

# create variant studies for user_1 and user_2 that are part of some groups
study_1 = VariantStudy(id=1, owner=user_1, groups=[group_1])
study_2 = VariantStudy(id=2, owner=user_1, groups=[group_2])
study_3 = VariantStudy(id=3, groups=[group_1])
Expand All @@ -800,12 +825,15 @@ def test_get_all__non_admin_permissions_filter(
study_9 = VariantStudy(id=9, groups=[group_1, group_2])
study_10 = VariantStudy(id=10, owner=user_1)
study_11 = VariantStudy(id=11, owner=user_2)

# create variant studies with neither owner nor groups
study_12 = VariantStudy(id=12)
study_13 = VariantStudy(id=13, public_mode=PublicMode.READ)
study_14 = VariantStudy(id=14, public_mode=PublicMode.EDIT)
study_15 = VariantStudy(id=15, public_mode=PublicMode.EXECUTE)
study_16 = VariantStudy(id=16, public_mode=PublicMode.FULL)

# create raw studies for user_1 and user_2 that are part of some groups
study_17 = RawStudy(id=17, owner=user_1, groups=[group_1])
study_18 = RawStudy(id=18, owner=user_1, groups=[group_2])
study_19 = RawStudy(id=19, groups=[group_1])
Expand All @@ -817,13 +845,25 @@ def test_get_all__non_admin_permissions_filter(
study_25 = RawStudy(id=25, groups=[group_1, group_2])
study_26 = RawStudy(id=26, owner=user_1)
study_27 = RawStudy(id=27, owner=user_2)

# create raw studies with neither owner nor groups
study_28 = RawStudy(id=28)
study_29 = RawStudy(id=29, public_mode=PublicMode.READ)
study_30 = RawStudy(id=30, public_mode=PublicMode.EDIT)
study_31 = RawStudy(id=31, public_mode=PublicMode.EXECUTE)
study_32 = RawStudy(id=32, public_mode=PublicMode.FULL)

db_session.add_all([user_1, user_2, group_1, group_2])
# create studies for user_3 that is not part of any group
study_33 = VariantStudy(id=33, owner=user_3, groups=[group_1])
study_34 = RawStudy(id=34, owner=user_3, groups=[group_2])
study_35 = VariantStudy(id=35, owner=user_3)
study_36 = RawStudy(id=36, owner=user_3)

# create studies for group_3 that has no user
study_37 = VariantStudy(id=37, groups=[group_3])
study_38 = RawStudy(id=38, groups=[group_3])

db_session.add_all([user_1, user_2, user_3, group_1, group_2, group_3])
db_session.add_all(
[
study_1,
Expand Down Expand Up @@ -858,6 +898,12 @@ def test_get_all__non_admin_permissions_filter(
study_30,
study_31,
study_32,
study_33,
study_34,
study_35,
study_36,
study_37,
study_38,
]
)
db_session.commit()
Expand Down Expand Up @@ -892,18 +938,76 @@ def test_get_all__non_admin_permissions_filter(
@pytest.mark.parametrize(
"is_admin, study_groups, expected_ids",
[
(True, [], {str(e) for e in range(1, 33)}),
(True, ["1"], {"1", "3", "4", "7", "8", "9", "17", "19", "20", "23", "24", "25"}),
(True, ["2"], {"2", "5", "6", "7", "8", "9", "18", "21", "22", "23", "24", "25"}),
(True, [], {str(e) for e in range(1, 39)}),
(True, ["1"], {"1", "3", "4", "7", "8", "9", "17", "19", "20", "23", "24", "25", "33"}),
(True, ["2"], {"2", "5", "6", "7", "8", "9", "18", "21", "22", "23", "24", "25", "34"}),
(True, ["3"], {"37", "38"}),
(
True,
["1", "2"],
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "17", "18", "19", "20", "21", "22", "23", "24", "25"},
{
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
"33",
"34",
},
),
(
True,
["1", "3"],
{"1", "3", "4", "7", "8", "9", "17", "19", "20", "23", "24", "25", "33", "37", "38"},
),
(
True,
["1", "2", "3"],
{
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
"33",
"34",
"37",
"38",
},
),
(False, [], set()),
(False, ["1"], set()),
(False, ["2"], set()),
(False, ["3"], set()),
(False, ["1", "2"], set()),
(False, ["1", "3"], set()),
(False, ["1", "2", "3"], set()),
],
)
def test_get_all__admin_permissions_filter(
Expand All @@ -914,13 +1018,15 @@ def test_get_all__admin_permissions_filter(
) -> None:
icache: Mock = Mock(spec=ICache)
repository = StudyMetadataRepository(cache_service=icache, session=db_session)

user_1 = User(id=1, name="user1")
user_2 = User(id=2, name="user2")
user_3 = User(id=3, name="user3")

group_1 = Group(id=1, name="group1")
group_2 = Group(id=2, name="group2")
group_3 = Group(id=3, name="group3")

# create variant studies for user_1 and user_2 that are part of some groups
study_1 = VariantStudy(id=1, owner=user_1, groups=[group_1])
study_2 = VariantStudy(id=2, owner=user_1, groups=[group_2])
study_3 = VariantStudy(id=3, groups=[group_1])
Expand All @@ -932,12 +1038,15 @@ def test_get_all__admin_permissions_filter(
study_9 = VariantStudy(id=9, groups=[group_1, group_2])
study_10 = VariantStudy(id=10, owner=user_1)
study_11 = VariantStudy(id=11, owner=user_2)

# create variant studies with neither owner nor groups
study_12 = VariantStudy(id=12)
study_13 = VariantStudy(id=13, public_mode=PublicMode.READ)
study_14 = VariantStudy(id=14, public_mode=PublicMode.EDIT)
study_15 = VariantStudy(id=15, public_mode=PublicMode.EXECUTE)
study_16 = VariantStudy(id=16, public_mode=PublicMode.FULL)

# create raw studies for user_1 and user_2 that are part of some groups
study_17 = RawStudy(id=17, owner=user_1, groups=[group_1])
study_18 = RawStudy(id=18, owner=user_1, groups=[group_2])
study_19 = RawStudy(id=19, groups=[group_1])
Expand All @@ -949,13 +1058,25 @@ def test_get_all__admin_permissions_filter(
study_25 = RawStudy(id=25, groups=[group_1, group_2])
study_26 = RawStudy(id=26, owner=user_1)
study_27 = RawStudy(id=27, owner=user_2)

# create raw studies with neither owner nor groups
study_28 = RawStudy(id=28)
study_29 = RawStudy(id=29, public_mode=PublicMode.READ)
study_30 = RawStudy(id=30, public_mode=PublicMode.EDIT)
study_31 = RawStudy(id=31, public_mode=PublicMode.EXECUTE)
study_32 = RawStudy(id=32, public_mode=PublicMode.FULL)

db_session.add_all([user_1, user_2, group_1, group_2])
# create studies for user_3 that is not part of any group
study_33 = VariantStudy(id=33, owner=user_3, groups=[group_1])
study_34 = RawStudy(id=34, owner=user_3, groups=[group_2])
study_35 = VariantStudy(id=35, owner=user_3)
study_36 = RawStudy(id=36, owner=user_3)

# create studies for group_3 that has no user
study_37 = VariantStudy(id=37, groups=[group_3])
study_38 = RawStudy(id=38, groups=[group_3])

db_session.add_all([user_1, user_2, user_3, group_1, group_2, group_3])
db_session.add_all(
[
study_1,
Expand Down Expand Up @@ -990,6 +1111,12 @@ def test_get_all__admin_permissions_filter(
study_30,
study_31,
study_32,
study_33,
study_34,
study_35,
study_36,
study_37,
study_38,
]
)
db_session.commit()
Expand Down

0 comments on commit b86bff0

Please sign in to comment.