diff --git a/docs/requirements.txt b/docs/requirements.txt index 22aa590..d7e0fe8 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,2 +1,3 @@ sphinx==3.1.2 sphinx_rtd_theme==0.4.2 +Jinja2<3.1 diff --git a/docs/source/conf.py b/docs/source/conf.py index f13a08d..d78bd52 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -54,9 +54,9 @@ # built documents. # # The short X.Y version. -version = u"1.6" +version = u"1.8" # The full version, including alpha/beta/rc tags. -release = u"1.6.0" +release = u"1.8.0" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/sanic_jwt/__init__.py b/sanic_jwt/__init__.py index 1ae3233..bbda315 100644 --- a/sanic_jwt/__init__.py +++ b/sanic_jwt/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.7.0" +__version__ = "1.8.0" __author__ = "Adam Hopkins" __credits__ = "Richard Kuesters" diff --git a/sanic_jwt/authentication.py b/sanic_jwt/authentication.py index 4ca4ea1..772e48b 100644 --- a/sanic_jwt/authentication.py +++ b/sanic_jwt/authentication.py @@ -152,7 +152,6 @@ async def _check_authentication( raise e args = e.args if isinstance(e, SanicJWTException) else [] - raise exceptions.Unauthorized(*args) return is_valid, status, reasons diff --git a/sanic_jwt/initialization.py b/sanic_jwt/initialization.py index 22225ae..ef4ca0f 100644 --- a/sanic_jwt/initialization.py +++ b/sanic_jwt/initialization.py @@ -176,7 +176,7 @@ def __add_endpoints(self): mapping.protected_kwargs, ) - self.bp.exception(exceptions.SanicJWTException)( + self.app.exception(exceptions.SanicJWTException)( self.responses.exception_response ) diff --git a/setup.py b/setup.py index f6dca80..56a7193 100644 --- a/setup.py +++ b/setup.py @@ -14,21 +14,23 @@ def open_local(paths, mode="r", encoding="utf8"): with open_local(["sanic_jwt", "__init__.py"], encoding="latin1") as fp: try: - version = re.findall(r"^__version__ = \"([0-9\.]+)\"", fp.read(), re.M)[0] + version = re.findall( + r"^__version__ = \"([0-9\.]+)\"", fp.read(), re.M + )[0] except IndexError: raise RuntimeError("Unable to determine version.") with open_local(["README.md"]) as rm: long_description = rm.read() -extras_require = {"docs": ["Sphinx"]} +extras_require = {"docs": ["Sphinx", "Jinja2<3.1"]} extras_require["all"] = [] for reqs in extras_require.values(): extras_require["all"].extend(reqs) install_requires = [ - "pyjwt~=2.1.0", + "pyjwt>=2.1.0,<3.0.0", ] setup( diff --git a/tests/test_async_options.py b/tests/test_async_options.py index d78f321..5fc1a16 100644 --- a/tests/test_async_options.py +++ b/tests/test_async_options.py @@ -49,7 +49,7 @@ async def extend_payload(self, payload, user=None, *args, **kwargs): @pytest.fixture def app(): - app = Sanic(__name__) + app = Sanic("Test") app.config.SANIC_JWT_AUTHORIZATION_HEADER_PREFIX = "JWT" app.config.SANIC_JWT_EXPIRATION_DELTA = 360000 app.config.SANIC_JWT_USER_ID = "username" diff --git a/tests/test_configuration.py b/tests/test_configuration.py index d8e526c..9c3a69f 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -364,13 +364,11 @@ def test_configuration_with_override_on_aliased(): def test_configuration_no_set_secret(): app = Sanic("sanic-jwt-test") - with pytest.warns(UserWarning) as record: - Initialize(app, authenticate=lambda: True) - - assert len(record) == 1 - assert record[0].message.args[0] == ( + message = ( "Sanic JWT was initialized using the default secret available to the " "public. DO NOT DEPLOY your application until you change it. " "See https://sanic-jwt.readthedocs.io/en/latest/pages/configuration.html#secret " "for more information." ) + with pytest.warns(UserWarning, match=message): + Initialize(app, authenticate=lambda: True) diff --git a/tests/test_endpoints_blueprint.py b/tests/test_endpoints_blueprint.py index 4d2c062..98bc0d2 100644 --- a/tests/test_endpoints_blueprint.py +++ b/tests/test_endpoints_blueprint.py @@ -1,3 +1,4 @@ +import pytest from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json @@ -5,27 +6,31 @@ from sanic_jwt import Initialize from sanic_jwt.decorators import protected -blueprint = Blueprint("Test", "/test") +@pytest.fixture +def app(): + blueprint = Blueprint("Test", "/test") -@blueprint.get("/", strict_slashes=True) -@protected() -def protected_hello_world(request): - return json({"message": "hello world"}) + @blueprint.get("/", strict_slashes=True) + @protected() + def protected_hello_world(request): + return json({"message": "hello world"}) + app = Sanic("sanic-jwt-test") + app.blueprint(blueprint) -async def authenticate(request, *args, **kwargs): - return {"user_id": 1} + return app -app = Sanic("sanic-jwt-test") +@pytest.fixture +def sanicjwt(app): + async def authenticate(request, *args, **kwargs): + return {"user_id": 1} -app.blueprint(blueprint) + return Initialize(app, authenticate=authenticate) -sanicjwt = Initialize(app, authenticate=authenticate) - -def test_protected_blueprint(): +def test_protected_blueprint(app, sanicjwt): _, response = app.test_client.get("/test/") assert response.status == 401 diff --git a/tests/test_endpoints_cbv.py b/tests/test_endpoints_cbv.py index 4a254a7..2f24e4f 100644 --- a/tests/test_endpoints_cbv.py +++ b/tests/test_endpoints_cbv.py @@ -1,4 +1,5 @@ import jwt +import pytest from sanic import Sanic from sanic.response import json from sanic.views import HTTPMethodView @@ -7,76 +8,77 @@ from sanic_jwt.decorators import protected -class User(object): - def __init__(self, id, username, password): - self.id = id - self.username = username - self.password = password +@pytest.fixture +def fixtures(): + class User(object): + def __init__(self, id, username, password): + self.id = id + self.username = username + self.password = password - def to_dict(self): - properties = ["user_id", "username"] - return {prop: getattr(self, prop, None) for prop in properties} + def to_dict(self): + properties = ["user_id", "username"] + return {prop: getattr(self, prop, None) for prop in properties} + users = [User(1, "user1", "abcxyz"), User(2, "user2", "abcxyz")] -users = [User(1, "user1", "abcxyz"), User(2, "user2", "abcxyz")] + username_table = {u.username: u for u in users} + # userid_table = {u.user_id: u for u in users} -username_table = {u.username: u for u in users} -# userid_table = {u.user_id: u for u in users} + async def authenticate(request, *args, **kwargs): + username = request.json.get("username", None) + password = request.json.get("password", None) + if not username or not password: + raise exceptions.AuthenticationFailed( + "Missing username or password." + ) -async def authenticate(request, *args, **kwargs): - username = request.json.get("username", None) - password = request.json.get("password", None) + user = username_table.get(username, None) + if user is None: + raise exceptions.AuthenticationFailed("User not found.") - if not username or not password: - raise exceptions.AuthenticationFailed("Missing username or password.") + if password != user.password: + raise exceptions.AuthenticationFailed("Password is incorrect.") - user = username_table.get(username, None) - if user is None: - raise exceptions.AuthenticationFailed("User not found.") + return user - if password != user.password: - raise exceptions.AuthenticationFailed("Password is incorrect.") + sanic_app = Sanic("sanic-jwt-test") + sanic_jwt = Initialize(sanic_app, authenticate=authenticate) - return user + class PublicView(HTTPMethodView): + def get(self, request): + return json({"hello": "world"}) + class ProtectedView(HTTPMethodView): + decorators = [protected()] -sanic_app = Sanic("sanic-jwt-test") -sanic_jwt = Initialize(sanic_app, authenticate=authenticate) + async def get(self, request): + return json({"protected": True}) + class PartiallyProtectedView(HTTPMethodView): + async def get(self, request): + return json({"protected": True}) -class PublicView(HTTPMethodView): - def get(self, request): - return json({"hello": "world"}) + @protected() + 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") -class ProtectedView(HTTPMethodView): - decorators = [protected()] - - async def get(self, request): - return json({"protected": True}) - - -class PartiallyProtectedView(HTTPMethodView): - async def get(self, request): - return json({"protected": True}) - - @protected() - 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") + return sanic_app, sanic_jwt class TestEndpointsCBV(object): - def test_unprotected(self): + def test_unprotected(self, fixtures): + sanic_app, sanic_jwt = fixtures _, response = sanic_app.test_client.get("/") assert response.status == 200 - def test_protected(self): + def test_protected(self, fixtures): + sanic_app, sanic_jwt = fixtures _, response = sanic_app.test_client.get("/protected") assert response.status == 401 assert response.json.get("exception") == "Unauthorized" @@ -84,7 +86,8 @@ def test_protected(self): "reasons" ) - def test_partially_protected(self): + def test_partially_protected(self, fixtures): + sanic_app, sanic_jwt = fixtures _, response = sanic_app.test_client.get("/partially") assert response.status == 200 @@ -95,12 +98,14 @@ def test_partially_protected(self): "reasons" ) - def test_auth_invalid_method(self): + def test_auth_invalid_method(self, fixtures): + sanic_app, sanic_jwt = fixtures _, response = sanic_app.test_client.get("/auth") assert response.status == 405 assert b"Method GET not allowed for URL /auth" in response.body - def test_auth_proper_credentials(self): + def test_auth_proper_credentials(self, fixtures): + sanic_app, sanic_jwt = fixtures _, response = sanic_app.test_client.post( "/auth", json={"username": "user1", "password": "abcxyz"} ) diff --git a/tests/test_endpoints_extra.py b/tests/test_endpoints_extra.py index 437a710..f6a345a 100644 --- a/tests/test_endpoints_extra.py +++ b/tests/test_endpoints_extra.py @@ -1,3 +1,4 @@ +import pytest from sanic import response, Sanic from sanic.response import json @@ -15,23 +16,26 @@ async def post(self, request): return json(response) -app = Sanic("sanic-jwt-test") -initialize( - app, - authenticate=lambda: True, - class_views=[ - ( - "/magic-login", - MagicLoginHandler, - ) # The path will be relative to the url prefix - # (which defaults to /auth) - ], -) +@pytest.fixture +def app(): + app = Sanic("sanic-jwt-test") + initialize( + app, + authenticate=lambda: True, + class_views=[ + ( + "/magic-login", + MagicLoginHandler, + ) # The path will be relative to the url prefix + # (which defaults to /auth) + ], + ) + return app class TestEndpointsExtra(object): def dispatch(self, path, method): - method = getattr(app.test_client, method) + method = getattr(self.app.test_client, method) request, response = method(path) return request, response @@ -44,7 +48,8 @@ def post(self, path): def options(self, path): return self.dispatch(path, "options") - def test_verify_token(self): + def test_verify_token(self, app): + self.app = app _, response = self.options("/auth/magic-login") assert response.status == 204 diff --git a/tests/test_endpoints_init_on_bp_and_app.py b/tests/test_endpoints_init_on_bp_and_app.py index 2c06900..45754b7 100644 --- a/tests/test_endpoints_init_on_bp_and_app.py +++ b/tests/test_endpoints_init_on_bp_and_app.py @@ -1,3 +1,4 @@ +import pytest from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json @@ -5,49 +6,60 @@ from sanic_jwt import Initialize from sanic_jwt.decorators import protected -blueprint = Blueprint("Test1") +@pytest.fixture +def bp(): + blueprint = Blueprint("Test1", url_prefix="/test1") -@blueprint.get("/", strict_slashes=True) -@protected(blueprint) -def protected_hello_world_bp(request): - return json({"type": "bp"}) + @blueprint.get("/", strict_slashes=True) + @protected(blueprint) + def protected_hello_world_bp(request): + return json({"type": "bp"}) + return blueprint -async def authenticate1(request, *args, **kwargs): - return {"user_id": 1} +@pytest.fixture +def app(bp): + app = Sanic("sanic-jwt-test") -async def authenticate2(request, *args, **kwargs): - return {"user_id": 2} + @app.get("/", strict_slashes=True) + @protected() + def protected_hello_world_app(request): + return json({"type": "app"}) + return app -app = Sanic("sanic-jwt-test") +async def authenticate1(request, *args, **kwargs): + return {"user_id": 1} -@app.get("/", strict_slashes=True) -@protected() -def protected_hello_world_app(request): - return json({"type": "app"}) +async def authenticate2(request, *args, **kwargs): + return {"user_id": 2} -sanicjwt1 = Initialize(blueprint, app=app, authenticate=authenticate1) +@pytest.fixture +def sanicjwt1(bp, app): + return Initialize(bp, app=app, authenticate=authenticate1) -sanicjwt2 = Initialize( - app, - authenticate=authenticate2, - url_prefix="/a", - access_token_name="token", - cookie_access_token_name="token", - cookie_set=True, - secret="somethingdifferent", -) -app.blueprint(blueprint, url_prefix="/test1") +@pytest.fixture +def sanicjwt2(app): + return Initialize( + app, + authenticate=authenticate2, + url_prefix="/a", + access_token_name="token", + cookie_access_token_name="token", + cookie_set=True, + secret="somethingdifferent", + ) -def test_protected_blueprints(): +def test_protected_blueprints(app, bp, sanicjwt1, sanicjwt2): + app.blueprint(bp) + _, response1 = app.test_client.post( "/test1/auth", json={"username": "user1", "password": "abcxyz"} ) @@ -109,7 +121,9 @@ def test_protected_blueprints(): assert "Auth required." in response2.json.get("reasons") -def test_protected_blueprints_debug(): +def test_protected_blueprints_debug(app, bp, sanicjwt1, sanicjwt2): + app.blueprint(bp, url_prefix="/test1") + sanicjwt1.config.debug.update(True) sanicjwt2.config.debug.update(True) diff --git a/tests/test_endpoints_init_on_bp_decorated.py b/tests/test_endpoints_init_on_bp_decorated.py index df087bb..4a7f3c6 100644 --- a/tests/test_endpoints_init_on_bp_decorated.py +++ b/tests/test_endpoints_init_on_bp_decorated.py @@ -1,3 +1,4 @@ +import pytest from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json @@ -9,33 +10,34 @@ async def authenticate(request, *args, **kwargs): return {"user_id": 1} -blueprint = Blueprint("Test") -app = Sanic("sanic-jwt-test") -sanicjwt = Initialize(blueprint, app=app, authenticate=authenticate) +@pytest.fixture +def fixtures(): + blueprint = Blueprint("Test") + app = Sanic("sanic-jwt-test") + sanicjwt = Initialize(blueprint, app=app, authenticate=authenticate) + @blueprint.get("/", strict_slashes=True) + @sanicjwt.protected() + def protected_hello_world(request): + return json({"message": "hello world"}) -@blueprint.get("/", strict_slashes=True) -@sanicjwt.protected() -def protected_hello_world(request): - return json({"message": "hello world"}) + @blueprint.get("/user/", strict_slashes=True) + @sanicjwt.protected(authorization_header="foobar") + def protected_user(request, id): + return json({"user": id}) + @blueprint.route("/scoped_empty") + @sanicjwt.scoped("something") + async def scoped(request): + return json({"scoped": True}) -@blueprint.get("/user/", strict_slashes=True) -@sanicjwt.protected(authorization_header="foobar") -def protected_user(request, id): - return json({"user": id}) + app.blueprint(blueprint, url_prefix="/test") + return blueprint, app, sanicjwt -@blueprint.route("/scoped_empty") -@sanicjwt.scoped("something") -async def scoped(request): - return json({"scoped": True}) - -app.blueprint(blueprint, url_prefix="/test") - - -def test_protected_blueprint(): +def test_protected_blueprint(fixtures): + blueprint, app, sanicjwt = fixtures _, response = app.test_client.get("/test/") assert response.status == 401 @@ -76,7 +78,8 @@ def test_protected_blueprint(): assert response.json.get("user") == "1" -def test_scoped_empty(): +def test_scoped_empty(fixtures): + blueprint, app, sanicjwt = fixtures _, response = app.test_client.get("/test/scoped_empty") assert response.status == 401 assert response.json.get("exception") == "Unauthorized" diff --git a/tests/test_endpoints_init_on_bp_multiple.py b/tests/test_endpoints_init_on_bp_multiple.py index e75bea5..0a64090 100644 --- a/tests/test_endpoints_init_on_bp_multiple.py +++ b/tests/test_endpoints_init_on_bp_multiple.py @@ -1,3 +1,4 @@ +import pytest from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json @@ -5,48 +6,48 @@ from sanic_jwt import Initialize from sanic_jwt.decorators import protected -blueprint1 = Blueprint("Test1") -blueprint2 = Blueprint("Test2") +@pytest.fixture +def fixtures(): + blueprint1 = Blueprint("Test1") + blueprint2 = Blueprint("Test2") -@blueprint1.get("/", strict_slashes=True) -@protected(blueprint1) -def protected_hello_world_1(request): - return json({"version": 1}) + @blueprint1.get("/", strict_slashes=True) + @protected(blueprint1) + def protected_hello_world_1(request): + return json({"version": 1}) + @blueprint2.get("/", strict_slashes=True) + @protected(blueprint2) + def protected_hello_world_2(request): + return json({"version": 2}) -@blueprint2.get("/", strict_slashes=True) -@protected(blueprint2) -def protected_hello_world_2(request): - return json({"version": 2}) + async def authenticate(request, *args, **kwargs): + return {"user_id": 1} + app = Sanic("sanic-jwt-test") -async def authenticate(request, *args, **kwargs): - return {"user_id": 1} - - -app = Sanic("sanic-jwt-test") - - -sanicjwt1 = Initialize(blueprint1, app=app, authenticate=authenticate) + sanicjwt1 = Initialize(blueprint1, app=app, authenticate=authenticate) + sanicjwt2 = Initialize( + blueprint2, + app=app, + authenticate=authenticate, + url_prefix="/a", + access_token_name="token", + cookie_access_token_name="token", + cookie_set=True, + secret="somethingdifferent", + ) -sanicjwt2 = Initialize( - blueprint2, - app=app, - authenticate=authenticate, - url_prefix="/a", - access_token_name="token", - cookie_access_token_name="token", - cookie_set=True, - secret="somethingdifferent", -) + app.blueprint(blueprint1, url_prefix="/test1") + app.blueprint(blueprint2, url_prefix="/test2") -app.blueprint(blueprint1, url_prefix="/test1") -app.blueprint(blueprint2, url_prefix="/test2") + return blueprint1, blueprint2, app, sanicjwt1, sanicjwt2 -def test_protected_blueprints(): +def test_protected_blueprints(fixtures): + blueprint1, blueprint2, app, sanicjwt1, sanicjwt2 = fixtures _, response1 = app.test_client.get("/test1/") _, response2 = app.test_client.get("/test2/") @@ -118,7 +119,8 @@ def test_protected_blueprints(): assert "Auth required." in response2.json.get("reasons") -def test_protected_blueprints_debug(): +def test_protected_blueprints_debug(fixtures): + blueprint1, blueprint2, app, sanicjwt1, sanicjwt2 = fixtures sanicjwt1.config.debug.update(True) sanicjwt2.config.debug.update(True) diff --git a/tests/test_endpoints_init_on_bp_single.py b/tests/test_endpoints_init_on_bp_single.py index dc8c731..03a1fce 100644 --- a/tests/test_endpoints_init_on_bp_single.py +++ b/tests/test_endpoints_init_on_bp_single.py @@ -1,64 +1,64 @@ +import pytest from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json from sanic_jwt import Authentication, Initialize, protected, scoped -blueprint = Blueprint("Test") -cache = {} +@pytest.fixture +def fixtures(): + blueprint = Blueprint("Test") + cache = {} -@blueprint.get("/", strict_slashes=True) -@protected(blueprint) -def protected_hello_world(request): - return json({"message": "hello world"}) + @blueprint.get("/", strict_slashes=True) + @protected(blueprint) + def protected_hello_world(request): + return json({"message": "hello world"}) + @blueprint.get("/user/", strict_slashes=True) + @protected(blueprint) + def protected_user(request, id): + return json({"user": id}) -@blueprint.get("/user/", strict_slashes=True) -@protected(blueprint) -def protected_user(request, id): - return json({"user": id}) + @blueprint.route("/scoped_empty") + @scoped("something", initialized_on=blueprint) + async def scoped_handler(request): + return json({"scoped": True}) + class MyAuthentication(Authentication): + async def authenticate(self, request, *args, **kwargs): + return {"user_id": 1} -@blueprint.route("/scoped_empty") -@scoped("something", initialized_on=blueprint) -async def scoped(request): - return json({"scoped": True}) + async def store_refresh_token( + self, user_id, refresh_token, *args, **kwargs + ): + key = "refresh_token_{user_id}".format(user_id=user_id) + cache[key] = refresh_token + async def retrieve_refresh_token(self, user_id, *args, **kwargs): + key = "refresh_token_{user_id}".format(user_id=user_id) + token = cache.get(key, None) + return token -class MyAuthentication(Authentication): - async def authenticate(self, request, *args, **kwargs): - return {"user_id": 1} + async def retrieve_user(self, request, payload, *args, **kwargs): + return {"user_id": 1} - async def store_refresh_token( - self, user_id, refresh_token, *args, **kwargs - ): - key = "refresh_token_{user_id}".format(user_id=user_id) - cache[key] = refresh_token + app = Sanic("sanic-jwt-test") - async def retrieve_refresh_token(self, user_id, *args, **kwargs): - key = "refresh_token_{user_id}".format(user_id=user_id) - token = cache.get(key, None) - return token - - async def retrieve_user(self, request, payload, *args, **kwargs): - return {"user_id": 1} - - -app = Sanic("sanic-jwt-test") - - -sanicjwt = Initialize( - blueprint, - app=app, - authentication_class=MyAuthentication, - refresh_token_enabled=True, -) + sanicjwt = Initialize( + blueprint, + app=app, + authentication_class=MyAuthentication, + refresh_token_enabled=True, + ) -app.blueprint(blueprint, url_prefix="/test") + app.blueprint(blueprint, url_prefix="/test") + return blueprint, app, sanicjwt -def test_protected_blueprint(): +def test_protected_blueprint(fixtures): + blueprint, app, sanicjwt = fixtures _, response = app.test_client.get("/test/") assert response.status == 401 @@ -83,14 +83,16 @@ def test_protected_blueprint(): assert response.json.get("message") == "hello world" -def test_scoped_empty(): +def test_scoped_empty(fixtures): + blueprint, app, sanicjwt = fixtures _, response = app.test_client.get("/test/scoped_empty") assert response.status == 401 assert response.json.get("exception") == "Unauthorized" assert "Authorization header not present." in response.json.get("reasons") -def test_authentication_all_methods(): +def test_authentication_all_methods(fixtures): + blueprint, app, sanicjwt = fixtures _, response = app.test_client.post( "/test/auth", json={"username": "user1", "password": "abcxyz"} diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 9ab8095..5fc2b77 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -1,4 +1,4 @@ -from sanic.exceptions import abort +from sanic.exceptions import InvalidUsage from sanic_jwt.decorators import protected @@ -9,7 +9,7 @@ def test_abort_called_in_endpoint(app): @sanic_app.route("/abort", methods=["GET"]) @protected() async def test(request): - abort(400, "Aborted request") + raise InvalidUsage("Aborted request") _, response = sanic_app.test_client.post( "/auth", json={"username": "user1", "password": "abcxyz"} diff --git a/tests/test_initialize.py b/tests/test_initialize.py index 618ab73..e6c903c 100644 --- a/tests/test_initialize.py +++ b/tests/test_initialize.py @@ -113,7 +113,7 @@ def test_initialize_class_on_multiple_blueprints(): app = Sanic("sanic-jwt-test") bp1 = Blueprint("test1") app.blueprint(bp1) - bp2 = Blueprint("test2") + bp2 = Blueprint("test2", version=2) app.blueprint(bp2) sanicjwt1 = Initialize(bp1, app=app, authenticate=lambda: True) @@ -127,7 +127,7 @@ def test_initialize_class_on_multiple_blueprints(): def test_initialize_class_on_app_and_blueprint(): app = Sanic("sanic-jwt-test") - bp = Blueprint("test") + bp = Blueprint("test", url_prefix="/bp") app.blueprint(bp) sanicjwt1 = Initialize(app, authenticate=lambda: True) diff --git a/tests/test_microservices.py b/tests/test_microservices.py index d8873b0..49a839c 100644 --- a/tests/test_microservices.py +++ b/tests/test_microservices.py @@ -24,7 +24,7 @@ def test_microservice_simple(): def test_microservice_interaction(): - microservice_app = Sanic("sanic-jwt-test") + microservice_app = Sanic("sanic-jwt-test-microservice") microservice_sanic_jwt = Initialize(microservice_app, auth_mode=False) @microservice_app.route("/protected") @@ -32,7 +32,7 @@ def test_microservice_interaction(): async def protected_request(request): return json({"protected": True}) - app = Sanic("sanic-jwt-test") + app = Sanic("sanic-jwt-test-app") sanic_jwt = Initialize(app, authenticate=authenticate) _, response = microservice_app.test_client.get("/protected") diff --git a/tests/test_sanic_abort.py b/tests/test_sanic_abort.py index fb84274..e1321e7 100644 --- a/tests/test_sanic_abort.py +++ b/tests/test_sanic_abort.py @@ -1,4 +1,4 @@ -from sanic.exceptions import abort +from sanic.exceptions import Unauthorized def test_sanic_abort_401(app): @@ -6,7 +6,7 @@ def test_sanic_abort_401(app): @sanic_app.route("/abort") async def abort_request(request): - abort(401) + raise Unauthorized("Aborted request") _, response = sanic_app.test_client.get("/abort") diff --git a/tests/test_user_secret.py b/tests/test_user_secret.py index 77b0b92..e45c78e 100644 --- a/tests/test_user_secret.py +++ b/tests/test_user_secret.py @@ -1,4 +1,3 @@ -import jwt import pytest from sanic import Sanic @@ -6,9 +5,9 @@ def test_secret_not_enabled(): - app = Sanic(__name__) + app = Sanic("Test") with pytest.raises(exceptions.UserSecretNotImplemented): - sanicjwt = Initialize( + Initialize( app, authenticate=lambda: {}, user_secret_enabled=True, diff --git a/tox.ini b/tox.ini index eb820f1..85d78c1 100644 --- a/tox.ini +++ b/tox.ini @@ -4,9 +4,9 @@ envlist = docs, clean, py{37,38,39}, report, check [gh-actions] python = - 3.7: py37,docs + 3.7: py37 3.8: py38 - 3.9: py39,clean,report,check + 3.9: py39,clean,report,check,docs [testenv] basepython = @@ -22,10 +22,10 @@ passenv = * deps = -r{toxinidir}/requirements.testing.txt commands = - {posargs:pytest --cov --cov-append --cov-report=term-missing tests} + pytest {posargs:--cov --cov-append --cov-report=term-missing tests} [testenv:docs] -basepython = python3.7 +basepython = python3.9 deps = -r{toxinidir}/docs/requirements.txt skip_install = true