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_no_field(html): body = html.serialize( Link( href="/users/", allow=["POST"], description="Posting to this endpoint creates a new user", )).decode("UTF-8") assert "POST" in body assert "field" not in body
def test_html_serializer_link(html): body = html.serialize( Link( href="/users/", allow=["POST"], description="Posting to this endpoint creates a new user", fields=[Field(name="username")], )) assert "POST" in body.decode("UTF-8")
def test_hal_serializer_link(hal_serializer): body = hal_serializer( Link( href="/users/", allow=["POST"], description="POSTing to this endpoint creates a new user", fields=[Field(name="username")], )) assert body == { "href": "/users/", }
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_link_not_required(coreapi_serializer): body = coreapi_serializer( Link( href="/users/", allow=["POST"], description="POSTing to this endpoint creates a new user", )) assert body == { "_type": "link", "url": "/users/", "description": "POSTing to this endpoint creates a new user", "action": "post", }
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_coreapi_serializer_link_with_no_allow(coreapi_serializer): body = coreapi_serializer( Link( href="/users/", description="POSTing to this endpoint creates a new user", fields=[Field(name="username")], )) assert body == { "_type": "link", "url": "/users/", "description": "POSTing to this endpoint creates a new user", "fields": [{ "name": "username" }], }
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_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_href_href_template(): with pytest.raises(ValueError): Link(href="/", href_template="/")