def test_bare_class_schemas_handled(self, foo_cls, foo_list_cls, headers_cls): rebar = Rebar() registry = rebar.create_handler_registry() expected_headers = {"x-name": "Header Name"} def get_foo(*args, **kwargs): return expected_foo def post_foo(*args, **kwargs): return expected_foo register_endpoint( registry=registry, method="GET", path="/my_get_endpoint", headers_schema=headers_cls, response_body_schema={200: foo_cls}, query_string_schema=foo_list_cls, func=get_foo, ) register_endpoint( registry=registry, method="POST", path="/my_post_endpoint", request_body_schema=foo_list_cls, response_body_schema=foo_cls, func=post_foo, ) expected_foo = FooSchema().load({ "uid": "some_uid", "name": "Namey McNamerton" }) app = create_rebar_app(rebar) # violate headers schema: resp = app.test_client().get( path="/my_get_endpoint?name=QuerystringName") self.assertEqual(resp.status_code, 400) self.assertEqual(resp.json["message"], messages.header_validation_failed.message) # violate querystring schema: resp = app.test_client().get(path="/my_get_endpoint", headers=expected_headers) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.json["message"], messages.query_string_validation_failed.message) # valid request: resp = app.test_client().get( path="/my_get_endpoint?name=QuerystringName", headers=expected_headers) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.json, expected_foo) resp = app.test_client().post( path="/my_post_endpoint", data='{"wrong": "Posted Name"}', content_type="application/json", ) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.json["message"], messages.body_validation_failed.message) resp = app.test_client().post( path="/my_post_endpoint", data='{"name": "Posted Name"}', content_type="application/json", ) self.assertEqual(resp.status_code, 200) # ensure Swagger generation doesn't break (Issue #115) from flask_rebar import SwaggerV2Generator, SwaggerV3Generator swagger = SwaggerV2Generator().generate(registry) self.assertIsNotNone(swagger) # really only care that it didn't barf swagger = SwaggerV3Generator().generate(registry) self.assertIsNotNone(swagger)
from flask_rebar import ( Rebar, errors, HeaderApiKeyAuthenticator, Tag, SwaggerV2Generator, ) from flask_rebar.validation import RequestSchema, ResponseSchema rebar = Rebar() # Rebar will create a default swagger generator if none is specified. # However, if you want more control over how the swagger is generated, you can # provide your own. # Here we've specified additional metadata for operation tags. generator = SwaggerV2Generator( tags=[Tag(name="todo", description="Operations for managing TODO items.")]) registry = rebar.create_handler_registry(swagger_generator=generator) # Just a mock database, for demonstration purposes todo_id_sequence = 0 todo_database = {} # Rebar relies heavily on Marshmallow. # These schemas will be used to validate incoming data, marshal outgoing # data, and to automatically generate a Swagger specification. class CreateTodoSchema(RequestSchema): complete = fields.Boolean(required=True) description = fields.String(required=True)