Skip to content

Commit

Permalink
Fix: Verify and Blacklist custom response schema (#72)
Browse files Browse the repository at this point in the history
* fixed verify and blacklist response schema #71

* 5.3.1
  • Loading branch information
eadwinCode authored Apr 27, 2024
1 parent 60aace7 commit 1dd21a3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ninja_jwt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Django Ninja JWT - JSON Web Token for Django-Ninja"""

__version__ = "5.3.0"
__version__ = "5.3.1"
12 changes: 6 additions & 6 deletions ninja_jwt/controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from asgiref.sync import sync_to_async
from ninja import Schema
from ninja_extra import ControllerBase, api_controller, http_post
from ninja_extra.permissions import AllowAny

Expand Down Expand Up @@ -31,19 +30,20 @@ class TokenVerificationController(ControllerBase):

@http_post(
"/verify",
response={200: Schema},
response={200: schema.verify_schema.get_response_schema()},
url_name="token_verify",
)
def verify_token(self, token: schema.verify_schema):
return token.to_response_schema()
asas = token.to_response_schema()
return asas


class TokenBlackListController(ControllerBase):
auto_import = False

@http_post(
"/blacklist",
response={200: Schema},
response={200: schema.blacklist_schema.get_response_schema()},
url_name="token_blacklist",
)
def blacklist_token(self, refresh: schema.blacklist_schema):
Expand Down Expand Up @@ -114,7 +114,7 @@ class NinjaJWTSlidingController(
class AsyncTokenVerificationController(TokenVerificationController):
@http_post(
"/verify",
response={200: Schema},
response={200: schema.verify_schema.get_response_schema()},
url_name="token_verify",
)
async def verify_token(self, token: schema.verify_schema):
Expand All @@ -126,7 +126,7 @@ class AsyncTokenBlackListController(TokenBlackListController):

@http_post(
"/blacklist",
response={200: Schema},
response={200: schema.blacklist_schema.get_response_schema()},
url_name="token_blacklist",
)
async def blacklist_token(self, refresh: schema.blacklist_schema):
Expand Down
3 changes: 1 addition & 2 deletions ninja_jwt/routers/blacklist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ninja import Schema
from ninja.router import Router

from ninja_jwt.schema_control import SchemaControl
Expand All @@ -11,7 +10,7 @@

@blacklist_router.post(
"/blacklist",
response={200: Schema},
response={200: schema.blacklist_schema.get_response_schema()},
url_name="token_blacklist",
auth=None,
)
Expand Down
3 changes: 1 addition & 2 deletions ninja_jwt/routers/verify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ninja import Schema
from ninja.router import Router

from ninja_jwt.schema_control import SchemaControl
Expand All @@ -11,7 +10,7 @@

@verify_router.post(
"/verify",
response={200: Schema},
response={200: schema.verify_schema.get_response_schema()},
url_name="token_verify",
auth=None,
)
Expand Down
23 changes: 21 additions & 2 deletions tests/test_custom_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,22 @@ def get_response_schema(cls):


class MyTokenVerifyInputSchema(TokenVerifyInputSchema):
pass
@classmethod
def get_response_schema(cls):
class NewResponseSchema(Schema):
refresh: str
access: str
user: dict

return NewResponseSchema

def to_response_schema(self):
values = {
"refresh": "your_refresh_token_here",
"access": self.token,
"user": {},
}
return values


class MyTokenBlacklistInputSchema(TokenBlacklistInputSchema):
Expand Down Expand Up @@ -330,7 +345,11 @@ def test_it_should_return_200_if_everything_okay(self, monkeypatch):
"/verify", json={"token": str(token)}, content_type="application/json"
)
assert res.status_code == 200
assert res.json() == {}
data = res.json()

assert "refresh" in data
assert "user" in data
assert "access" in data


@pytest.mark.django_db
Expand Down

0 comments on commit 1dd21a3

Please sign in to comment.