def test_readonly(json_validation_spec_dir, spec): app = build_app_from_fixture(json_validation_spec_dir, spec, validate_responses=True) app_client = app.app.test_client() res = app_client.get('/v1.0/user') # type: flask.Response assert res.status_code == 200 assert json.loads(res.data.decode()).get('user_id') == 7 res = app_client.post( '/v1.0/user', data=json.dumps({ 'name': 'max', 'password': '******' }), content_type='application/json') # type: flask.Response assert res.status_code == 200 assert json.loads(res.data.decode()).get('user_id') == 8 res = app_client.post( '/v1.0/user', data=json.dumps({ 'user_id': 9, 'name': 'max' }), content_type='application/json') # type: flask.Response assert res.status_code == 400
def test_multipart_form_json(json_validation_spec_dir, spec): app = build_app_from_fixture(json_validation_spec_dir, spec, validate_responses=True) app_client = app.app.test_client() res = app_client.post('/v1.0/multipart_form_json', data={'x': json.dumps({"name": "joe", "age": 20})}, content_type='multipart/form-data') assert res.status_code == 200 assert json.loads(res.data.decode())['name'] == "joe-reply" assert json.loads(res.data.decode())['age'] == 30
def simple_app_with_custom_format_converters(schema_file): def date_converter(_type, _format, value): assert _type == 'string' assert _format == 'date' return datetime.strptime('2011-01-21', '%Y-%m-%d').date() return build_app_from_fixture( 'simple', schema_file, validate_responses=True, format_converters={'string': { 'date': date_converter }})
def test_readonly(json_validation_spec_dir, spec): app = build_app_from_fixture(json_validation_spec_dir, spec, validate_responses=True) app_client = app.app.test_client() res = app_client.get('/v1.0/user') # type: flask.Response assert res.status_code == 200 assert json.loads(res.data.decode()).get('user_id') == 7 res = app_client.post('/v1.0/user', data=json.dumps({'name': 'max', 'password': '******'}), content_type='application/json') # type: flask.Response assert res.status_code == 200 assert json.loads(res.data.decode()).get('user_id') == 8 res = app_client.post('/v1.0/user', data=json.dumps({'user_id': 9, 'name': 'max'}), content_type='application/json') # type: flask.Response assert res.status_code == 400
def test_readonly(json_datetime_dir, spec): app = build_app_from_fixture(json_datetime_dir, spec, validate_responses=True) app_client = app.app.test_client() res = app_client.get('/v1.0/' + spec.replace('yaml', 'json')) assert res.status_code == 200, "Error is {}".format(res.data) spec_data = json.loads(res.data.decode()) if spec == 'openapi.yaml': response_path = 'responses.200.content.application/json.schema' else: response_path = 'responses.200.schema' def get_value(data, path): for part in path.split('.'): data = data.get(part) assert data, "No data in part '{}' of '{}'".format(part, path) return data example = get_value( spec_data, 'paths./datetime.get.{}.example.value'.format(response_path)) assert example in [ '2000-01-23T04:56:07.000008+00:00', # PyYAML 5.3+ '2000-01-23T04:56:07.000008Z' ] example = get_value( spec_data, 'paths./date.get.{}.example.value'.format(response_path)) assert example == '2000-01-23' example = get_value( spec_data, 'paths./uuid.get.{}.example.value'.format(response_path)) assert example == 'a7b8869c-5f24-4ce0-a5d1-3e44c3663aa9' res = app_client.get('/v1.0/datetime') assert res.status_code == 200, "Error is {}".format(res.data) data = json.loads(res.data.decode()) assert data == {'value': '2000-01-02T03:04:05.000006Z'} res = app_client.get('/v1.0/date') assert res.status_code == 200, "Error is {}".format(res.data) data = json.loads(res.data.decode()) assert data == {'value': '2000-01-02'} res = app_client.get('/v1.0/uuid') assert res.status_code == 200, "Error is {}".format(res.data) data = json.loads(res.data.decode()) assert data == {'value': 'e7ff66d0-3ec2-4c4e-bed0-6e4723c24c51'}
def test_writeonly(json_validation_spec_dir, spec): app = build_app_from_fixture(json_validation_spec_dir, spec, validate_responses=True) app_client = app.app.test_client() res = app_client.post('/v1.0/user', data=json.dumps({'name': 'max', 'password': '******'}), content_type='application/json') # type: flask.Response assert res.status_code == 200 assert 'password' not in json.loads(res.data.decode()) res = app_client.get('/v1.0/user') # type: flask.Response assert res.status_code == 200 assert 'password' not in json.loads(res.data.decode()) res = app_client.get('/v1.0/user_with_password') # type: flask.Response assert res.status_code == 500 assert json.loads(res.data.decode())['title'] == 'Response body does not conform to specification'
def test_default_query_param_does_not_match_defined_type( default_param_error_spec_dir): with pytest.raises(SwaggerValidationError): build_app_from_fixture(default_param_error_spec_dir, validate_responses=True, debug=False)
def test_default_query_param_does_not_match_defined_type( default_param_error_spec_dir): with pytest.raises(InvalidSpecification): build_app_from_fixture(default_param_error_spec_dir)
def test_default_query_param_does_not_match_defined_type( default_param_error_spec_dir): with pytest.raises(InvalidSpecification): build_app_from_fixture(default_param_error_spec_dir, validate_responses=True, debug=False)