diff --git a/tests/merge_request/api/integration/get_merge_requests_test.py b/tests/merge_request/api/integration/get_merge_requests_test.py index 3ccb6221..d723fa64 100644 --- a/tests/merge_request/api/integration/get_merge_requests_test.py +++ b/tests/merge_request/api/integration/get_merge_requests_test.py @@ -225,3 +225,63 @@ def test_get_merge_requests_with_hidden( "hidden": False, }, ) + + +def test_includes_curated( + auth_server_commissioner, + merge_request_user, + merge_request_user1, + merge_request_curated, +): + server, cookies = auth_server_commissioner + rsp = req.get_merge_requests(server.url, cookies=cookies) + assert rsp.status_code == 200 + json = rsp.json() + assert_versioned( + json["assigned"], + [ + { + "id_persistent": c.id_persistent_merge_request_curated, + "created_by": { + "user_name": "test-user1", + "id_persistent": "2e858c5e-60cf-4ce5-946f-6b4559a21211", + "permission_group": "APPLICANT", + }, + "assigned_to": None, + "created_at": format_datetime(c.time_merge_request_curated), + "state": "OPEN", + "destination": { + "id_persistent": "2ec43995-338b-4f4b-b1cc-4bfc71466fc5", + "id_parent_persistent": None, + "name": "name curated tag test", + "name_path": ["name curated tag test"], + "type": "STRING", + "owner": None, + "curated": True, + "hidden": False, + }, + "origin": { + "id_persistent": "52d5de0a-2fdb-457f-80d0-6e10131ad1b9", + "id_parent_persistent": None, + "name": "name tag test1", + "name_path": ["name tag test1"], + "owner": "test-user1", + "type": "STRING", + "curated": False, + "hidden": False, + }, + } + ], + ) + assert len(json["created"]) == 0 + + +def test_does_not_include_curated_for_normal_user( + auth_server, + merge_request_curated, +): + server, cookies = auth_server + rsp = req.get_merge_requests(server.url, cookies=cookies) + assert rsp.status_code == 200 + json = rsp.json() + assert json == {"assigned": [], "created": []} diff --git a/tests/merge_request/common.py b/tests/merge_request/common.py index 81bb6f65..f10559b0 100644 --- a/tests/merge_request/common.py +++ b/tests/merge_request/common.py @@ -19,8 +19,10 @@ id_persistent_merge_request = "8a651ace-54d1-4c1d-850d-3e23d85fe990" id_persistent_merge_request_fast_forward = "c2ed0d7c-a0f7-46b9-a28b-d3a5dc701640" id_persistent_merge_request1 = "d530b3f2-2ae1-4ed6-ab8c-af2667bcde30" +id_persistent_merge_request_curated = "e0042964-781f-43da-8214-f0f82a1f569a" time_merge_request = datetime(1970, 1, 5) time_merge_request1 = datetime(1970, 1, 6) +time_merge_request_curated = datetime(1970, 2, 3) name_contribution = "contribution test" name_contribution1 = "contribution test 1" description_contribution = "a contribution for tests" diff --git a/tests/merge_request/conftest.py b/tests/merge_request/conftest.py index a5593028..9a91f053 100644 --- a/tests/merge_request/conftest.py +++ b/tests/merge_request/conftest.py @@ -158,6 +158,19 @@ def merge_request_user1( ) +@pytest.fixture +def merge_request_curated(tag_def_curated, tag_def1, contribution_for_mr): + return TagMergeRequest.objects.create( # pylint: disable=no-member + id_destination_persistent=tag_def_curated.id_persistent, + id_origin_persistent=tag_def1.id_persistent, + created_by=tag_def1.owner, + assigned_to=None, + created_at=c.time_merge_request_curated, + id_persistent=c.id_persistent_merge_request_curated, + contribution_candidate=contribution_for_mr, + ) + + @pytest.fixture def instances_merge_request_origin_user(merge_request_user, entity0, entity1): id_tag_definition = merge_request_user.id_origin_persistent diff --git a/vran/merge_request/models_django.py b/vran/merge_request/models_django.py index 332cfea4..95b0f769 100644 --- a/vran/merge_request/models_django.py +++ b/vran/merge_request/models_django.py @@ -36,14 +36,34 @@ class TagMergeRequest(AbstractMergeRequest): @classmethod def assigned_to_user(cls, user: VranUser): "Get all merge requests assigned to a user" - return TagMergeRequest.objects.filter( # pylint: disable=no-member + states = [ + TagMergeRequest.OPEN, + TagMergeRequest.CONFLICTS, + TagMergeRequest.ERROR, + ] + + assigned = TagMergeRequest.objects.filter( # pylint: disable=no-member assigned_to=user, - state__in=[ - TagMergeRequest.OPEN, - TagMergeRequest.CONFLICTS, - TagMergeRequest.ERROR, - ], + state__in=states, ) + if user.permission_group in [VranUser.EDITOR, VranUser.COMMISSIONER]: + curated = ( + TagMergeRequest.objects.filter( # pylint: disable=no-member + state__in=states + ) + .annotate( + curated=models.Subquery( + TagDefinition.most_recent_query_set() + .filter( + id_persistent=models.OuterRef("id_destination_persistent") + ) + .values("curated") + ) + ) + .filter(curated=True) + ) + return assigned.annotate(curated=models.Value(False)).union(curated) + return assigned def has_read_access(self, user: VranUser): "Check wether a user can read the merge request."