-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
18 changed files
with
577 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[paths] | ||
source = | ||
sanic_jwt | ||
tests | ||
|
||
[run] | ||
source = | ||
sanic_jwt | ||
tests | ||
branch = True | ||
|
||
[report] | ||
exclude_lines = | ||
no cov | ||
no qa | ||
noqa | ||
pragma: no cover | ||
if __name__ == .__main__.: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[settings] | ||
known_first_party=sanic_jwt | ||
known_third_party=sanic | ||
line_length=79 | ||
multi_line_output=3 | ||
not_skip=__init__.py | ||
order_by_type=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[pytest] | ||
addopts = -xrs --cov sanic_jwt --cov-report term-missing --pep8 --flakes --cache-clear | ||
pep8ignore = | ||
*.py E501 E402 E701 | ||
examples/*.py ALL | ||
pep8maxlinelength = 79 | ||
testpaths = | ||
tests | ||
sanic_jwt | ||
flakes-ignore = | ||
examples/*.py ALL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
[aliases] | ||
test=pytest | ||
|
||
[metadata] | ||
description-file = README.md | ||
description-file = README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from sanic import Sanic | ||
from sanic.response import json | ||
|
||
import pytest | ||
from sanic_jwt import exceptions, initialize | ||
from sanic_jwt.decorators import protected | ||
|
||
|
||
class User(object): | ||
|
||
def __init__(self, id, username, password): | ||
self.user_id = id | ||
self.username = username | ||
self.password = password | ||
|
||
def __str__(self): | ||
return "User(id='%s')" % self.id | ||
|
||
def to_dict(self): | ||
properties = [ | ||
'user_id', | ||
'username', | ||
] | ||
return {prop: getattr(self, prop, None) for prop in properties} | ||
|
||
|
||
@pytest.yield_fixture | ||
def users(): | ||
yield [ | ||
User(1, 'user1', 'abcxyz'), | ||
User(2, 'user2', 'abcxyz'), | ||
] | ||
|
||
|
||
@pytest.yield_fixture | ||
def username_table(users): | ||
yield {u.username: u for u in users} | ||
|
||
|
||
@pytest.yield_fixture | ||
def authenticate(username_table): | ||
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.") | ||
|
||
user = username_table.get(username, None) | ||
if user is None: | ||
raise exceptions.AuthenticationFailed("User not found.") | ||
|
||
if password != user.password: | ||
raise exceptions.AuthenticationFailed("Password is incorrect.") | ||
|
||
return user | ||
|
||
yield authenticate | ||
|
||
|
||
@pytest.yield_fixture | ||
def app(username_table, authenticate): | ||
|
||
sanic_app = Sanic() | ||
initialize( | ||
sanic_app, | ||
authenticate=authenticate, | ||
) | ||
|
||
@sanic_app.route("/") | ||
async def helloworld(request): | ||
return json({"hello": "world"}) | ||
|
||
@sanic_app.route("/protected") | ||
@protected() | ||
async def protected_request(request): | ||
return json({"protected": True}) | ||
|
||
yield sanic_app |
Oops, something went wrong.