Skip to content

Commit

Permalink
Fixed coverage for the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
amitrip committed Dec 8, 2018
1 parent e1b268d commit a2051a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 0 additions & 2 deletions starlette_jwt/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ def get_token_from_header(cls, authorization: str, prefix: str):
:param authorization:
:return:
"""
if authorization is None:
raise AuthenticationError('Authorization header is missing')
try:
scheme, token = authorization.split()
except ValueError:
Expand Down
17 changes: 16 additions & 1 deletion tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from starlette.applications import Starlette
from starlette_jwt import JWTAuthenticationBackend
from starlette_jwt import JWTAuthenticationBackend, JWTUser
from starlette.responses import JSONResponse
from starlette.testclient import TestClient
from starlette.middleware.authentication import AuthenticationMiddleware
Expand Down Expand Up @@ -29,6 +29,11 @@ def test_header_parse():
app.add_middleware(AuthenticationMiddleware, backend=JWTAuthenticationBackend(secret_key=secret_key))
client = TestClient(app)

# No token for auth endpoint
response = client.get("/auth")
assert response.text == 'Forbidden'
assert response.status_code == 403

# Without prefix
response = client.get("/auth",
headers=dict(Authorization=jwt.encode(dict(username="user"), secret_key).decode()))
Expand Down Expand Up @@ -56,3 +61,13 @@ def test_header_parse():
def test_get_token_from_header():
token = jwt.encode(dict(username="user"), 'secret').decode()
assert token == JWTAuthenticationBackend.get_token_from_header(authorization=f'JWT {token}', prefix='JWT')


def test_user_object():
payload = dict(username="user")
token = jwt.encode(payload, "BAD SECRET").decode()
user_object = JWTUser(username="user", payload=payload, token=token)
assert user_object.is_authenticated == True
assert user_object.display_name == 'user'
assert user_object.token == token
assert user_object.payload == payload

0 comments on commit a2051a5

Please sign in to comment.