From 90386f1b5098aa3af75d32c44bc51be1219c2c3a Mon Sep 17 00:00:00 2001 From: index-git Date: Wed, 29 Nov 2023 14:50:19 +0100 Subject: [PATCH] Rewrite test_update_rights positive cases with parametrization --- .../prime_db_schema/publications_test.py | 142 ++++++++++++------ 1 file changed, 92 insertions(+), 50 deletions(-) diff --git a/src/layman/common/prime_db_schema/publications_test.py b/src/layman/common/prime_db_schema/publications_test.py index 2020e4d28..f074b7a1b 100644 --- a/src/layman/common/prime_db_schema/publications_test.py +++ b/src/layman/common/prime_db_schema/publications_test.py @@ -342,6 +342,98 @@ def test_rights(self, username, access_rights, read_to_test, write_to_test, ): publications.delete_publication(username, publication_info["publ_type_name"], publication_info["name"]) +class TestUpdateRights: + workspace_name = 'test_update_rights_workspace' + username = 'test_update_rights_user' + username2 = 'test_update_rights_user2' + + publication_name = 'test_update_rights_publication_name' + publication_type = MAP_TYPE + publication_insert_info = {"name": publication_name, + "title": publication_name, + "publ_type_name": publication_type, + "actor_name": username, + "uuid": uuid.uuid4(), + "access_rights": {"read": {settings.RIGHTS_EVERYONE_ROLE, }, + "write": {settings.RIGHTS_EVERYONE_ROLE, }, + }, + "image_mosaic": False, + } + + @pytest.fixture(scope="class", autouse=True) + def provide_data(self, request): + with app.app_context(): + workspaces.ensure_workspace(self.workspace_name) + id_workspace_user = workspaces.ensure_workspace(self.username) + userinfo = userinfo_baseline.copy() + userinfo['sub'] = '50' + users.ensure_user(id_workspace_user, userinfo) + id_workspace_user2 = workspaces.ensure_workspace(self.username2) + userinfo = userinfo_baseline.copy() + userinfo['sub'] = '60' + users.ensure_user(id_workspace_user2, userinfo) + + publications.insert_publication(self.username, self.publication_insert_info) + yield + if request.node.session.testsfailed == 0: + with app.app_context(): + publications.delete_publication(self.username, self.publication_insert_info["publ_type_name"], + self.publication_insert_info["name"]) + users.delete_user(self.username) + users.delete_user(self.username2) + workspaces.delete_workspace(self.workspace_name) + + @pytest.mark.parametrize("username, publication_update_info, read_to_test, write_to_test", [ + pytest.param( + username, + {"access_rights": {"read": {settings.RIGHTS_EVERYONE_ROLE, }, + "write": {settings.RIGHTS_EVERYONE_ROLE, }}, + "actor_name": username}, + [username, settings.RIGHTS_EVERYONE_ROLE, ], [username, settings.RIGHTS_EVERYONE_ROLE, ], + id='personal_everyone', + ), + pytest.param( + username, + {"access_rights": {"read": {username, username2, }, + "write": {username, username2, }}, + "actor_name": username}, + [username, username2, ], [username, username2, ], + id='personal_owner_editor', + ), + pytest.param( + username, + {"access_rights": {"read": {username, }, + "write": {username, }}, + "actor_name": username}, + [username, ], [username, ], + id='personal_owner', + ), + pytest.param( + username, + {"access_rights": {"read": {settings.RIGHTS_EVERYONE_ROLE, }, + "write": {settings.RIGHTS_EVERYONE_ROLE, }}, + "actor_name": None}, + [username, settings.RIGHTS_EVERYONE_ROLE, ], [username, settings.RIGHTS_EVERYONE_ROLE, ], + id='personal_everyone_as_anonym', + ), + ]) + def test_rights(self, username, publication_update_info, read_to_test, write_to_test, ): + publication_info_original = self.publication_insert_info + if not publication_update_info.get("publ_type_name"): + publication_update_info["publ_type_name"] = publication_info_original["publ_type_name"] + if not publication_update_info.get("name"): + publication_update_info["name"] = publication_info_original["name"] + publications.update_publication(username, + publication_update_info, + ) + assert_access_rights(username, + publication_info_original["name"], + publication_info_original["publ_type_name"], + read_to_test, + write_to_test, + ) + + def test_update_rights(): def case_test_update_rights(username, publication_info_original, @@ -393,56 +485,6 @@ def case_test_update_rights(username, publications.insert_publication(username, publication_insert_info) - case_test_update_rights(username, - publication_insert_info, - {"access_rights": {"read": {settings.RIGHTS_EVERYONE_ROLE, }, - "write": {settings.RIGHTS_EVERYONE_ROLE, }, - }, - 'actor_name': username}, - [username, settings.RIGHTS_EVERYONE_ROLE, ], - [username, settings.RIGHTS_EVERYONE_ROLE, ], - ) - - case_test_update_rights(username, - publication_insert_info, - {"access_rights": {"read": {username, username2, }, - "write": {username, username2, }, - }, - 'actor_name': username}, - [username, username2, ], - [username, username2, ], - ) - - case_test_update_rights(username, - publication_insert_info, - {"access_rights": {"read": {settings.RIGHTS_EVERYONE_ROLE, }, - "write": {settings.RIGHTS_EVERYONE_ROLE, }, - }, - 'actor_name': username}, - [username, settings.RIGHTS_EVERYONE_ROLE, ], - [username, settings.RIGHTS_EVERYONE_ROLE, ], - ) - - case_test_update_rights(username, - publication_insert_info, - {"access_rights": {"read": {username, }, - "write": {username, }, - }, - 'actor_name': username}, - [username, ], - [username, ], - ) - - case_test_update_rights(username, - publication_insert_info, - {"access_rights": {"read": {settings.RIGHTS_EVERYONE_ROLE, }, - "write": {settings.RIGHTS_EVERYONE_ROLE, }, - }, - 'actor_name': None}, - [username, settings.RIGHTS_EVERYONE_ROLE, ], - [username, settings.RIGHTS_EVERYONE_ROLE, ], - ) - with pytest.raises(LaymanError) as exc_info: case_test_update_rights(username, publication_insert_info,