From ac8b022c33bc4ad8ba5b37dca030867e81094a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Wed, 20 Oct 2021 22:02:17 +0200 Subject: [PATCH] tests: move package_revise tests to separate file --- .../dcor_schemas/tests/test_auth_dataset.py | 195 ----------------- .../tests/test_auth_package_revise.py | 207 ++++++++++++++++++ 2 files changed, 207 insertions(+), 195 deletions(-) create mode 100644 ckanext/dcor_schemas/tests/test_auth_package_revise.py diff --git a/ckanext/dcor_schemas/tests/test_auth_dataset.py b/ckanext/dcor_schemas/tests/test_auth_dataset.py index 31a282f..d6bf228 100644 --- a/ckanext/dcor_schemas/tests/test_auth_dataset.py +++ b/ckanext/dcor_schemas/tests/test_auth_dataset.py @@ -1,7 +1,5 @@ import cgi -import copy import pathlib -import uuid import pytest @@ -47,199 +45,6 @@ def test_dataset_add_resources_only_to_drafts(): ) -@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') -@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') -def test_dataset_add_resources_only_to_drafts_package_revise(): - """do not allow adding resources to non-draft datasets""" - user = factories.User() - owner_org = factories.Organization(users=[{ - 'name': user['id'], - 'capacity': 'admin' - }]) - # Note: `call_action` bypasses authorization! - create_context = {'ignore_auth': False, 'user': user['name']} - test_context = {'ignore_auth': False, 'user': user['name'], "model": model} - # create a dataset - dataset, _ = make_dataset(create_context, owner_org, with_resource=True, - activate=True) - # assert: adding resources to active datasets forbidden - resources = copy.deepcopy(dataset["resources"]) - resources.append({"name": "peter.rtdc", - "url": "upload", - "package_id": dataset["id"]}) - with pytest.raises( - logic.NotAuthorized, - match="Adding resources to non-draft datasets not allowed"): - helpers.call_auth( - "package_revise", test_context, - **{"update": { - "id": dataset["id"], - "resources": resources} - }) - - -@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') -@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') -def test_dataset_add_resources_only_to_drafts_package_revise_control(): - """do not allow adding resources to non-draft datasets""" - user = factories.User() - owner_org = factories.Organization(users=[{ - 'name': user['id'], - 'capacity': 'admin' - }]) - # Note: `call_action` bypasses authorization! - create_context = {'ignore_auth': False, 'user': user['name']} - test_context = {'ignore_auth': False, 'user': user['name'], "model": model} - # create a dataset - dataset, _ = make_dataset(create_context, owner_org, with_resource=True, - activate=False) - # assert: adding resources to draft datasets allowed - resources = copy.deepcopy(dataset["resources"]) - resources.append({"name": "peter.rtdc", - "url": "upload", - "sp:chip:channel width": 21.0, # this must be supported - "package_id": dataset["id"]}) - helpers.call_auth( - "package_revise", test_context, - **{"update": { - "id": dataset["id"], - "resources": resources} - }) - - -@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') -@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') -def test_dataset_add_resources_set_id_not_allowed_package_revise(): - """do not allow adding resources to non-draft datasets""" - user = factories.User() - owner_org = factories.Organization(users=[{ - 'name': user['id'], - 'capacity': 'admin' - }]) - # Note: `call_action` bypasses authorization! - create_context = {'ignore_auth': False, 'user': user['name']} - test_context = {'ignore_auth': False, 'user': user['name'], "model": model} - # create a dataset - dataset, _ = make_dataset(create_context, owner_org, with_resource=True, - activate=False) - # assert: adding resources to active datasets forbidden - resources = copy.deepcopy(dataset["resources"]) - resources.append({"name": "peter.rtdc", - "url": "upload", - "package_id": dataset["id"], - "id": uuid.uuid4()}) - with pytest.raises(logic.NotAuthorized, match="Invalid resource ID"): - helpers.call_auth( - "package_revise", test_context, - **{"update": { - "id": dataset["id"], - "resources": resources, - }}, - ) - - -@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') -@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') -def test_dataset_update_resources_only_for_drafts_package_revise(): - """do not allow editing resources (except description)""" - user = factories.User() - owner_org = factories.Organization(users=[{ - 'name': user['id'], - 'capacity': 'admin' - }]) - # Note: `call_action` bypasses authorization! - create_context = {'ignore_auth': False, 'user': user['name']} - test_context = {'ignore_auth': False, 'user': user['name'], "model": model} - - # create a dataset - dataset, _ = make_dataset(create_context, owner_org, with_resource=True, - activate=False) - # modifying the description should work - helpers.call_auth( - "package_revise", test_context, - **{"update": { - "id": dataset["id"], - "resources": [{ - "id": dataset["resources"][0]["id"], - "description": "A new description", - "sp:chip:channel width": 21.0, # this must be supported - } - ], - }}) - helpers.call_action( - "package_revise", test_context, - **{"match__id": dataset["id"], - "update__resources__0": {"description": "A new description", - "sp:chip:channel width": 21.0} - }) - # make sure that worked - dataset2 = helpers.call_action("package_show", create_context, - id=dataset["id"]) - assert dataset2["resources"][-1]["description"] == "A new description" - assert dataset2["resources"][-1]["sp:chip:channel width"] == 21.0 - - # modifying anything else should *not* work - with pytest.raises( - logic.NotAuthorized, - match="Editing not allowed: dc:experiment:date=2017-02-09"): - helpers.call_auth( - "package_revise", test_context, - **{"update": { - "id": dataset["id"], - "resources": [{ - "id": dataset["resources"][0]["id"], - "dc:experiment:date": "2017-02-09"}], - }}) - - -@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') -@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') -def test_dataset_update_resources_only_for_drafts_package_revise_2(): - """do not allow editing resources (except description)""" - user = factories.User() - owner_org = factories.Organization(users=[{ - 'name': user['id'], - 'capacity': 'admin' - }]) - # Note: `call_action` bypasses authorization! - create_context = {'ignore_auth': False, 'user': user['name']} - test_context = {'ignore_auth': False, 'user': user['name'], "model": model} - - # create a dataset - dataset, _ = make_dataset(create_context, owner_org, with_resource=True, - activate=True) - - # modifying the description should not work for active datasets - with pytest.raises( - logic.NotAuthorized, - match="Changing 'resources' not allowed for non-draft datasets"): - helpers.call_auth( - "package_revise", test_context, - **{"update": { - "id": dataset["id"], - "resources": [{ - "id": dataset["resources"][0]["id"], - "description": "A new description", - } - ], - }}) - - # modifying the RSS should not work for active datasets - with pytest.raises( - logic.NotAuthorized, - match="Changing 'resources' not allowed for non-draft datasets"): - helpers.call_auth( - "package_revise", test_context, - **{"update": { - "id": dataset["id"], - "resources": [{ - "id": dataset["resources"][0]["id"], - "sp:chip:channel width": 21.0, - } - ], - }}) - - @pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') @pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') def test_dataset_create_anonymous(): diff --git a/ckanext/dcor_schemas/tests/test_auth_package_revise.py b/ckanext/dcor_schemas/tests/test_auth_package_revise.py new file mode 100644 index 0000000..57bb0c8 --- /dev/null +++ b/ckanext/dcor_schemas/tests/test_auth_package_revise.py @@ -0,0 +1,207 @@ +import copy +import pathlib +import uuid + +import pytest + +import ckan.logic as logic +import ckan.tests.factories as factories +import ckan.tests.helpers as helpers +from ckan import model + +from .helper_methods import make_dataset + +data_path = pathlib.Path(__file__).parent / "data" + + +@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') +@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') +def test_dataset_add_resources_only_to_drafts_package_revise(): + """do not allow adding resources to non-draft datasets""" + user = factories.User() + owner_org = factories.Organization(users=[{ + 'name': user['id'], + 'capacity': 'admin' + }]) + # Note: `call_action` bypasses authorization! + create_context = {'ignore_auth': False, 'user': user['name']} + test_context = {'ignore_auth': False, 'user': user['name'], "model": model} + # create a dataset + dataset, _ = make_dataset(create_context, owner_org, with_resource=True, + activate=True) + # assert: adding resources to active datasets forbidden + resources = copy.deepcopy(dataset["resources"]) + resources.append({"name": "peter.rtdc", + "url": "upload", + "package_id": dataset["id"]}) + with pytest.raises( + logic.NotAuthorized, + match="Adding resources to non-draft datasets not allowed"): + helpers.call_auth( + "package_revise", test_context, + **{"update": { + "id": dataset["id"], + "resources": resources} + }) + + +@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') +@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') +def test_dataset_add_resources_only_to_drafts_package_revise_control(): + """do not allow adding resources to non-draft datasets""" + user = factories.User() + owner_org = factories.Organization(users=[{ + 'name': user['id'], + 'capacity': 'admin' + }]) + # Note: `call_action` bypasses authorization! + create_context = {'ignore_auth': False, 'user': user['name']} + test_context = {'ignore_auth': False, 'user': user['name'], "model": model} + # create a dataset + dataset, _ = make_dataset(create_context, owner_org, with_resource=True, + activate=False) + # assert: adding resources to draft datasets allowed + resources = copy.deepcopy(dataset["resources"]) + resources.append({"name": "peter.rtdc", + "url": "upload", + "sp:chip:channel width": 21.0, # this must be supported + "package_id": dataset["id"]}) + helpers.call_auth( + "package_revise", test_context, + **{"update": { + "id": dataset["id"], + "resources": resources} + }) + + +@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') +@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') +def test_dataset_add_resources_set_id_not_allowed_package_revise(): + """do not allow adding resources to non-draft datasets""" + user = factories.User() + owner_org = factories.Organization(users=[{ + 'name': user['id'], + 'capacity': 'admin' + }]) + # Note: `call_action` bypasses authorization! + create_context = {'ignore_auth': False, 'user': user['name']} + test_context = {'ignore_auth': False, 'user': user['name'], "model": model} + # create a dataset + dataset, _ = make_dataset(create_context, owner_org, with_resource=True, + activate=False) + # assert: adding resources to active datasets forbidden + resources = copy.deepcopy(dataset["resources"]) + resources.append({"name": "peter.rtdc", + "url": "upload", + "package_id": dataset["id"], + "id": uuid.uuid4()}) + with pytest.raises(logic.NotAuthorized, match="Invalid resource ID"): + helpers.call_auth( + "package_revise", test_context, + **{"update": { + "id": dataset["id"], + "resources": resources, + }}, + ) + + +@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') +@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') +def test_dataset_update_resources_only_for_drafts_package_revise(): + """do not allow editing resources (except description)""" + user = factories.User() + owner_org = factories.Organization(users=[{ + 'name': user['id'], + 'capacity': 'admin' + }]) + # Note: `call_action` bypasses authorization! + create_context = {'ignore_auth': False, 'user': user['name']} + test_context = {'ignore_auth': False, 'user': user['name'], "model": model} + + # create a dataset + dataset, _ = make_dataset(create_context, owner_org, with_resource=True, + activate=False) + # modifying the description should work + helpers.call_auth( + "package_revise", test_context, + **{"update": { + "id": dataset["id"], + "resources": [{ + "id": dataset["resources"][0]["id"], + "description": "A new description", + "sp:chip:channel width": 21.0, # this must be supported + } + ], + }}) + helpers.call_action( + "package_revise", test_context, + **{"match__id": dataset["id"], + "update__resources__0": {"description": "A new description", + "sp:chip:channel width": 21.0} + }) + # make sure that worked + dataset2 = helpers.call_action("package_show", create_context, + id=dataset["id"]) + assert dataset2["resources"][-1]["description"] == "A new description" + assert dataset2["resources"][-1]["sp:chip:channel width"] == 21.0 + + # modifying anything else should *not* work + with pytest.raises( + logic.NotAuthorized, + match="Editing not allowed: dc:experiment:date=2017-02-09"): + helpers.call_auth( + "package_revise", test_context, + **{"update": { + "id": dataset["id"], + "resources": [{ + "id": dataset["resources"][0]["id"], + "dc:experiment:date": "2017-02-09"}], + }}) + + +@pytest.mark.ckan_config('ckan.plugins', 'dcor_schemas') +@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') +def test_dataset_update_resources_only_for_drafts_package_revise_2(): + """do not allow editing resources (except description)""" + user = factories.User() + owner_org = factories.Organization(users=[{ + 'name': user['id'], + 'capacity': 'admin' + }]) + # Note: `call_action` bypasses authorization! + create_context = {'ignore_auth': False, 'user': user['name']} + test_context = {'ignore_auth': False, 'user': user['name'], "model": model} + + # create a dataset + dataset, _ = make_dataset(create_context, owner_org, with_resource=True, + activate=True) + + # modifying the description should not work for active datasets + with pytest.raises( + logic.NotAuthorized, + match="Changing 'resources' not allowed for non-draft datasets"): + helpers.call_auth( + "package_revise", test_context, + **{"update": { + "id": dataset["id"], + "resources": [{ + "id": dataset["resources"][0]["id"], + "description": "A new description", + } + ], + }}) + + # modifying the RSS should not work for active datasets + with pytest.raises( + logic.NotAuthorized, + match="Changing 'resources' not allowed for non-draft datasets"): + helpers.call_auth( + "package_revise", test_context, + **{"update": { + "id": dataset["id"], + "resources": [{ + "id": dataset["resources"][0]["id"], + "sp:chip:channel width": 21.0, + } + ], + }})