Esempio n. 1
0
def assert_json_results(response, json, catalogs):
    """Verify that the API result list matches the given catalog batch."""
    items = json['items']
    assert json['total'] == len(items) == len(catalogs)
    items.sort(key=lambda i: i['id'])
    catalogs.sort(key=lambda c: c.id)
    for n, catalog in enumerate(catalogs):
        assert_json_result(response, items[n], catalog)


@pytest.mark.parametrize('scopes', [
    [ODPScope.CATALOG_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.CATALOG_READ),
])
def test_list_catalogs(api, catalog_batch, scopes):
    authorized = ODPScope.CATALOG_READ in scopes
    r = api(scopes).get('/catalog/')
    if authorized:
        assert_json_results(r, r.json(), catalog_batch)
    else:
        assert_forbidden(r)
    assert_db_state(catalog_batch)


@pytest.mark.parametrize('scopes', [
    [ODPScope.CATALOG_READ],
    [],
    all_scopes,
Esempio n. 2
0
    assert json['type'] == scope.type


def assert_json_results(response, json, scopes):
    """Verify that the API result list matches the given scope batch."""
    items = json['items']
    assert json['total'] == len(items) == len(scopes)
    items.sort(key=lambda i: i['id'])
    scopes.sort(key=lambda s: s.id)
    for n, scope in enumerate(scopes):
        assert_json_result(response, items[n], scope)


@pytest.mark.parametrize('scopes', [
    [ODPScope.SCOPE_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.SCOPE_READ),
])
def test_list_scopes(api, scope_batch, scopes):
    authorized = ODPScope.SCOPE_READ in scopes
    # add the parameterized scopes to the batch of expected scopes,
    # as they will be created by the api fixture
    scope_batch += [ScopeFactory.build(id=s.value, type='odp') for s in scopes]
    r = api(scopes).get('/scope/')
    if authorized:
        assert_json_results(r, r.json(), scope_batch)
    else:
        assert_forbidden(r)
    assert_db_state(scope_batch)
Esempio n. 3
0
def assert_json_results(response, json, users):
    """Verify that the API result list matches the given user batch."""
    items = json['items']
    assert json['total'] == len(items) == len(users)
    items.sort(key=lambda i: i['id'])
    users.sort(key=lambda u: u.id)
    for n, user in enumerate(users):
        assert_json_result(response, items[n], user)


@pytest.mark.parametrize('scopes', [
    [ODPScope.USER_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.USER_READ),
])
def test_list_users(api, user_batch, scopes):
    authorized = ODPScope.USER_READ in scopes
    r = api(scopes).get('/user/')
    if authorized:
        assert_json_results(r, r.json(), user_batch)
    else:
        assert_forbidden(r)
    assert_db_state(user_batch)


@pytest.mark.parametrize('scopes', [
    [ODPScope.USER_READ],
    [],
    all_scopes,
Esempio n. 4
0
def assert_json_results(response, json, schemas):
    """Verify that the API result list matches the given schema batch."""
    items = json['items']
    assert json['total'] == len(items) == len(schemas)
    items.sort(key=lambda i: i['id'])
    schemas.sort(key=lambda s: s.id)
    for n, schema in enumerate(schemas):
        assert_json_result(response, items[n], schema)


@pytest.mark.parametrize('scopes', [
    [ODPScope.SCHEMA_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.SCHEMA_READ),
])
def test_list_schemas(api, schema_batch, scopes):
    authorized = ODPScope.SCHEMA_READ in scopes
    r = api(scopes).get('/schema/')
    if authorized:
        assert_json_results(r, r.json(), schema_batch)
    else:
        assert_forbidden(r)
    assert_db_state(schema_batch)


@pytest.mark.parametrize('scopes', [
    [ODPScope.SCHEMA_READ],
    [],
    all_scopes,
Esempio n. 5
0
def assert_json_results(response, json, roles):
    """Verify that the API result list matches the given role batch."""
    items = json['items']
    assert json['total'] == len(items) == len(roles)
    items.sort(key=lambda i: i['id'])
    roles.sort(key=lambda r: r.id)
    for n, role in enumerate(roles):
        assert_json_result(response, items[n], role)


@pytest.mark.parametrize('scopes', [
    [ODPScope.ROLE_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.ROLE_READ),
])
def test_list_roles(api, role_batch, scopes, collection_auth):
    authorized = ODPScope.ROLE_READ in scopes

    if collection_auth == CollectionAuth.MATCH:
        api_client_collection = role_batch[2].collection
        expected_result_batch = [role_batch[2]]
    elif collection_auth == CollectionAuth.MISMATCH:
        api_client_collection = CollectionFactory()
        expected_result_batch = []
    else:
        api_client_collection = None
        expected_result_batch = role_batch

    r = api(scopes, api_client_collection).get('/role/')
Esempio n. 6
0
def assert_json_results(response, json, tags):
    """Verify that the API result list matches the given tag batch."""
    items = json['items']
    assert json['total'] == len(items) == len(tags)
    items.sort(key=lambda i: i['id'])
    tags.sort(key=lambda t: t.id)
    for n, tag in enumerate(tags):
        assert_json_result(response, items[n], tag)


@pytest.mark.parametrize('scopes', [
    [ODPScope.TAG_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.TAG_READ),
])
def test_list_tags(api, tag_batch, scopes):
    authorized = ODPScope.TAG_READ in scopes
    r = api(scopes).get('/tag/')
    if authorized:
        assert_json_results(r, r.json(), tag_batch)
    else:
        assert_forbidden(r)
    assert_db_state(tag_batch)


@pytest.mark.parametrize('scopes', [
    [ODPScope.TAG_READ],
    [],
    all_scopes,
def assert_json_results(response, json, providers):
    """Verify that the API result list matches the given provider batch."""
    items = json['items']
    assert json['total'] == len(items) == len(providers)
    items.sort(key=lambda i: i['id'])
    providers.sort(key=lambda p: p.id)
    for n, provider in enumerate(providers):
        assert_json_result(response, items[n], provider)


@pytest.mark.parametrize('scopes', [
    [ODPScope.PROVIDER_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.PROVIDER_READ),
])
def test_list_providers(api, provider_batch, scopes):
    authorized = ODPScope.PROVIDER_READ in scopes
    r = api(scopes).get('/provider/')
    if authorized:
        assert_json_results(r, r.json(), provider_batch)
    else:
        assert_forbidden(r)
    assert_db_state(provider_batch)


@pytest.mark.parametrize('scopes', [
    [ODPScope.PROVIDER_READ],
    [],
    all_scopes,
def assert_json_results(response, json, clients):
    """Verify that the API result list matches the given client batch."""
    items = [j for j in json['items'] if j['id'] != 'odp.test']
    assert json['total'] - 1 == len(items) == len(clients)
    items.sort(key=lambda i: i['id'])
    clients.sort(key=lambda c: c.id)
    for n, client in enumerate(clients):
        assert_json_result(response, items[n], client)


@pytest.mark.parametrize('scopes', [
    [ODPScope.CLIENT_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.CLIENT_READ),
])
def test_list_clients(api, client_batch, scopes, collection_auth):
    authorized = ODPScope.CLIENT_READ in scopes

    if collection_auth == CollectionAuth.MATCH:
        api_client_collection = client_batch[2].collection
        expected_result_batch = [client_batch[2]]
    elif collection_auth == CollectionAuth.MISMATCH:
        api_client_collection = CollectionFactory()
        expected_result_batch = []
    else:
        api_client_collection = None
        expected_result_batch = client_batch

    r = api(scopes, api_client_collection).get('/client/')
Esempio n. 9
0
    assert json['public'] == collection_tag['public']


def assert_doi_result(response, collection):
    assert response.status_code == 200
    assert re.match(DOI_REGEX, doi := response.json()) is not None
    prefix, _, suffix = doi.rpartition('.')
    assert prefix == f'10.15493/{collection.doi_key}'
    assert re.match(r'^\d{8}$', suffix) is not None


@pytest.mark.parametrize('scopes', [
    [ODPScope.COLLECTION_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.COLLECTION_READ),
])
def test_list_collections(api, collection_batch, scopes, collection_auth):
    authorized = ODPScope.COLLECTION_READ in scopes

    if collection_auth == CollectionAuth.MATCH:
        api_client_collection = collection_batch[2]
        expected_result_batch = [collection_batch[2]]
    elif collection_auth == CollectionAuth.MISMATCH:
        api_client_collection = CollectionFactory()
        expected_result_batch = [api_client_collection]
        collection_batch += [api_client_collection]
    else:
        api_client_collection = None
        expected_result_batch = collection_batch
Esempio n. 10
0
def assert_json_record_results(response, json, records):
    """Verify that the API result list matches the given record batch."""
    items = json['items']
    assert json['total'] == len(items) == len(records)
    items.sort(key=lambda i: i['id'])
    records.sort(key=lambda r: r.id)
    for n, record in enumerate(records):
        assert_json_record_result(response, items[n], record)


@pytest.mark.parametrize('scopes', [
    [ODPScope.RECORD_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.RECORD_READ),
])
def test_list_records(api, record_batch, scopes, collection_auth):
    authorized = ODPScope.RECORD_READ in scopes

    if collection_auth == CollectionAuth.MATCH:
        api_client_collection = record_batch[2].collection
        expected_result_batch = [record_batch[2]]
    elif collection_auth == CollectionAuth.MISMATCH:
        api_client_collection = CollectionFactory()
        expected_result_batch = []
    else:
        api_client_collection = None
        expected_result_batch = record_batch

    r = api(scopes, api_client_collection).get('/record/')
Esempio n. 11
0
def assert_json_results(response, json, projects):
    """Verify that the API result list matches the given project batch."""
    items = json['items']
    assert json['total'] == len(items) == len(projects)
    items.sort(key=lambda i: i['id'])
    projects.sort(key=lambda p: p.id)
    for n, project in enumerate(projects):
        assert_json_result(response, items[n], project)


@pytest.mark.parametrize('scopes', [
    [ODPScope.PROJECT_READ],
    [],
    all_scopes,
    all_scopes_excluding(ODPScope.PROJECT_READ),
])
def test_list_projects(api, project_batch, scopes):
    authorized = ODPScope.PROJECT_READ in scopes
    r = api(scopes).get('/project/')
    if authorized:
        assert_json_results(r, r.json(), project_batch)
    else:
        assert_forbidden(r)
    assert_db_state(project_batch)


@pytest.mark.parametrize('scopes', [
    [ODPScope.PROJECT_READ],
    [],
    all_scopes,