2018-02-25 12:05:29 +00:00
|
|
|
import pytest
|
|
|
|
from rest_framework_jwt.settings import api_settings
|
|
|
|
|
|
|
|
from funkwhale_api.common.auth import TokenAuthMiddleware
|
|
|
|
|
|
|
|
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
|
|
|
|
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@pytest.mark.parametrize("query_string", [b"token=wrong", b""])
|
2018-02-25 12:05:29 +00:00
|
|
|
def test_header_anonymous(query_string, factories):
|
|
|
|
def callback(scope):
|
2018-06-09 13:36:16 +00:00
|
|
|
assert scope["user"].is_anonymous
|
2018-02-25 12:05:29 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
scope = {"query_string": query_string}
|
2018-02-25 12:05:29 +00:00
|
|
|
consumer = TokenAuthMiddleware(callback)
|
|
|
|
consumer(scope)
|
|
|
|
|
|
|
|
|
|
|
|
def test_header_correct_token(factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
user = factories["users.User"]()
|
2018-02-25 12:05:29 +00:00
|
|
|
payload = jwt_payload_handler(user)
|
|
|
|
token = jwt_encode_handler(payload)
|
2018-06-09 13:36:16 +00:00
|
|
|
|
2018-02-25 12:05:29 +00:00
|
|
|
def callback(scope):
|
2018-06-09 13:36:16 +00:00
|
|
|
assert scope["user"] == user
|
2018-02-25 12:05:29 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
scope = {"query_string": "token={}".format(token).encode("utf-8")}
|
2018-02-25 12:05:29 +00:00
|
|
|
consumer = TokenAuthMiddleware(callback)
|
|
|
|
consumer(scope)
|