Пример #1
0
def test_invalid_event_type():

    event = create_api_event("/events", body={"type": "not_there"})

    response = app(event, context)

    assert response["statusCode"] == codes.bad_request
Пример #2
0
def test_bad_request_correctly_handled():

    event = create_api_event('/bad_request', body={'sample': 'field'})

    response = sample_app(event, context)

    assert response['statusCode'] == codes.bad_request
    assert 'Not good mah friend. Something terrible has happened!' in response['body']
Пример #3
0
def test_route_with_int_invalid_params(year, month):

    event = create_api_event('/articles/{year}/{month}/',
                                method='GET',
                                pathParameters={'year': year, 'month': month})

    response = sample_app(event, context)
    assert response['statusCode'] == codes.not_found
Пример #4
0
def test_sample_view_route_does_not_match():

    event = create_api_event('/router/mismatch',
                                pathParameters={'first': 'adventure'},
                                body={'request_id': 'navigate_request_id', 'urls': '', 'term': ''})

    response = sample_app(event, context)

    assert response['statusCode'] == codes.not_found
Пример #5
0
def test_sample_view_with_undefined_headers():

    event = create_api_event('/findme/{first}/{second}',
                                pathParameters={'first': 'adventure', 'second': 'chile'},
                                body={'request_id': 'navigate_request_id', 'urls': '', 'term': ''})

    event['headers'] = None
    response = sample_app(event, context)

    assert response['statusCode'] == codes.ok
Пример #6
0
def test_url_verification_request_without_challenge():
    """
    If a url_verification request does not have a
    valid challenge, a bad request is returned
    """
    event = create_api_event("/events", body={"type": "url_verification"})

    response = app(event, context)

    assert response["statusCode"] == codes.bad_request
Пример #7
0
def test_route_with_int_valid():

    event = create_api_event('/articles/{year}/{month}/',
                                method='GET',
                                pathParameters={'year': '2020', 'month': '10'})

    response = sample_app(event, context)
    expected_response = get_articles_view(2020, 10)

    assert response['body'] == json.dumps(expected_response)
Пример #8
0
 def _get_request_for_user(self, user):
     return create_api_event(
         "/events",
         body={
             "type": "event_callback",
             "event": {
                 "type": "user_change",
                 "user": user
             },
         },
     )
Пример #9
0
def test_post_custom_response():

    event = create_api_event('/events/{zip_code}',
                             method='POST',
                             pathParameters={'zip_code': 20902})

    response = sample_app(event, context)

    assert response['body'] == json.dumps({'findme': True, 'zip': 20902})
    assert response['headers'] == {'Content-Type': 'application/json'}
    assert response['statusCode'] == codes.partial
Пример #10
0
def test_routing_for_decorated_views(http_method):
    """
    Validate that a view defined for a GET request is correctly evaluated when
    the route + method match the signature.
    """

    event = create_api_event('/pacific_resource', method=http_method)

    response = sample_app(event, context)

    assert json.loads(response['body'])['action'] == http_method
Пример #11
0
def test_custom_re_in_route_invalid(item_id):
    """
    Validate the uiid based view with invalid values.
    """

    event = create_api_event('/item/{item_id}/',
                                method='GET',
                                pathParameters={'item_id': item_id})

    response = sample_app(event, context)
    assert response['statusCode'] == codes.not_found
Пример #12
0
def test_get_custom_response():

    event = create_api_event('/events/{zip_code}',
                             method='GET',
                             pathParameters={'zip_code': 20902})

    response = sample_app(event, context)

    assert response['body'] == 'hello'
    assert response['headers'] == {'Content-Type': 'text/html'}
    assert response['statusCode'] == codes.ok
Пример #13
0
def test_sample_view_with_path_params():

    event = create_api_event('/findme/{first}/{second}',
                                pathParameters={'first': 'adventure', 'second': 'chile'},
                                body={'request_id': 'navigate_request_id', 'urls': '', 'term': ''})

    view_body = sample_view('adventure', 'chile')
    response = sample_app(event, context)

    assert response['body'] == json.dumps(view_body)
    assert response['statusCode'] == codes.ok
Пример #14
0
def test_empty_query_params():
    """
    If there are no query parameters provided, the default value should be {}. The
    view should not throw an exception.
    """
    event = create_api_event('/empty_params', queryParameters=None)

    response = sample_app(event, context)

    assert response['statusCode'] == codes.ok
    assert json.loads(response['body']) == {'value': None}
Пример #15
0
def test_custom_field_in_route_invalid(tracker_name):
    """
    Custom route field with invalid values.
    """

    event = create_api_event('/tracker/{name}/',
                                method='GET',
                                pathParameters={'name': tracker_name})

    response = sample_app(event, context)
    assert response['statusCode'] == codes.not_found
Пример #16
0
def test_uuid_in_route_invalid(product_id):
    """
    Validate the uiid based view with invalid values.
    """

    event = create_api_event('/product/{product_id}/',
                                method='GET',
                                pathParameters={'product_id': product_id})

    response = sample_app(event, context)
    assert response['statusCode'] == codes.not_found
Пример #17
0
def test_view_without_path_parameters():
    """
    Validate that a view without uri paramters does not crash when invoked.
    """

    test_body = {'field': 'value', 'expected': [2, 3, 4, 5]}
    event = create_api_event('/echo', body=test_body, pathParameters=None)

    response = sample_app(event, context)

    assert response['statusCode'] == codes.ok
    assert json.loads(response['body']) == test_body
Пример #18
0
def test_custom_re_in_route_valid(item_id):
    """
    Validate a uuid based parameter with a valid value.
    """

    event = create_api_event('/item/{item_id}/',
                                method='GET',
                                pathParameters={'item_id': item_id})

    response = sample_app(event, context)

    assert json.loads(response['body'])['id'] == item_id
Пример #19
0
def test_uuid_in_route_valid():
    """
    Validate a uuid based parameter with a valid value.
    """

    pid = '00010203-0405-0607-0809-0a0b0c0d0e0f'
    event = create_api_event('/product/{product_id}/',
                                method='GET',
                                pathParameters={'product_id': pid})

    response = sample_app(event, context)

    assert json.loads(response['body'])['id'] == pid
Пример #20
0
def test_route_defined_for_duplicate_views():
    """
    This is an invalid definition in which the user of minik is trying to associate
    two different views for the same (path, method) pair.
    """

    event = create_api_event('/event_list',
                             method='GET',
                             body={'type': 'cycle'})

    response = sample_app(event, context)

    assert response['statusCode'] == codes.not_allowed
Пример #21
0
def test_route_defined_for_post_put(http_method, expected_message):
    """
    For a given path, minik can execute different routes based on the HTTP method.
    """

    event = create_api_event('/events/{zip_code}',
                             method=http_method,
                             pathParameters={'zip_code': 20902},
                             body={'type': 'cycle'})

    response = sample_app(event, context)

    assert json.loads(response['body'])['message'] == expected_message
Пример #22
0
def test_json_body_in_view():
    """
    Test that a given view has access to the request.json_body. The current
    request contains the data of the requests payload.
    """

    test_body = {'field': 'value', 'expected': [2, 3, 4, 5]}
    event = create_api_event('/echo', body=test_body)

    response = sample_app(event, context)

    assert response['statusCode'] == codes.ok
    assert json.loads(response['body']) == test_body
Пример #23
0
def test_str_route_validation_invalid(username):
    """
    Test string based routing with INVALID paramters. Note that partial matches
    will be rejected. For instance hello@gmail will be rejected given that `@`
    is not part of [a-zA-Z0-9_] pattern.
    """

    event = create_api_event('/bio/{username}',
                                method='GET',
                                pathParameters={'username': username})

    response = sample_app(event, context)
    assert response['statusCode'] == codes.not_found
Пример #24
0
def test_custom_field_in_route_valid(tracker_name):
    """
    Validate that a route with a custom field definition works when valid values
    are provided.
    """

    event = create_api_event('/tracker/{name}/',
                                method='GET',
                                pathParameters={'name': tracker_name})

    response = sample_app(event, context)

    assert json.loads(response['body'])['name'] == tracker_name
Пример #25
0
def test_routing_no_method(http_method):
    """
    A route defined without a set of methods will be invoked for ANY HTTP method.
    """

    event = create_api_event('/activity_no_method',
                             method=http_method,
                             body={
                                 'type': 'cycle',
                                 'distance': 15
                             })

    response = sample_app(event, context)
    assert json.loads(response['body'])['method'] == http_method.lower()
Пример #26
0
def test_url_verification_request():
    """
    With a valid url verification event type, return
    the challenge provided.
    """
    event = create_api_event("/events",
                             body={
                                 "type": "url_verification",
                                 "challenge": "findme"
                             })

    response = app(event, context)

    assert response["statusCode"] == codes.ok
    assert response["body"] == '"findme"'
Пример #27
0
def test_event_without_resoure_raises_error():
    """
    This is a misconfiguration in the integration between the API gateway and the
    lambda function. If the uri is None, that means that the event is not being
    correctly propagated to the lambda function.
    """

    test_body = {'field': 'value', 'expected': [2, 3, 4, 5]}
    event = create_api_event('/echo', body=test_body)
    del event['resource']

    try:
        sample_app(event, context)
    except ConfigurationError as ce:
        assert CONFIG_ERROR_MSG in str(ce)
Пример #28
0
def test_str_route_validation_valid(username):
    """
    The string based route validation will match any valid \w+ regular expression,
    which is used for unicode patterns [a-zA-Z0-9_].

    https://docs.python.org/3/library/re.html
    """

    event = create_api_event('/bio/{username}',
                                method='GET',
                                pathParameters={'username': username})

    response = sample_app(event, context)
    expected_response = get_re_view(username)

    assert response['body'] == json.dumps(expected_response)
Пример #29
0
def test_route_defined_for_post_put_not_called(http_method):
    """
    Using the activity_post_put view definition, validate that the view gets
    correctly executed for the two methods it has in its definition.
    """

    event = create_api_event('/activity_post_put',
                             method=http_method,
                             body={
                                 'type': 'cycle',
                                 'distance': 15
                             })

    response = sample_app(event, context)

    # The given view is not associated with the given HTTP method.
    assert response['statusCode'] == codes.method_not_allowed
Пример #30
0
def test_route_defined_for_post_put(http_method):
    """
    Using the activity_post_put view definition, validate that the view gets
    correctly executed for the two methods it has in its definition.
    """

    event = create_api_event('/activity_post_put',
                             method=http_method,
                             body={
                                 'type': 'cycle',
                                 'distance': 15
                             })

    response = sample_app(event, context)
    expected_response = post_put_view()

    assert response['body'] == json.dumps(expected_response)