reedv / ckanext-publicrestrictiondatasets

GNU Affero General Public License v3.0
2 stars 2 forks source link

Add tests #4

Open reedv opened 3 years ago

reedv commented 3 years ago

Here is an example I already use

import ckanapi
import pprint
import datetime

API_KEY = "xxxxxxxxxxxxxxxxxxxx" # some non-sysadmin's API token
demo = ckanapi.RemoteCKAN('http://myserver.myorg.local', apikey=API_KEY)
res = demo.action.package_list()
pprint.pprint(res)
res = demo.action.organization_list_for_user()
pprint.pprint(res)

TESTPKG_NAME = f"publicrestrictiondatasets_test{datetime.date.today().strftime('%Y%m%d')}"
TESTPKG_ORG = "xxxxxxxxxx-b9fb-4c77-8bbe-xxxxxxxxxx"
# attempt to create public dataset
print("\n==========\ntest creating public pkg\n==========")
try:
    res = demo.action.package_create(**{
        "name": TESTPKG_NAME,
        "private": False,
        "owner_org": TESTPKG_ORG
    })
    # pprint.pprint(res)
except Exception as e:
    pprint.pprint(e)
    errd = e.error_dict
    assert 'private' in errd
    assert errd['private'][0] == 'Only sysadmin users may set datasets as public'
    print("SUCCESS\n\n")

# attempt to create private dataset
print("\n==========\ntest creating private pkg\n==========")
res = demo.action.package_create(**{
    "name": TESTPKG_NAME,
    "private": True,
    "owner_org": TESTPKG_ORG
})
# pprint.pprint(res)
print("SUCCESS\n\n")

# attempt to convert private dataset public
print("\n==========\ntest convert private to public pkg\n==========")
try:
    res = demo.action.package_update(**{
        "name": TESTPKG_NAME,
        "private": False,
    })
    # pprint.pprint(res)
except Exception as e:
    pprint.pprint(e.error_dict)
    errd = e.error_dict
    assert 'private' in errd
    assert errd['private'][0] == 'Only sysadmin users may set datasets as public'
    print("SUCCESS\n\n")

# attempt to modify an existing public dataset in any other way
print("\n==========\ntest modify existing public pkg\n==========")
res = demo.action.package_update(**{
    "name": "another-public-test-dataset",
    "author": TESTPKG_NAME,
})
# pprint.pprint(res)
print("SUCCESS\n\n")

# delete dataset create for testing here
print("\n==========\n deleting test pkg\n==========")
res = demo.action.package_delete(**{
    "id": TESTPKG_NAME
})
pprint.pprint(res)
reedv commented 3 years ago

see https://docs.ckan.org/en/2.9/extensions/testing-extensions.html