def test_make_service_request_to_unexisting_detail_endpoint(
        client_session_mock, auth_api_client, logic_module, event_loop):

    url = f'/async/{logic_module.endpoint_name}/nowhere/123/'

    # mock aiohttp responses
    with open(os.path.join(CURRENT_PATH, 'fixtures/swagger_documents.json'),
              'rb') as r:
        swagger_body = r.read()
    responses = [
        AiohttpResponseMock(method='GET',
                            url=f'{logic_module.endpoint}/docs/swagger.json',
                            status=200,
                            body=swagger_body,
                            headers={'Content-Type': 'application/json'}),
    ]
    client_session_mock.return_value = create_aiohttp_session_mock(
        responses, loop=event_loop)

    # make api request
    response = auth_api_client.get(url)

    # This operation doesn't exist in Swagger schema so 404 must be thrown
    assert response.status_code == 404
    assert response.has_header('Content-Type')
    assert response.get('Content-Type') == 'application/json'
    assert json.loads(
        response.content)['detail'] == "Endpoint not found: GET /nowhere/{id}/"
def test_make_service_request_data_and_raw(client_session_mock,
                                           auth_api_client, logic_module,
                                           content, content_type, event_loop):
    url = f'/async/{logic_module.endpoint_name}/thumbnail/1/'

    # mock aiohttp responses
    with open(os.path.join(CURRENT_PATH, 'fixtures/swagger_documents.json'),
              'rb') as r:
        swagger_body = r.read()

    responses = [
        AiohttpResponseMock(method='GET',
                            url=f'{logic_module.endpoint}/docs/swagger.json',
                            status=200,
                            body=swagger_body,
                            headers={'Content-Type': 'application/json'}),
        AiohttpResponseMock(method='GET',
                            url=f'{logic_module.endpoint}/thumbnail/1/',
                            status=200,
                            body=content,
                            headers={'Content-Type': content_type}),
    ]
    client_session_mock.return_value = create_aiohttp_session_mock(
        responses, loop=event_loop)

    # make api request
    response = auth_api_client.get(url)

    assert response.status_code == 200
    if content:
        assert response.content == content
    else:
        assert response.content == b''
    assert response.has_header('Content-Type')
    assert response.get('Content-Type') == content_type
예제 #3
0
def test_make_service_request_with_datamesh_list(auth_api_client, datamesh):
    lm1, lm2, relationship = datamesh
    factories.JoinRecord(relationship=relationship,
                         record_id=None,
                         record_uuid='19a7f600-74a0-4123-9be5-dfa69aa172cc',
                         related_record_id=1,
                         related_record_uuid=None)

    url = f'/{lm1.endpoint_name}/siteprofiles/'

    # mock requests
    with open(os.path.join(CURRENT_PATH,
                           'fixtures/swagger_location.json')) as r:
        swagger_location_body = r.read()
    with open(os.path.join(CURRENT_PATH,
                           'fixtures/swagger_documents.json')) as r:
        swagger_documents_body = r.read()
    with open(os.path.join(CURRENT_PATH,
                           'fixtures/data_list_siteprofile.json')) as r:
        data_location_body = r.read()
    with open(os.path.join(CURRENT_PATH,
                           'fixtures/data_detail_document.json')) as r:
        data_documents_body = r.read()
    httpretty.register_uri(httpretty.GET,
                           f'{lm1.endpoint}/docs/swagger.json',
                           body=swagger_location_body,
                           adding_headers={'Content-Type': 'application/json'})
    httpretty.register_uri(httpretty.GET,
                           f'{lm2.endpoint}/docs/swagger.json',
                           body=swagger_documents_body,
                           adding_headers={'Content-Type': 'application/json'})
    httpretty.register_uri(httpretty.GET,
                           f'{lm1.endpoint}/siteprofiles/',
                           body=data_location_body,
                           adding_headers={'Content-Type': 'application/json'})
    httpretty.register_uri(httpretty.GET,
                           f'{lm2.endpoint}/documents/1/',
                           body=data_documents_body,
                           adding_headers={'Content-Type': 'application/json'})

    # make api request
    response = auth_api_client.get(url, {'join': 'true'})

    assert response.status_code == 200
    assert response.has_header('Content-Type')
    assert response.get('Content-Type') == 'application/json'
    data = response.json()

    # first item in the list has a joined record
    item1 = data["results"][0]
    assert relationship.key in item1
    assert len(item1[relationship.key]) == 1
    assert item1[relationship.key][0]['id'] == 1

    # second item in the list doesn't have a joined record
    item2 = data["results"][1]
    assert relationship.key in item2
    assert len(item2[relationship.key]) == 0
예제 #4
0
def test_join_data_one_obj_w_relationships(mock_perform_request, mock_spec,
                                           auth_api_client, relationship):
    factories.JoinRecord(relationship=relationship,
                         record_id=1,
                         related_record_id=2,
                         record_uuid=None,
                         related_record_uuid=None)
    # mock app
    mock_spec.return_value = Mock(Spec)

    # mock first response
    service_response = ({
        'id': 1,
        'name': 'test',
        'contact_uuid': 1
    }, 200, {
        'Content-Type': ['application/json']
    })
    # mock second response
    expand_response = ({
        'id': 1,
        'file': '/somewhere/128/',
    }, 200, {
        'Content-Type': ['application/json']
    })
    mock_perform_request.side_effect = [service_response, expand_response]

    # make api request
    path = '/{}/{}/'.format(
        relationship.origin_model.logic_module_endpoint_name, 'products')
    response = auth_api_client.get(path, {'join': ''})

    # validate result
    expected_data = {
        'id': 1,
        'name': 'test',
        'contact_uuid': 1,
        relationship.key: [{
            'id': 1,
            'file': '/somewhere/128/',
        }]
    }

    assert response.status_code == 200
    assert json.loads(response.content) == expected_data
예제 #5
0
def test_make_service_request_to_unexisting_detail_endpoint(
        auth_api_client, logic_module):

    url = f'/{logic_module.endpoint_name}/nowhere/123/'

    # mock requests
    with open(os.path.join(CURRENT_PATH,
                           'fixtures/swagger_documents.json')) as r:
        swagger_body = r.read()
    httpretty.register_uri(httpretty.GET,
                           f'{logic_module.endpoint}/docs/swagger.json',
                           body=swagger_body,
                           adding_headers={'Content-Type': 'application/json'})

    # make api request
    response = auth_api_client.get(url)

    # This operation doesn't exist in Swagger schema so 404 must be thrown
    assert response.status_code == 404
    assert response.has_header('Content-Type')
    assert response.get('Content-Type') == 'application/json'
    assert json.loads(
        response.content)['detail'] == "Endpoint not found: GET /nowhere/{id}/"
예제 #6
0
def test_join_data_list(mock_perform_request, mock_spec, auth_api_client,
                        relationship_with_10_records, org):
    mock_spec.return_value = Mock(Spec)

    join_records = relationship_with_10_records.joinrecords.all()
    main_service_data = [{
        'uuid': item.record_uuid,
        'name': f'Boiler #{i}'
    } for i, item in enumerate(join_records)]
    main_service_response = (main_service_data, 200, {
        'Content-Type': ['application/json']
    })

    expand_responses = []
    for item in join_records:
        expand_responses.append(({
            'uuid': item.related_record_uuid,
            'file': '/documents/128/'
        }, 200, {
            'Content-Type': ['application/json']
        }))
    mock_perform_request.side_effect = [main_service_response
                                        ] + expand_responses

    # make api request
    path = '/{}/{}/'.format(
        relationship_with_10_records.origin_model.logic_module_endpoint_name,
        'products')
    response = auth_api_client.get(path, {'join': ''})

    assert response.status_code == 200
    for i, item in enumerate(json.loads(response.content)):
        assert item['uuid'] == str(join_records[i].record_uuid)
        assert relationship_with_10_records.key in item
        nested = item[relationship_with_10_records.key]
        assert len(nested) == 1
        assert nested[0]['uuid'] == str(join_records[i].related_record_uuid)
예제 #7
0
def test_make_service_request_data_and_raw(auth_api_client, logic_module,
                                           content, content_type):
    url = f'/{logic_module.endpoint_name}/thumbnail/1/'

    # mock requests
    with open(os.path.join(CURRENT_PATH,
                           'fixtures/swagger_documents.json')) as r:
        swagger_body = r.read()
    httpretty.register_uri(httpretty.GET,
                           f'{logic_module.endpoint}/docs/swagger.json',
                           body=swagger_body,
                           adding_headers={'Content-Type': 'application/json'})
    httpretty.register_uri(httpretty.GET,
                           f'{logic_module.endpoint}/thumbnail/1/',
                           body=content,
                           adding_headers={'Content-Type': content_type})

    # make api request
    response = auth_api_client.get(url)

    assert response.status_code == 200
    assert response.content == content.encode()
    assert response.has_header('Content-Type')
    assert response.get('Content-Type') == content_type
def test_make_service_request_with_datamesh_list(client_session_mock,
                                                 auth_api_client, datamesh,
                                                 event_loop):
    lm1, lm2, relationship = datamesh
    factories.JoinRecord(relationship=relationship,
                         record_id=None,
                         record_uuid='19a7f600-74a0-4123-9be5-dfa69aa172cc',
                         related_record_id=1,
                         related_record_uuid=None)

    url = f'/async/{lm1.endpoint_name}/siteprofiles/'

    # mock requests
    with open(os.path.join(CURRENT_PATH, 'fixtures/swagger_location.json'),
              'rb') as r:
        swagger_location_body = r.read()
    with open(os.path.join(CURRENT_PATH, 'fixtures/swagger_documents.json'),
              'rb') as r:
        swagger_documents_body = r.read()
    with open(
            os.path.join(CURRENT_PATH, 'fixtures/data_list_siteprofile.json'),
            'rb') as r:
        data_location_body = r.read()
    with open(os.path.join(CURRENT_PATH, 'fixtures/data_detail_document.json'),
              'rb') as r:
        data_documents_body = r.read()
    responses = [
        AiohttpResponseMock(method='GET',
                            url=f'{lm1.endpoint}/docs/swagger.json',
                            status=200,
                            body=swagger_location_body,
                            headers={'Content-Type': 'application/json'}),
        AiohttpResponseMock(method='GET',
                            url=f'{lm2.endpoint}/docs/swagger.json',
                            status=200,
                            body=swagger_documents_body,
                            headers={'Content-Type': 'application/json'}),
        AiohttpResponseMock(method='GET',
                            url=f'{lm1.endpoint}/siteprofiles/',
                            status=200,
                            body=data_location_body,
                            headers={'Content-Type': 'application/json'}),
        AiohttpResponseMock(method='GET',
                            url=f'{lm2.endpoint}/documents/1/',
                            status=200,
                            body=data_documents_body,
                            headers={'Content-Type': 'application/json'}),
    ]
    client_session_mock.return_value = create_aiohttp_session_mock(
        responses, loop=event_loop)

    # make api request
    response = auth_api_client.get(url, {'join': 'true'})

    assert response.status_code == 200
    assert response.has_header('Content-Type')
    assert response.get('Content-Type') == 'application/json'
    data = response.json()

    # first item in the list has a joined record
    item1 = data["results"][0]
    assert relationship.key in item1
    assert len(item1[relationship.key]) == 1
    assert item1[relationship.key][0]['id'] == 1

    # second item in the list doesn't have a joined record
    item2 = data["results"][1]
    assert relationship.key in item2
    assert len(item2[relationship.key]) == 0