Ejemplo n.º 1
0
def test_call(mocker):
    cinp = CInP('http://localhost:8080', '/api/v1/', None)
    mocked_open = mocker.patch('urllib.request.OpenerDirector.open')
    mocked_open.return_value = MockResponse(200, {}, '{}')

    with pytest.raises(InvalidRequest):
        cinp.call('/api/v1/', {})

    with pytest.raises(InvalidRequest):
        cinp.call('/api/v1/model', {})

    with pytest.raises(InvalidRequest):
        cinp.call('/api/v1/model:dfs:', {})

    with pytest.raises(InvalidRequest):
        cinp.call('/api/v1/model(adsf)', 'sdf')

    mocked_open.reset_mock()
    return_value = cinp.call('/api/v1/model(myfunc)', {})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model(myfunc)'
    assert req.data == b'{}'
    assert req.headers == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CALL'
    assert return_value == {}

    mocked_open.reset_mock()
    return_value = cinp.call('/api/v1/model:234:(myfunc)', {})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model:234:(myfunc)'
    assert req.data == b'{}'
    assert req.headers == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CALL'
    assert return_value == {}

    mocked_open.reset_mock()
    return_value = cinp.call('/api/v1/model:234:sdf:(myfunc)', {})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model:234:sdf:(myfunc)'
    assert req.data == b'{}'
    assert req.headers == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CALL'
    assert return_value == {}

    mocked_open.reset_mock()
    return_value = cinp.call('/api/v1/model(myfunc)', {'arg1': 12})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model(myfunc)'
    assert req.data == b'{"arg1": 12}'
    assert req.headers == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CALL'
    assert return_value == {}

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(200, {}, '"The Value"')
    return_value = cinp.call('/api/v1/model(myfunc)', {})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model(myfunc)'
    assert req.data == b'{}'
    assert req.headers == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CALL'
    assert return_value == 'The Value'

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(200, {}, '{ "stuff": "nice"}')
    return_value = cinp.call('/api/v1/model(myfunc)', {})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model(myfunc)'
    assert req.data == b'{}'
    assert req.headers == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CALL'
    assert return_value == {'stuff': 'nice'}

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(404, {}, '')
    with pytest.raises(NotFound):
        cinp.call('/api/v1/model:123:(myfunc)', {})

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(201, {}, '')
    with pytest.raises(ResponseError):
        cinp.call('/api/v1/model(myfunc)', {})
Ejemplo n.º 2
0
        }))

print('look there it is')
print(client.list('/api/v1/Car/Car'))

print(
    'but we can not look at it, checkAuth in that model saies that only owners can do that'
)
try:
    print(client.get('/api/v1/Car/Car:gremlin:'))
except NotAuthorized:
    pass

print('let us login...')
auth_token = client.call('/api/v1/User/Session(login)', {
    'username': '******',
    'password': '******'
})
client.setAuth('ford', auth_token)
print('Now we can see it')
print(client.get('/api/v1/Car/Car:gremlin:'))

print('login as a super user...')
auth_token = client.call('/api/v1/User/Session(login)', {
    'username': '******',
    'password': '******'
})
client.setAuth('admin', auth_token)
print('can still see it, superusers are subjext to checkAuth')
print(client.get('/api/v1/Car/Car:gremlin:'))

print('let us login again...')