"""JSON Validator Tests. Testing the logic for parsing RAML schemas and constructing validators """ import flask import jsonschema from product_digital import config from product_digital.constants import header as header_constants from product_digital.validation.schema import json_validators app = flask.Flask(__name__) @app.route('/test_header', methods=['GET']) @app.route('/test_query_params', methods=['GET']) @app.route('/test_body', methods=['POST']) def _test_handler(): pass def test_raml_header_to_json_schema(): """Test method that converts RAML parsed headers. Should be converted into JSON schema for our Draft3Validator class. """ response = json_validators.raml_header_to_json_schema( config.API_TEST_DEFINITION.resources[ '/test_header'].methods['get'].headers) assert response.get('properties').get( header_constants.CORRELATION_ID).get('type') == 'string' assert response.get('properties').get( header_constants.CONTENT_TYPE).get('type') == 'string' assert response.get('properties').get( header_constants.GRASS_ACCOUNT_TYPE).get('type') == 'string' assert response.get('properties').get( header_constants.GRASS_ACCOUNT_ID).get('type') == 'string' def test_raml_header_to_json_schema_load_draft3_validator(): """Test that the converted RAML to JSON Schema loads. Should be loaded into a Draft3Validator, and that it didn't load any properties set to None """ schema = json_validators.raml_header_to_json_schema( config.API_TEST_DEFINITION.resources[ '/test_header'].methods['get'].headers) # we know we didn't set the pattern property of Correlation-Id validator = jsonschema.Draft3Validator(schema) assert validator.schema.get('properties').get('Correlation-Id').\ get('pattern') is None def test_get_method_spec(monkeypatch): """Test that the expected method spec is returned.""" monkeypatch.setattr(config, 'API_DEFINITION', config.API_TEST_DEFINITION) with app.test_request_context('/test_header', method='GET'): method_spec = json_validators.get_method_spec() expected_spec = config.API_TEST_DEFINITION.resources[ '/test_header'].methods['get'] assert method_spec == expected_spec def test_query_args_validator_with_invalid_args(monkeypatch): """Test that validator is invalid. Should be invalid when passed query params that do not match the RAML spec. """ monkeypatch.setattr(config, 'API_DEFINITION', config.API_TEST_DEFINITION) with app.test_request_context('/test_query_params', method='GET'): args_validator = json_validators.query_args_validator() invalid_args = {'page_limit': 'a bajillion'} assert args_validator.is_valid(invalid_args) is False def test_query_args_validator_with_valid_args(monkeypatch): """Test that validator is valid. Shoud be valid when passed query params that do match the RAML spec. """ monkeypatch.setattr(config, 'API_DEFINITION', config.API_TEST_DEFINITION) with app.test_request_context('/test_query_params', method='GET'): args_validator = json_validators.query_args_validator() valid_args = {'page_limit': '100', 'page_offset': '50'} assert args_validator.is_valid(valid_args) is True def test_headers_validator_with_invalid_args(monkeypatch): """Test that validator is invalid. Should be invalid when passed headers that do not match the RAML spec. """ monkeypatch.setattr(config, 'API_DEFINITION', config.API_TEST_DEFINITION) with app.test_request_context('/test_header', method='GET'): headers_validator = json_validators.headers_validator() invalid_headers = {'Grass-Account-Id': 'five hundred plus infinity'} assert headers_validator.is_valid(invalid_headers) is False def test_headers_validator_with_valid_args(monkeypatch): """Test that validator is valid when passed headers matching RAML spec.""" monkeypatch.setattr(config, 'API_DEFINITION', config.API_TEST_DEFINITION) with app.test_request_context('/test_header', method='GET'): headers_validator = json_validators.headers_validator() valid_headers = { 'Correlation-Id': 'abc-123', 'Content-Type': 'application/json', 'Grass-Account-Id': '12345', 'Grass-Account-Type': 'vendor'} assert headers_validator.is_valid(valid_headers) is True def test_body_validator_with_invalid_body(monkeypatch): """Test that validator is invalid. Should be invalid when passed body content that does not match the RAML spec. """ monkeypatch.setattr(config, 'API_DEFINITION', config.API_TEST_DEFINITION) with app.test_request_context('/test_body', method='POST'): body_validator = json_validators.body_validator() invalid_body = {'name': 123, 'artist_type': 'a bad one'} assert body_validator.is_valid(invalid_body) is False def test_body_validator_with_valid_body(monkeypatch): """Test that validator is valid. Should be valid when passed body content that matches the RAML spec. """ monkeypatch.setattr(config, 'API_DEFINITION', config.API_TEST_DEFINITION) with app.test_request_context('/test_body', method='POST'): body_validator = json_validators.body_validator() valid_body = {'name': 'Somebody', 'artist_type': 'artist'} assert body_validator.is_valid(valid_body) is True