def test_validate_request_against_schema_many_errors_has_nice_output(self):
        """
        validate_request_against_schema() raises multiple types of (sub)errors
        """
        self.request_data = {'x': 1, 'y': [{}, 3, "foo"]}
        self.query_data = {
            'optional_param': 'not a number',
            'date_param': 'not a date',
            'not_defined_param': 'dummy'
        }
        req_info = Data(
            path=self.req_path_info,
            method=self.method,
            request=Request(self.request_data, self.query_data),
            response=self.dummy_response,
        )
        error_indicator = '-'
        suberror_indicator = '>'  # for sub schema errors
        stage_indicator = '...'  # once each for query and data validation

        try:
            validate_request_against_schema(req_info, self.spec)
        except JSONSchemaValidationError as e:
            self.assertGreater(str(e).count(error_indicator), 1)
            self.assertGreater(str(e).count(suberror_indicator), 1)
            self.assertGreater(str(e).count(stage_indicator), 1)
        else:
            self.fail("Did not raise JSONSchemaValidationError.")
 def test_get_spec_found_with_extended_path(self):
     req_path_info = '/api/v5/test/fake_id:1234567890/extended'
     method = 'long'
     request_info = Data(
         path=req_path_info,
         method=method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     spec = get_spec(request_info, self.raw_schema)
     self.assertEqual(spec['method'], method)
 def test_validate_request_against_schema_undefined_param(self):
     """
     validate_request_against_schema() raises for undefined schema field
     """
     self.query_data = {'required_param': "dummy", 'not_defined': "dummy"}
     req_info = Data(path=self.req_path_info,
                     method=self.method,
                     request=Request(self.request_data, self.query_data),
                     response=self.dummy_response)
     with self.assertRaises(JSONSchemaValidationError):
         validate_request_against_schema(req_info, self.spec)
 def test_get_spec_success_without_endpoint_path(self):
     req_path_info = '/api/v5/test'
     method = 'short'
     request_info = Data(
         path=req_path_info,
         method=method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     spec = get_spec(request_info, self.raw_schema)
     self.assertEqual(spec['method'], method)
 def test_get_spec_raises_when_resource_not_found(self):
     """
     get_spec() raises UndefinedSchemaError
     """
     req_path_info = '/api/v5/not_test/'
     request_info = Data(
         path=req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     with self.assertRaises(UndefinedSchemaError):
         get_spec(request_info, self.raw_schema)
 def test_get_spec_raises_when_method_not_found(self):
     """
     get_spec() raises UndefinedSchemaError
     """
     method = 'YAR'
     request_info = Data(
         path=self.req_path_info,
         method=method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     with self.assertRaises(UndefinedSchemaError):
         get_spec(request_info, self.raw_schema)
 def test_validate_request_against_schema_missing_required_query(self):
     """
     validate_request_against_schema() raise on missing required query param
     """
     self.query_data = {'optional_param': 1}
     req_info = Data(
         path=self.req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     with self.assertRaises(JSONSchemaValidationError):
         validate_request_against_schema(req_info, self.spec)
 def test_validate_request_against_schema_raises_with_invalid_data(self):
     """
     validate_request_against_schema() JSONSchemaValidationError on bad data
     """
     self.request_data = {'x': 1}
     req_info = Data(
         path=self.req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     with self.assertRaises(JSONSchemaValidationError):
         validate_request_against_schema(req_info, self.spec)
 def setUp(self):
     super(RequestValidatorTestCase, self).setUp()
     self.req_path_info = '/api/v5/test/'
     self.method = 'post'
     self.request_data = {'x': 'data'}
     self.query_data = {'required_param': 'a string', 'optional_param': 1}
     self.dummy_response = Response(response_data={}, status_code=200)
     self.parsed_data = Data(
         path=self.req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     self.spec = get_spec(self.parsed_data, self.raw_schema)
 def test_validate_request_against_schema_numbers_fallback_to_string(self):
     """
     validate_request_against_schema() quiet on valid list param instance
     """
     # check ValidationError is not thrown
     self.query_data = {
         'required_param': 1,
     }
     req_info = Data(
         path=self.req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     validate_request_against_schema(req_info, self.spec)
 def setUp(self):
     super(ResponseValidatorTestCase, self).setUp()
     self.req_path_info = '/api/v5/test/'
     self.method = 'post'
     self.dummy_request = Request(request_data={}, query_data={})
     self.response_data = {'data': {'uni': '¶'}}
     self.status_code = 200
     self.parsed_data = Data(
         path=self.req_path_info,
         method=self.method,
         request=self.dummy_request,
         response=Response(response_data=self.response_data,
                           status_code=self.status_code),
     )
     self.spec = get_spec(self.parsed_data, self.raw_schema)
 def test_validate_request_against_schema_quiet_on_valid_list_param(self):
     """
     validate_request_against_schema() quiet on valid list param instance
     """
     # check ValidationError is not thrown
     self.query_data = {
         'required_param': 'a string',
         'optional_param': [1, 2, 3, 4, 5]
     }
     req_info = Data(
         path=self.req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     validate_request_against_schema(req_info, self.spec)
 def test_validate_request_against_schema_raises_with_invalid_query(self):
     """
     validate_request_against_schema() JSONSchemaValidationError on bad query
     """
     self.query_data = {
         'required_param': "dummy",
         'not_defined': "should be num"
     }
     req_info = Data(
         path=self.req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     with self.assertRaises(JSONSchemaValidationError):
         validate_request_against_schema(req_info, self.spec)
 def test_validate_request_against_schema_invalid_date_list_param(self):
     """
     validate_request_against_schema() raises on invalid date list param
     """
     # check ValidationError is not thrown
     self.query_data = {
         'required_param': 'a string',
         'date_param': ['2016-06-14T13:58:32+00:00', 'nonsense']
     }
     req_info = Data(
         path=self.req_path_info,
         method=self.method,
         request=Request(self.request_data, self.query_data),
         response=self.dummy_response,
     )
     with self.assertRaises(JSONSchemaValidationError):
         validate_request_against_schema(req_info, self.spec)