2021-01-23 01:18:44 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-01-25 12:12:04 +00:00
|
|
|
|
|
|
|
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
|
2021-12-21 21:56:03 +00:00
|
|
|
assert json["userid"] == USERID
|
2021-01-25 12:12:04 +00:00
|
|
|
# Token works
|
2021-12-21 21:56:03 +00:00
|
|
|
assert client.get("/auth", headers={"Authorization": f"Bearer {json['token']}"}).status_code == 200
|
2021-01-23 01:18:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-01-25 12:12:04 +00:00
|
|
|
# Valid Token
|
|
|
|
assert client.get("/auth", headers={"Authorization": f"Bearer {VALID_TOKEN}"}).status_code == 200
|