flaschengeist/tests/test_auth.py

33 lines
1.0 KiB
Python

import pytest
VALID_TOKEN = "f4ecbe14be3527ca998143a49200e294"
USERID = "user"
PASSWORD = "1234"
@pytest.mark.depends(on=["test_login_decorator"])
def test_login(client):
"""Testing login"""
result = client.post("/auth", json={"userid": USERID, "password": PASSWORD})
json = result.get_json()
# Login successful
assert result.status_code == 201
# User set correctly
assert json["user"]["userid"] == USERID
# Token works
assert client.get("/auth", headers={"Authorization": f"Bearer {json['session']['token']}"}).status_code == 200
def test_login_decorator(client):
"""Testing the login_required decorator"""
# No header at all
assert client.get("/auth").status_code == 401
# Invalid header
assert client.get("/auth", headers={"Authorization": "INVALID"}).status_code == 401
# Invalid Token
assert client.get("/auth", headers={"Authorization": "Bearer INVALID"}).status_code == 401
# Valid Token
assert client.get("/auth", headers={"Authorization": f"Bearer {VALID_TOKEN}"}).status_code == 200