コード例 #1
0
def test_parse_hub_key_s1(_get_provider, _get_repository):
    hub_key = 'https://openpermissions.org/s1/hub1/0123456789abcdef0123456789abcdef/asset/abcdef0123456789abcdef0123456789'
    _get_repository.return_value = make_future({
        'data': {
            'organisation': {'id': 'orguid'}
        }
    })
    _get_provider.return_value = make_future({
        'data': {
            'organisation_id': 'orguid',
            'website': 'www.something.org'
        }
    })
    expected = {
        'entity_id': 'abcdef0123456789abcdef0123456789',
        'entity_type': 'asset',
        'hub_id': 'hub1',
        'repository_id': '0123456789abcdef0123456789abcdef',
        'resolver_id': 'https://openpermissions.org',
        'schema_version': 's1',
        'hub_key': hub_key,
        'provider': {
            'organisation_id': 'orguid',
            'website': 'http://www.something.org'
        }
    }

    result = IOLoop.current().run_sync(
        partial(hub_key_handler._parse_hub_key, hub_key))
    assert _get_provider.call_count == 1
    assert _get_repository.call_count == 1
    assert result == expected
コード例 #2
0
def test_notification(options, API, get_token):
    db = create_mockdb()
    options.service_id = 'a test service ID'
    client = API()

    client.index.get.return_value = make_future({
        'service_name': 'Index Service',
        'service_id': 'index1234',
        'version': '0.1.0'
    })

    client.index.notifications.post.return_value = make_future(None)
    yield send_notification(db)

    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
    body = json.dumps({'id': db.repository_id})

    client.index.notifications.prepare_request.assert_called_with(
        request_timeout=options.request_timeout,
        headers=headers,
        body=body
    )

    assert get_token.called
    assert client.index.notifications.post.called
コード例 #3
0
def test_prepare_oauth_required_missing_bearer(jwt, verify_repository_token, get_organisation_id, options):
    jwt.decode.return_value = {
        "client": {
            "service_type": "external",
            "organisation_id": "developerco",
            "id": "client3"
        },
        "sub": "client1"
    }
    verify_repository_token.return_value = make_future(True)
    get_organisation_id.return_value = make_future('org1')
    options.standalone = False
    handler = PartialMockedHandler()
    handler.request.headers = {'Authorization': 'token1234'}

    yield handler.prepare()

    verify_repository_token.assert_called_once_with('token1234', 'r', 'repo1')
    get_organisation_id.assert_called_once_with('repo1')
    assert handler.token == {
        "client": {
            "service_type": "external",
            "organisation_id": "developerco",
            "id": "client3"
        },
        "sub": "client1"
    }
コード例 #4
0
def test_resolve_link_id_not_in_s0_hk(_get_repos_for_source_id, _get_ids):
    _get_repos_for_source_id.return_value = make_future([{'repository_id': '043023143124', 'entity_id': '0102343434'}])
    _get_ids.return_value = make_future([{'source_id_type': 'testidtype', 'source_id': 'this id has spaces and ?'}])
    res = yield hub_key_handler.resolve_link_id_type({'redirect_id_type': 'testidtype',
                                                      'links': {'testidtype': 'http://test/{source_id}'}},
                                                     {'id_type': 'otheridtype', 'entity_id': '321a23'})
    assert res is not None
    assert res == 'http://test/this+id+has+spaces+and+%3F'
コード例 #5
0
def test_store_db_called(get_asset_ids, send_notification):
    db = DatabaseConnection(TEST_NAMESPACE)
    db.store = Mock()
    db.update = Mock()
    db.store.return_value = make_future(None)
    db.update.return_value = make_future(None)
    yield db.store(get_valid_xml())

    assert db.store.call_count==1
コード例 #6
0
def create_mockdb():
    db = DatabaseConnection(TEST_NAMESPACE)
    db.store = Mock()
    db.update = Mock()
    db.query = Mock()
    db.store.return_value = make_future(None)
    db.update.return_value = make_future(None)
    db.query.return_value = make_future(None)
    return db
コード例 #7
0
def test_retrieve_retrieve_offers_for_assets_one_item(_process):
    expected = {'nice': 123}
    db = create_mockdb()
    db.query.return_value = make_future(Mock(buffer=None))
    _process.return_value = make_future(expected)

    result = yield Offer.retrieve_for_assets([{'source_id_type': 'chub', 'source_id': 'id0'}], db)

    assert result == expected
コード例 #8
0
def test_insert_ids_no_errors(_insert_ids, insert_timestamps):
    db = create_mockdb()
    _insert_ids.return_value = make_future(None)
    insert_timestamps.return_value = make_future(None)

    result = yield asset.add_ids(db, 'asset1',
                                 [{'source_id': 'id1', 'source_id_type': 'id_type1'}])
    _insert_ids.assert_called_with(db, 'asset1', [{'source_id': 'id1', 'source_id_type': 'id_type1'}])
    insert_timestamps.assert_called_with(db, ['asset1'])
    assert not result
コード例 #9
0
def test_handler_get_no_result(assets, options, url):
    options.service_id = 'service_id'
    assets.retrieve_paged_ids.return_value = make_future(([], ()))
    url.return_value = yield make_future('https://localhost:8000/bigdata/namespace/c8ab01')
    repository_id = 'c8ab01'

    handler = PartialMockedHandler()
    handler.get(repository_id).result()

    assert handler.finish.called
    assert handler.finish.call_args[0][0].keys() == ['status', 'data', 'metadata']
コード例 #10
0
def test_assets_handler_post_no_headers(assets):
    assets.exists.return_value = make_future(True)
    assets.add_ids.return_value = make_future([])

    handler = PartialMockedHandler()
    handler.request.body = '{"ids": ["id1"]}'

    with pytest.raises(exceptions.HTTPError) as exc:
        handler.post(TEST_NAMESPACE, 'asset1').result()

    assert exc.value.status_code == 415
コード例 #11
0
def test_assets_handler_post(assets, audit):
    assets.exists.return_value = make_future(True)
    assets.add_ids.return_value = make_future([])

    handler = PartialMockedHandler()
    handler.request.headers = {'Content-Type': 'application/json'}
    handler.request.body = '{"ids": ["id1"]}'
    handler.post(TEST_NAMESPACE, 'asset1').result()

    audit.log_asset_ids.assert_called_once_with('asset1', ['id1'], {'sub': 'client1'})
    assert assets.add_ids.call_count == 1
    handler.finish.assert_called_once_with({"status": 200})
コード例 #12
0
def test_assets_handler_post_has_errors(assets):
    assets.exists.return_value = make_future(True)
    assets.add_ids.return_value = make_future(['errormsg1'])

    handler = PartialMockedHandler()
    handler.request.headers = {'Content-Type': 'application/json'}
    handler.request.body = '{"ids": ["id1"]}'

    with pytest.raises(exceptions.HTTPError) as exc:
        handler.post(TEST_NAMESPACE, 'asset1').result()

    assert exc.value.status_code == 400
コード例 #13
0
def test_offers_handler_put(DatabaseConnection, _validate_offer_expiry, offer, audit):
    offer.expire.return_value = make_future(None)
    audit.log_update_offer_expiry.return_value = make_future(None)

    handler = PartialMockedOfferHandler()
    handler.request.body = '{"expires": "1999-12-31T23:59:59Z"}'
    handler.request.headers = {"Content-Type": "application/json"}

    handler.put(TEST_NAMESPACE, "0ffe31").result()

    offer.expire.assert_called_once_with(TEST_NAMESPACE, '0ffe31', '1999-12-31T23:59:59Z')
    audit.log_update_offer_expiry.assert_called_with('0ffe31', '1999-12-31T23:59:59Z',{"client": { "id": "client3"}, "sub": "client1"})
    handler.finish.assert_called_once_with({'status': 200, 'data': {'id': '0ffe31', 'expires': '1999-12-31T23:59:59Z'}})
コード例 #14
0
def test_repository_assets_handler_post(assets, audit, helper, _validate_body):
    helper.validate.return_value = None
    assets.store.return_value = make_future('asset data')
    audit.log_added_assets.return_value = make_future(None)

    handler = PartialMockedHandler()
    yield handler.post(TEST_NAMESPACE)

    assert assets.store.call_count == 1
    audit.log_added_assets.assert_called_once_with(
        'asset data',
        {'sub': 'client1', 'client': {'id': 'testco'}},
        repository_id='c8ab01')
    handler.finish.assert_called_once_with({"status": 200})
コード例 #15
0
def test_get_repositories_empty_result(service_client):
    """Test receiving an empty list from the index"""
    service_client.return_value = make_future(MagicMock())
    client = yield service_client()
    endpoint = client.index['entity-types'].asset.repositories
    endpoint.post.return_value = make_future({})

    ids = [
        {'id': 'an asset', 'id_type': 'an_id_type'},
        {'id': 'another asset', 'id_type': 'an_id_type'},
    ]

    result = yield get_repositories(ids)

    assert result == []
コード例 #16
0
def test__insert_ids_multiple_ids():
    db = create_mockdb()
    db.update.return_value = make_future('')
    yield asset._insert_ids(db, 'assetid1',
        [{'source_id': 'id1', 'source_id_type': 'id_type1'},
         {'source_id': 'id2', 'source_id_type': 'id_type2'}])
    assert db.update.call_count >= 1
コード例 #17
0
def test__retrieve_paged_ids_query_no_result():
    db = create_mockdb()

    response = Mock()
    response.buffer = StringIO("""<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <head>
    <variable name="entity_uri"/>
    <variable name="source_id_type"/>
    <variable name="source_id"/>
    <variable name="last_modified"/>
  </head>
  <results>
  </results>
</sparql>
""")

    db.query.return_value = make_future(response)

    result = yield asset.retrieve_paged_assets(
        db,
        datetime(2016, 1, 1, 0, 0, 0),
        datetime(2016, 1, 2, 0, 0, 0),
        10,
        2000)

    assert result[0] == []
コード例 #18
0
def test_post_with_bad_data(service_client, get_repositories, get_repository):
    get_repositories.return_value = future_repositories
    get_repository.return_value = future_repository

    mock_response = Mock()
    mock_response.body = '{"errors": [{"source_id_type": "", "message": "not supported asset id type"}]}'
    mock_response.code = 400
    exc = httpclient.HTTPError(400, response=mock_response)
    service_client.return_value = make_future(MagicMock())
    client = yield service_client()
    endpoint = client.repository.repositories[''].search.offers
    endpoint.post.side_effect = exc

    handler = _create_offers_handler()

    # MUT
    handler.request.body = ('[{"source_id":' +
                            '"https://openpermissions.org/s0/hub1/asset/exampleco/ExampleCoPictureID/1",' +
                            '"source_id_type":""}]')

    with pytest.raises(HTTPError) as excinfo:
        handler.post().result()

    assert excinfo.value.status_code == mock_response.code
    assert excinfo.value.errors == json.loads(mock_response.body)
コード例 #19
0
def test_single_page_with_data(koi, repository_service_client):
    """
    Store data in the database and check if data in the next page. The result
    date range should be stored so that it can be used as the start of the date
    range the next time the repository is queried.
    """
    client = MagicMock()
    repository_service_client.return_value = make_future(client)
    endpoint = client.repository.repositories.__getitem__().assets.identifiers
    endpoint.get.side_effect = mock_identifiers({
        1: {'data': ['some data'],
            'metadata': {'result_range': ('2000-01-01', '2010-01-01')}}
    })
    scheduler, repostore, manager = mock_manager()

    yield manager.fetch_identifiers('repo_a')

    # Should have called API twice, stopped after page 2 because there was
    # no further data
    from_time = manager.DEFAULT_FROM_TIME.isoformat()
    endpoint.get.assert_has_calls([call(**{'page': 1, 'from': from_time}),
                                   call(**{'page': 2, 'from': from_time})])

    # Only one set of data should have been inserted into the database
    manager.db.add_entities.assert_called_once_with('asset',
                                                    ['some data'],
                                                    'repo_a')

    # Check date was stored for the next time the endpoint is scheduled
    assert repostore._shelf['repo_a']['next'] == datetime(2010, 1, 1)
コード例 #20
0
def test_three_pages_with_data(koi, repository_service_client):
    """
    Store data in the database and check if data in the next page after each
    page. The last result date range should be stored so that it can be used
    as the start of the date range the next time the repository is queried.
    """
    client = MagicMock()
    repository_service_client.return_value = make_future(client)
    endpoint = client.repository.repositories.__getitem__().assets.identifiers
    endpoint.get.side_effect = mock_identifiers({
        1: {'data': ['first page'],
            'metadata': {'result_range': ('2000-01-01', '2001-01-01')}},
        2: {'data': ['second page'],
            'metadata': {'result_range': ('2001-01-01', '2002-01-01')}},
        3: {'data': ['third page'],
            'metadata': {'result_range': ('2002-01-01', '2003-01-01')}}
    })
    scheduler, repostore, manager = mock_manager()

    yield manager.fetch_identifiers('repo_a')

    from_time = manager.DEFAULT_FROM_TIME.isoformat()
    endpoint.get.assert_has_calls([
        call(**{'page': 1, 'from': from_time}),
        call(**{'page': 2, 'from': from_time}),
        call(**{'page': 3, 'from': from_time}),
        call(**{'page': 4, 'from': from_time}),
    ])
    manager.db.add_entities.assert_has_calls([
        call('asset', ['first page'], 'repo_a'),
        call('asset', ['second page'], 'repo_a'),
        call('asset', ['third page'], 'repo_a'),
    ])
    assert repostore._shelf['repo_a']['next'] == datetime(2003, 1, 1)
コード例 #21
0
def test_create():
    db = create_mockdb()
    db.store.return_value = make_future(None)

    content_type = 'text/turtle'
    yield Offer.store(db, 'data', content_type=content_type)
    db.store.assert_called_once_with('data', content_type=content_type)
コード例 #22
0
    def test_cannot_access_hosted_resource(self):
        self.request.body_arguments['requested_access'] = ['r']
        self.request.body_arguments['resource_id'] = ['1234']
        with patch.object(grants.Repository, 'get') as repo_get:
            client = perch.Service(
                parent=ORGANISATION,
                id='something',
                organisation_id=ORGANISATION.id,
                service_type='external',
                state=perch.State.approved
            )
            repo = perch.Repository(
                parent=ORGANISATION,
                service_id=self.request.client.id,
                organisation_id=ORGANISATION.id,
                state=perch.State.approved,
                permissions=[
                    {'type': 'organisation_id',
                     'value': client.organisation_id,
                     'permission': '-'
                     }
                ]
            )
            repo_get.return_value = make_future(repo)

            grant = self.Grant(self.request)
            with pytest.raises(grants.Unauthorized):
                yield grant.verify_access_hosted_resource(client)
コード例 #23
0
    def test_verify_access_service(self):
        self.request.body_arguments['requested_access'] = ['r']
        with patch.object(grants.Service, 'get') as service_get:
            client = perch.Service(
                parent=ORGANISATION,
                id='something',
                organisation_id=ORGANISATION.id,
                service_type='external',
                state=perch.State.approved
            )
            service = perch.Repository(
                parent=ORGANISATION,
                organisation_id=ORGANISATION.id,
                state=perch.State.approved,
                permissions=[
                    {'type': 'organisation_id',
                     'value': client.organisation_id,
                     'permission': 'rw'
                     }
                ]
            )
            service_get.return_value = make_future(service)

            grant = self.Grant(self.request)
            yield grant.verify_access_service(client)
コード例 #24
0
def test_process_other_id_type(retrieve):
    db = create_mockdb()
    retrieve.side_effect = lambda rid, oid: make_future({'id': oid, 'rid': rid.repository_id})

    csv_buffer = StringIO('\n'.join([
        'ids,entity_id_bundle,policies',
        'a11beee1110,http://ns/hub/other_id#other_id1,0ffe31|0ffe32',
        'a11beee2220,http://ns/hub/other_id#other_id2,0ffe31|0ffe33'
    ]))
    expected = [
        {'entity_id': 'a11beee1110',
         'source_id': 'other_id1',
         'source_id_type': 'other_id',
         'offers': [
             {'rid': TEST_NAMESPACE, 'id': '0ffe31'},
             {'rid': TEST_NAMESPACE, 'id': '0ffe32'}
         ]},
        {'entity_id': 'a11beee2220',
         'source_id': 'other_id2',
         'source_id_type': 'other_id',
         'offers': [
             {'rid': TEST_NAMESPACE, 'id': '0ffe31'},
             {'rid': TEST_NAMESPACE, 'id': '0ffe33'}
         ]},
    ]
    result = yield Offer._process(db, csv_buffer)
    assert (result) == (expected)
コード例 #25
0
ファイル: test_db.py プロジェクト: openpermissions/index-srv
def test_get_repositories_with_utf8_ids():
    """
    Test the query result is formatted correctly and covers the case where
    some elements of the query are not found.

    Not checking the db query string because the query will be covered by our
    system tests
    """
    id_value = 'b%E2%82%AC'
    id_type = u'som%E2%82%ACthing'
    ids = [
        {'source_id': id_value, 'source_id_type': id_type}
    ]
    db = DbInterface('url', '8080', '/path/', 'schema')
    # mock methods that should be called
    db._run_query = Mock()
    db._run_query.return_value = make_future([{
        'source_id': 'b%E2%82%AC',
        'source_id_type': 'som%E2%82%ACthing',
        'repositories': json.dumps([]),
        'relations': json.dumps([])
    }])

    func = partial(db._query_ids, ids)
    res = ioloop.IOLoop().run_sync(func)

    assert res == [{
        'source_id': id_value,
        'source_id_type': id_type,
        'repositories': [],
        'relations': []
    }]
コード例 #26
0
def test_insert_timestamps_multiple_ids_update_called():
    db = create_mockdb()
    db.update.return_value = make_future('')

    yield asset.insert_timestamps(db, ['asset1', 'asset2'])

    assert db.update.call_count == 1
コード例 #27
0
def test_prepare_no_repository_id(jwt, verify_repository_token, get_organisation_id, options):
    jwt.decode.return_value = {
        "client": {
            "service_type": "external",
            "organisation_id": "developerco",
            "id": "client3"
        },
        "sub": "client1"
    }

    verify_repository_token.return_value = make_future(True)

    options.standalone = False
    handler = PartialMockedHandler()
    handler.request.headers = {'Authorization': 'Bearer token1234'}
    handler.path_kwargs = {}

    yield handler.prepare()

    verify_repository_token.assert_called_once_with('token1234', 'r', None)
    assert not get_organisation_id.called
    assert handler.token == {
        "client": {
            "service_type": "external",
            "organisation_id": "developerco",
            "id": "client3"
        },
        "sub": "client1"
    }
コード例 #28
0
def test_parse_hub_key_s0(_get_provider):
    hub_key = 'https://openpermissions.org/s0/hub1/asset/maryevans/maryevanspictureid/10413373'
    _get_provider.return_value = make_future(
        {
            'data': {
                'website': 'www.something.org'
            }
        }
    )
    expected = {
        'resolver_id': 'https://openpermissions.org',
        'schema_version': 's0',
        'hub_id': 'hub1',
        'entity_type': 'asset',
        'organisation_id': 'maryevans',
        'id_type': 'maryevanspictureid',
        'entity_id': '10413373',
        'hub_key': hub_key,
        'provider': {
            'website': 'http://www.something.org'
        }
    }

    result = IOLoop.current().run_sync(
        partial(hub_key_handler._parse_hub_key, hub_key))

    assert _get_provider.call_count == 1
    assert result == expected
コード例 #29
0
def test_load_data_valid_ttl(db_interface):
    db = db_interface.return_value
    db.store = Mock(return_value=make_future(None))
    do_load_data(db, os.path.abspath(
        os.path.join(FIXTURE_DIR, '../../fixtures/test.ttl')))

    assert db_interface().store.call_count == 1
コード例 #30
0
def test_process_chub_id_type(retrieve):
    db = create_mockdb()

    retrieve.side_effect = lambda rid, oid: make_future({'entity_id': oid, 'repository_id': rid.repository_id})
    csv_buffer = StringIO('\n'.join([
        'entity_id_bundle,ids,policies',
        'http://ns/id/hub_key#a11beee1110,a11beee1110,0ffe31|0ffe32',
        'http://ns/id/hub_key#a11beee2220,a11beee2220,0ffe31|0ffe33'
    ]))

    expected = [
        {'entity_id': 'a11beee1110',
         'source_id': 'a11beee1110',
         'source_id_type': 'hub_key',
         'offers': [
             {'repository_id': TEST_NAMESPACE, 'entity_id': '0ffe31'},
             {'repository_id': TEST_NAMESPACE, 'entity_id': '0ffe32'}
         ]},
        {'entity_id': 'a11beee2220',
         'source_id': 'a11beee2220',
         'source_id_type': 'hub_key',
         'offers': [
             {'repository_id': TEST_NAMESPACE, 'entity_id': '0ffe31'},
             {'repository_id': TEST_NAMESPACE, 'entity_id': '0ffe33'}
         ]},
    ]

    result = yield Offer._process(db, csv_buffer)

    assert len(result) == len(expected)
    assert result[0] in expected
    assert result[1] in expected
コード例 #31
0
 def mock_retrieve(source_id_list, repository_id):
     return make_future([(a['source_id_type'], a['source_id'])
                         for a in source_id_list])
コード例 #32
0
import json

import pytest
from mock import patch, Mock, MagicMock

from tornado import httpclient
from koi import define_options
from koi.base import HTTPError
from koi.test_helpers import make_future, gen_test

from query.controllers.offers_handler import OffersHandler
from query.app import CONF_DIR

future_accounts_repositories = make_future([{
    'id': 'repo1',
    'service': {
        'location': 'https://localhost'
    }
}])

future_repositories = make_future([({
    'repository_id': 'repo1'
}, [{
    'source_id':
    'https://openpermissions.org/s0/hub1/asset/exampleco/ExampleCoPictureID/1',
    'source_id_type': 'chub'
}])])
future_repository = make_future({
    'status': 200,
    'data': {
        'service': {
            'location': 'https://localhost',
コード例 #33
0
def test_expire():
    db = create_mockdb()
    db.update.return_value = make_future(None)
    yield Offer.expire(db, '0ffe31', 'timestamp')

    assert db.update.call_count == 1
コード例 #34
0
import tornado.httpclient
from repository.controllers.namespace_handler import NamespaceHandler

TEST_NAMESPACE = 'c8ab01'


class PartialMockedHandler(NamespaceHandler):
    def __init__(self):
        super(PartialMockedHandler, self).__init__(application=MagicMock(),
                                                   request=MagicMock())
        self.finish = MagicMock()


@patch('repository.controllers.namespace_handler.create_namespace',
       return_value=make_future(True))
def test_namespace_handler_post(create_namespace):
    handler = PartialMockedHandler()
    handler.post(TEST_NAMESPACE).result()

    create_namespace.assert_called_once_with(TEST_NAMESPACE)
    handler.finish.assert_called_once_with({"status": 200})


@patch('repository.controllers.namespace_handler.create_namespace',
       return_value=make_future(True))
def test_namespace_handler_http_error(create_namespace):
    create_namespace.side_effect = tornado.httpclient.HTTPError(404, "Error")

    handler = PartialMockedHandler()
    with pytest.raises(exceptions.HTTPError) as exc:
コード例 #35
0
import os

import pytest
from mock import patch, MagicMock

from koi import define_options
from koi.base import HTTPError
from koi.test_helpers import make_future

from query.controllers.licensors_handler import LicensorsHandler
from query.app import CONF_DIR

future_not_found = make_future({
    'status': 404,
    'errors': [{
        'message': 'Not Found'
    }]
})

future_asset_repositories = make_future({
    'status': 200,
    'data': {
        'repositories': [{
            'repository_id': 'an_org'
        }]
    }
})

future_repository = make_future({
    'status': 200,
    'data': {
コード例 #36
0
def test__insert_ids_one_id():
    db = create_mockdb()
    db.update.return_value = make_future('')
    yield asset._insert_ids(db, 'assetid1', [{'source_id': 'id1', 'source_id_type': 'id_type1'}])
    assert db.update.call_count == 1
コード例 #37
0
def test_offer_handler_get(DatabaseConnection, offer):
    offer.retrieve.return_value = make_future('offer')
    handler = PartialMockedOfferHandler()
    handler.get(TEST_NAMESPACE, '0ffe31').result()
    handler.finish.assert_called_once_with({'status': 200, 'data': 'offer'})
コード例 #38
0
# See the License for the specific language governing permissions and limitations under the License.

from functools import partial
from mock import MagicMock, patch
import pytest
from koi.test_helpers import make_future
from koi.exceptions import HTTPError
from tornado.ioloop import IOLoop

from repository.controllers import offers_handler

TEST_NAMESPACE = 'c8ab01'


@patch("repository.models.offer.Offer.expired",
       return_value=make_future(False))
@patch("repository.controllers.offers_handler.DatabaseConnection",
       return_value=TEST_NAMESPACE)
def test__validate_offer_expiry(DatabaseConnection, expired):
    func = partial(offers_handler.OfferHandler._validate_offer_expiry,
                   TEST_NAMESPACE, 'offerid', '2015-01-01T00:00:00Z')
    IOLoop.current().run_sync(func)


@patch("repository.models.offer.Offer.expired", return_value=make_future(True))
@patch("repository.controllers.offers_handler.DatabaseConnection",
       return_value=TEST_NAMESPACE)
def test__validate_offer_expiry_expired(DatabaseConnection, expired):
    func = partial(offers_handler.OfferHandler._validate_offer_expiry,
                   TEST_NAMESPACE, 'offerid', '2015-01-01T00:00:00Z')
    with pytest.raises(HTTPError) as exc:
コード例 #39
0
def test_insert_timestamps_no_ids():
    db = create_mockdb()
    db.update.return_value = make_future([])
    yield asset.insert_timestamps(db, [])
    assert db.update.call_count == 0
コード例 #40
0
def get_valid_xml():
    with open(os.path.abspath(os.path.join(FIXTURE_DIR, 'sample.xml')), 'r') as fr:
        return fr.read()


def test_foo():
    hub_key = 'https://openpermissions.org/s0/hub1/asset/testco/testcopictureid/{}'.format(uuid.uuid4())
    doc = ASSET_TEMPLATE.format(hub_key=hub_key)
    results = asset.get_asset_ids(doc, 'text/rdf+n3')
    expected = [
        {u'entity_id': hub_key.split('/')[-1]}]
    assert sorted(results) == sorted(expected)


@patch('repository.models.asset.send_notification', return_value=make_future(None))
@patch('repository.models.asset.get_asset_ids', return_value=[{u'entity_id': 'fa0',
         u'source_id': u'id1',
         u'source_id_type': u'testcopictureid'}])
@gen_test
def test_store_db_called(get_asset_ids, send_notification):
    db = DatabaseConnection(TEST_NAMESPACE)
    db.store = Mock()
    db.update = Mock()
    db.store.return_value = make_future(None)
    db.update.return_value = make_future(None)
    yield db.store(get_valid_xml())

    assert db.store.call_count==1

コード例 #41
0
def test_offer_handler_get_no_offer(DatabaseConnection, offer):
    offer.retrieve.return_value = make_future(None)
    handler = PartialMockedOfferHandler()
    with pytest.raises(HTTPError) as exc:
        handler.get(TEST_NAMESPACE, '0ffe31').result()
    assert exc.value.status_code == 404
コード例 #42
0
def test__insert_ids_contains_id_type():
    db = create_mockdb()
    db.update.return_value = make_future('')
    yield asset._insert_ids(db, 'assetid1', [{'source_id': 'id1', 'source_id_type': 'id_type1'}])
    assert db.update.call_args[0][0].find('id_type1') >= 0
コード例 #43
0
 def __getattr__(self, item):
     v = getattr(x, item)
     if callable(v):
         return lambda *args, **kwargs: make_future(v(*args, **kwargs))
コード例 #44
0
def test_get_repositories(service_client):
    """Test successfully getting a list of repository IDs from the index"""
    response = [
        {
            'id': 'an asset',
            'id_type': 'an_id_type',
            'repositories': [{
                'repository_id': 'a'
            }, {
                'repository_id': 'b'
            }]
        },
        {
            'id': 'another asset',
            'id_type': 'an_id_type',
            'repositories': [{
                'repository_id': 'b'
            }, {
                'repository_id': 'c'
            }]
        },
    ]
    service_client.return_value = make_future(MagicMock())
    client = yield service_client()
    endpoint = client.index['entity-types'].asset.repositories
    endpoint.post.return_value = make_future({'data': response})

    ids = [{
        'id': 'an asset',
        'id_type': 'an_id_type'
    }, {
        'id': 'another asset',
        'id_type': 'an_id_type'
    }]

    result = yield get_repositories(ids)

    # check mock
    assert endpoint.post.call_count == 1
    endpoint.prepare_request.assert_called_once_with(body=json.dumps(ids),
                                                     request_timeout=180,
                                                     headers={
                                                         'Accept':
                                                         'application/json',
                                                         'Content-Type':
                                                         'application/json'
                                                     })

    # check result
    assert sorted(result) == [
        IndexItem({'repository_id': 'a'}, [{
            'id': 'an asset',
            'id_type': 'an_id_type'
        }]),
        IndexItem({'repository_id': 'b'}, [{
            'id': 'an asset',
            'id_type': 'an_id_type'
        }, {
            'id': 'another asset',
            'id_type': 'an_id_type'
        }]),
        IndexItem({'repository_id': 'c'}, [{
            'id': 'another asset',
            'id_type': 'an_id_type'
        }]),
    ]
コード例 #45
0
import json
from mock import Mock, patch

from tornado.gen import coroutine, Return
from tornado.concurrent import Future
from tornado import httpclient
import pytest
from koi.exceptions import HTTPError
from koi.test_helpers import make_future

from onboarding.models import remote
from onboarding.models.assets import onboard


@patch('onboarding.models.remote.oauth2.get_token',
       return_value=make_future('token1234'))
@patch('onboarding.models.remote.options')
@patch('onboarding.models.remote.API')
def test_onboard_csv(API, options, get_token):
    options.service_id = 'test service ID'
    options.url_transformation = ''
    client = API()

    client.transformation.get.return_value = make_future({
        'data': {
            'serivce_name': 'transformation service',
            'service_id': 'tid123',
            'version': '0.1.0'
        }
    })
コード例 #46
0
def test_insert_timestamps_one_id_update_called():
    db = create_mockdb()
    db.update.return_value = make_future('')
    yield asset.insert_timestamps(db, ['asset1'])
    assert db.update.call_count == 1