Beispiel #1
0
def test_service_client(url_mock, classes_mock):
    url_mock.return_value = MOCK_URL
    classes_mock.return_value = {}

    sc = ServiceClient(MOCK_SERVICE_ID)

    spec_endpoint = "/endpoints"

    assert sc._api_key is None
    assert sc._service_id == MOCK_SERVICE_ID
    assert sc._base_url == MOCK_URL
    assert sc._root_path is None
    assert sc._swagger_path == spec_endpoint

    # Custom root path
    sc = ServiceClient(MOCK_SERVICE_ID, root_path='/api')
    assert sc._root_path == '/api'

    # Custom Swagger path
    sc = ServiceClient(MOCK_SERVICE_ID, swagger_path='/spec')
    assert sc._swagger_path == "/spec"

    # Passed in API Key
    sc = ServiceClient(MOCK_SERVICE_ID, api_key="this_is_an_API_key")
    assert sc._api_key == "this_is_an_API_key"
Beispiel #2
0
def test_get_api_spec(url_mock, classes_mock, auth_session_mock, mock_response,
                      mock_swagger):
    mock_response.return_value = mock.Mock(ok=True)
    mock_response.return_value.json.return_value = mock_swagger

    url_mock.return_value = MOCK_URL
    classes_mock.return_value = {}

    sc = ServiceClient(MOCK_SERVICE_ID)

    spec = sc.get_api_spec()
    assert spec == mock_swagger
Beispiel #3
0
def test_generate_classes(url_mock, api_spec_mock,
                          parse_mock, mock_swagger):
    api_spec_mock.return_value = {}
    mock_class_function = (lambda client, return_type: "return")
    parse_mock.return_value = {'class': mock_class_function}
    url_mock.return_value = MOCK_URL

    sc = ServiceClient(MOCK_SERVICE_ID)

    classes = sc.generate_classes()

    assert 'class' in classes
Beispiel #4
0
def test_generate_classes(url_mock, api_spec_mock, parse_mock, mock_swagger):
    api_spec_mock.return_value = {}
    mock_class_function = (lambda client, return_type: "return")
    parse_mock.return_value = {'class': mock_class_function}
    url_mock.return_value = MOCK_URL

    sc = ServiceClient(MOCK_SERVICE_ID, root_path='/foo')

    classes = sc.generate_classes()
    parse_mock.assert_called_once_with(api_spec_mock.return_value,
                                       root_path='/foo')

    assert 'class' in classes
Beispiel #5
0
def test_generate_classes_maybe_cached(url_mock, api_spec_mock,
                                       parse_mock, mock_swagger):
    api_spec_mock.return_value = {}
    mock_class_function = (lambda client, return_type: "return")
    parse_mock.return_value = {'class': mock_class_function}
    url_mock.return_value = MOCK_URL

    sc = ServiceClient(MOCK_SERVICE_ID)

    mock_spec_str = str(json.dumps(mock_swagger))
    mock_spec = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(mock_spec_str)  # noqa: E501
    classes = sc.generate_classes_maybe_cached(mock_spec)

    assert 'class' in classes
Beispiel #6
0
def test_get_service(mock_client, classes_mock):
    classes_mock.return_value = {}
    sc = ServiceClient(MOCK_SERVICE_ID)
    expected_service = {'current_url': MOCK_URL}
    mock_client.return_value.services.get.return_value = expected_service
    service = _get_service(sc)
    assert service == expected_service
Beispiel #7
0
def test_get_base_url(get_service_mock, classes_mock):
    get_service_mock.return_value = {'current_url': MOCK_URL}
    classes_mock.return_value = {}

    sc = ServiceClient(MOCK_SERVICE_ID)

    assert sc._base_url == MOCK_URL
    get_service_mock.assert_called_once_with(sc)
Beispiel #8
0
def test_generate_classes_maybe_cached(url_mock, api_spec_mock, parse_mock,
                                       mock_swagger):
    api_spec_mock.return_value = {}
    mock_class_function = (lambda client, return_type: "return")
    parse_mock.return_value = {'class': mock_class_function}
    url_mock.return_value = MOCK_URL

    sc = ServiceClient(MOCK_SERVICE_ID, root_path='/foo')

    mock_spec_str = str(json.dumps(mock_swagger))
    mock_spec = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(
        mock_spec_str)  # noqa: E501
    classes = sc.generate_classes_maybe_cached(mock_spec)

    parse_mock.assert_has_calls([
        # the call from generate_classes_maybe_cached in ServiceClient.__init__
        mock.call({}, root_path='/foo'),
        # the call from generate_classes_maybe_cached in this test
        mock.call(mock_spec, root_path='/foo')
    ])

    assert 'class' in classes
Beispiel #9
0
def test_get_service__not_found(mock_client, classes_mock):
    classes_mock.return_value = {}
    sc = ServiceClient(MOCK_SERVICE_ID)
    err_resp = response.Response({
        'status_code': 404,
        'error': 'not_found',
        'errorDescription': 'The requested resource could not be found.',
        'content': True})
    err_resp.json = lambda: err_resp.json_data

    mock_client.return_value.services.get.side_effect = CivisAPIError(err_resp)

    with pytest.raises(CivisAPIError) as excinfo:
        _get_service(sc)

    expected_error = ('(404) The requested resource could not be found.')
    assert str(excinfo.value) == expected_error