def test_json_serializer(): assert json_serialize([]) == [] assert json_serialize("Hello world") == "Hello world" assert json_serialize(Document(url="/", title="Test", content={})) == {} assert json_serialize(Link(href="/link/", allow=["POST"], title="Foo")) == "/link/" assert ( json_serialize( Field(name="username", required=False, description="Just a login") ) == "username" )
def test_coreapi_serializer_small_document(coreapi_serializer): body = coreapi_serializer( Document(url="/users/", title="Users", content={"users": ["1", "2", "3"]})) assert body == { "_type": "document", "_meta": { "url": "/users/", "title": "Users" }, "users": ["1", "2", "3"], }
def test_json_home(): body = JSONHomeSerializer().serialize( Document( title="Identification Provider", links={ "create_token": Link( "/jwt/", title="jwt", allow=["GET", "POST"], formats={"application/coreapi+json": {}}, description="Create a new JSON Web Token.", fields=[ Field("username", required=True), Field("password", required=True), ], ), "users": Link( "/users/", allow=["GET", "POST", "PATCH"], formats={"application/coreapi+json": {}}, fields=[ Field("username", required=True), Field("password", required=True), Field("email", required=True), ], ), }, ), base_url="http://localhost:8000/", ) assert json.loads(body.decode("UTF-8")) == { "api": {"title": "Identification Provider"}, "resources": { "create_token": { "href": "http://localhost:8000/jwt/", "hints": { "allow": ["GET", "POST"], "formats": {"application/coreapi+json": {}}, }, }, "users": { "href": "http://localhost:8000/users/", "hints": { "allow": ["GET", "POST", "PATCH"], "formats": {"application/coreapi+json": {}}, }, }, }, }
def test_href_template(hal_serializer): body = hal_serializer( Document(url="/", links={"comments": Link(href_template="/comments{/id}/")})) assert body == { "_links": { "self": { "href": "/" }, "comments": { "href": "/comments{/id}/", "templated": True }, } }
def test_hal_serializer_full_document(hal_serializer): body = hal_serializer( Document( url="/users/", title="Users", content={"users": []}, links={ "register_user": Link( href="/users/", allow=["POST"], title="Register a new user", description="POSTing to this endpoint creates a new user", fields=[ Field( name="username", required=True, schema={ "type": "string", "minLength": 3 }, ), Field( name="password", required=True, schema={ "type": "string", "minLength": 5, "format": "password", }, ), ], ), }, )) assert body == { "_links": { "self": { "href": "/users/", "title": "Users" }, "register_user": { "href": "/users/", "title": "Register a new user" }, }, "users": [], }
def test_href_template(): body = JSONHomeSerializer().serialize( Document( url="/", title="Articles", links={ "comments": Link( href_template="/comments{/id}/", href_vars={"id": "/params/id/"} ) }, ) ) assert json.loads(body.decode("UTF-8")) == { "api": {"title": "Articles"}, "resources": { "comments": { "hrefTemplate": "/comments{/id}/", "hrefVars": {"id": "/params/id/"}, }, }, }
def test_type(hal_serializer): body = hal_serializer( Document( url="/", links={ "comments": Link(href_template="/comments{/id}/", formats=["application/hal+json"]) }, )) assert body == { "_links": { "self": { "href": "/" }, "comments": { "href": "/comments{/id}/", "templated": True, "type": "application/hal+json", }, } }
def test_html_serializer_full_document(html): body = html.serialize( Document( url="/users/", title="Users", content={"users": []}, links={ "register_user": Link( href="/users/", allow=["POST"], title="Register a new user", description="POSTing to this endpoint creates a new user", fields=[ Field( name="username", required=True, schema={ "type": "string", "minLength": 3 }, ), Field( name="password", required=True, schema={ "type": "string", "minLength": 5, "format": "password", }, ), ], ), }, )).decode("UTF-8") assert "Register a new user" in body assert "password" in body
def test_json_serializer_full_document(): basejson = serializers.serializers["application/json"] body = basejson.serialize( Document( url="/users/", title="Users", content={"users": []}, links={ "register_user": Link( href="/users/", allow=["POST"], title="Register a new user", description="POSTing to this endpoint creates a new user", fields=[ Field( name="username", required=True, schema={"type": "string", "minLength": 3}, ), Field( name="password", required=True, schema={ "type": "string", "minLength": 5, "format": "password", }, ), ], ), }, ) ) assert json.loads(body.decode("UTF-8")) == { "register_user": "******", "users": [], }
def test_html_serializer_small_document(html): body = html.serialize( Document(url="/users/", title="Users", content={"users": ["1", "2", "3"]})) assert "Users" in body.decode("UTF-8")
def test_warn_if_missing_self(hal_serializer): with pytest.warns(UserWarning): hal_serializer(Document())
def test_no_link(): body = JSONHomeSerializer().serialize(Document(title="Articles")) assert json.loads(body.decode("UTF-8")) == { "api": {"title": "Articles"}, "resources": {}, }
def test_coreapi_serializer_full_document(coreapi_serializer): body = coreapi_serializer( Document( url="/users/", title="Users", content={"users": []}, links={ "register_user": Link( href="/users/", allow=["POST"], title="Register a new user", description="POSTing to this endpoint creates a new user", fields=[ Field( name="username", required=True, schema={ "type": "string", "minLength": 3 }, ), Field( name="password", required=True, schema={ "type": "string", "minLength": 5, "format": "password", }, ), ], ), }, )) assert body == { "_type": "document", "_meta": { "url": "/users/", "title": "Users" }, "users": [], "register_user": { "_type": "link", "url": "/users/", "title": "Register a new user", "description": "POSTing to this endpoint creates a new user", "action": "post", "fields": [ { "name": "username", "required": True, "schema": { "type": "string", "minLength": 3 }, }, { "name": "password", "required": True, "schema": { "type": "string", "minLength": 5, "format": "password" }, }, ], }, }
def test_serialize_function(): assert serializers.serialize( Document(), accept_header="application/json", hostname="http://127.0.0.1:8000", ) == ("application/json", b"{}")