Skip to content

Commit

Permalink
tests fix for latest Sanic version
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny_Gavrilin committed Nov 1, 2021
1 parent 74faa3a commit 9351949
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 16 deletions.
4 changes: 3 additions & 1 deletion sanic_jwt/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def __add_endpoints(self):
self.bp.exception(exceptions.SanicJWTException)(
self.responses.exception_response
)

self.app.exception(exceptions.Unauthorized)(
self.responses.exception_response
)
if not self.instance_is_blueprint:
url_prefix = self._get_url_prefix()
self.instance.blueprint(self.bp, url_prefix=url_prefix)
Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ async def retrieve_user_secret(user_id, **kwargs):
def app(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
sanic_app.config.FALLBACK_ERROR_FORMAT = "json"
sanic_jwt = Initialize(sanic_app, authenticate=authenticate)

@sanic_app.route("/")
async def helloworld(request):
return json({"hello": "world"})

@sanic_app.route("/protected")
@sanic_app.route("/protected", error_format="json")
@protected()
async def protected_request(request):
return json({"protected": True})
Expand Down Expand Up @@ -361,7 +362,7 @@ def user2(payload):
extra_verifications=extra_verifications,
)

@sanic_app.route("/protected")
@sanic_app.route("/protected", error_format="json")
@protected()
async def protected_request(request):
return json({"protected": True})
Expand Down
4 changes: 2 additions & 2 deletions tests/test_async_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def get(self, request):


bp = Blueprint("bp")
bp.add_route(Tester.as_view(), "/test", methods=ALL_METHODS)
bp.add_route(Tester.as_view(), "/test", methods=ALL_METHODS, error_format="json")


class CustomAuth(Authentication):
Expand All @@ -53,7 +53,7 @@ def app():
app.config.SANIC_JWT_AUTHORIZATION_HEADER_PREFIX = "JWT"
app.config.SANIC_JWT_EXPIRATION_DELTA = 360000
app.config.SANIC_JWT_USER_ID = "username"

app.config.FALLBACK_ERROR_FORMAT = "json"
sanicjwt = initialize(app, authentication_class=CustomAuth)
app.blueprint(bp)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_wrong_header(app):
def test_tricky_debug_option_true(app):
sanic_app, sanic_jwt = app

@sanic_app.route("/another_protected")
@sanic_app.route("/another_protected", error_format="json")
@sanic_jwt.protected(debug=lambda: True)
def another_protected(request):
return json(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
blueprint = Blueprint("Test", "/test")


@blueprint.get("/", strict_slashes=True)
@blueprint.get("/", strict_slashes=True, error_format="json")
@protected()
def protected_hello_world(request):
return json({"message": "hello world"})
Expand All @@ -19,7 +19,7 @@ async def authenticate(request, *args, **kwargs):


app = Sanic("sanic-jwt-test")

app.config.FALLBACK_ERROR_FORMAT = "json"
app.blueprint(blueprint)

sanicjwt = Initialize(app, authenticate=authenticate)
Expand Down
9 changes: 5 additions & 4 deletions tests/test_endpoints_cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ async def authenticate(request, *args, **kwargs):


sanic_app = Sanic("sanic-jwt-test")
sanic_jwt = Initialize(sanic_app, authenticate=authenticate)
sanic_app.config.FALLBACK_ERROR_FORMAT = "json"

sanic_jwt = Initialize(sanic_app, authenticate=authenticate)

class PublicView(HTTPMethodView):
def get(self, request):
Expand All @@ -66,9 +67,9 @@ async def patch(self, request):
return json({"protected": True})


sanic_app.add_route(PublicView.as_view(), "/")
sanic_app.add_route(ProtectedView.as_view(), "/protected")
sanic_app.add_route(PartiallyProtectedView.as_view(), "/partially")
sanic_app.add_route(PublicView.as_view(), "/", error_format='json')
sanic_app.add_route(ProtectedView.as_view(), "/protected", error_format='json')
sanic_app.add_route(PartiallyProtectedView.as_view(), "/partially", error_format='json')


class TestEndpointsCBV(object):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ async def retrieve_user(request, payload, *args, **kwargs):
secret=secret,
)

@sanic_app.route("/")
@sanic_app.route("/", error_format="json")
async def helloworld(request):
return json({"hello": "world"})

@sanic_app.route("/protected")
@sanic_app.route("/protected", error_format="json")
@protected()
async def protected_request(request):
return json({"protected": True})
Expand Down
1 change: 1 addition & 0 deletions tests/test_endpoints_init_on_bp_and_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def authenticate2(request, *args, **kwargs):
app = Sanic("sanic-jwt-test")


# @app.get("/", strict_slashes=True, error_format="json")
@app.get("/", strict_slashes=True)
@protected()
def protected_hello_world_app(request):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_extra_verifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def bad_return(payload):
extra_verifications=extra_verifications,
)

@sanic_app.route("/protected")
@sanic_app.route("/protected", error_format="json")
@protected()
async def protected_request(request):
return json({"protected": True})
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_extra_verification_non_callable(authenticate):
extra_verifications=extra_verifications,
)

@sanic_app.route("/protected")
@sanic_app.route("/protected", error_format="json")
@protected()
async def protected_request(request):
return json({"protected": True})
Expand Down

0 comments on commit 9351949

Please sign in to comment.