def test_enrich_call_success(mock_request, misp_client,
                             success_enrich_expected_payload, route, client,
                             valid_jwt, valid_json):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT
    response = client.post(route,
                           headers=get_headers(valid_jwt()),
                           json=valid_json)
    assert response.status_code == HTTPStatus.OK
    response = response.get_json()
    if route != '/refer/observables':
        if route == '/observe/observables':
            for doc in response['data']['verdicts']['docs']:
                assert doc.pop('judgement_id')
            for doc in response['data']['judgements']['docs']:
                assert doc.pop('valid_time')
                assert doc.pop('id')
            for doc in response['data']['sightings']['docs']:
                assert doc.pop('id')
            for doc in response['data']['relationships']['docs']:
                assert doc.pop('id')
                assert doc.pop('source_ref')

        for doc in response['data']['verdicts']['docs']:
            assert doc.pop('valid_time')
    assert response == success_enrich_expected_payload
def test_call_with_wrong_jwt_structure(route, client, wrong_jwt_structure,
                                       authorization_errors_expected_payload):
    response = client.post(route, headers=get_headers(wrong_jwt_structure))

    assert response.status_code == HTTPStatus.OK
    assert response.json == authorization_errors_expected_payload(
        'Wrong JWT structure')
def test_health_call_failure(mock_request, route, client, valid_jwt,
                             misp_client_error,
                             misp_internal_expected_payload):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT
    response = client.post(route, headers=get_headers(valid_jwt()))
    assert response.status_code == HTTPStatus.OK
    assert response.json == misp_internal_expected_payload
def test_call_with_jwt_encoded_by_wrong_key(
        mock_request, route, client, valid_jwt,
        authorization_errors_expected_payload):
    mock_request.return_value = RESPONSE_OF_JWKS_ENDPOINT_WITH_WRONG_KEY
    response = client.post(route, headers=get_headers(valid_jwt()))

    assert response.status_code == HTTPStatus.OK
    assert response.json == authorization_errors_expected_payload(WRONG_KEY)
def test_call_with_missing_jwks_host(mock_request, route, client, valid_jwt,
                                     authorization_errors_expected_payload):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT

    response = client.post(route, headers=get_headers(valid_jwt(jwks_host='')))
    assert response.status_code == HTTPStatus.OK
    assert response.json == authorization_errors_expected_payload(
        JWKS_HOST_MISSING)
def test_call_with_wrong_authorization_type(
        route, client, valid_jwt, authorization_errors_expected_payload):
    response = client.post(route,
                           headers=get_headers(valid_jwt(),
                                               auth_type='wrong_type'))

    assert response.status_code == HTTPStatus.OK
    assert response.json == authorization_errors_expected_payload(
        'Wrong authorization type')
def test_call_with_wrong_kid(mock_request, route, client, valid_jwt,
                             authorization_errors_expected_payload):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT

    response = client.post(route,
                           headers=get_headers(valid_jwt(kid='wrong_kid')))
    assert response.status_code == HTTPStatus.OK
    assert response.json == authorization_errors_expected_payload(
        KID_NOT_FOUND)
def test_enrich_call_with_unsupported_type_json(mock_request, misp_client,
                                                unsupported_type_expected_body,
                                                route, client, valid_jwt,
                                                unsupported_type_json):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT
    response = client.post(route,
                           headers=get_headers(valid_jwt()),
                           json=unsupported_type_json)
    assert response.status_code == HTTPStatus.OK
    assert response.get_json() == unsupported_type_expected_body
def test_enrich_call_with_valid_jwt_but_invalid_json_value(
        mock_request, misp_client, route, client, valid_jwt,
        invalid_json_value, invalid_json_expected_payload):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT
    response = client.post(route,
                           headers=get_headers(valid_jwt()),
                           json=invalid_json_value)
    assert response.status_code == HTTPStatus.OK
    assert response.json == invalid_json_expected_payload(
        "{0: {'value': ['Field may not be blank.']}}")
def test_call_with_wrong_jwt_payload_structure(
        mock_request, route, client, valid_jwt,
        authorization_errors_expected_payload):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT
    response = \
        client.post(route,
                    headers=get_headers(valid_jwt(wrong_structure=True)))

    assert response.status_code == HTTPStatus.OK
    assert response.json == authorization_errors_expected_payload(
        WRONG_PAYLOAD_STRUCTURE)
def test_health_call_success(mock_request, route, client, valid_jwt,
                             misp_client):
    mock_request.return_value = EXPECTED_RESPONSE_OF_JWKS_ENDPOINT
    response = client.post(route, headers=get_headers(valid_jwt()))
    assert response.status_code == HTTPStatus.OK
    assert response.json == {'data': {'status': 'ok'}}