tests/test_userinfo.py: Split off authentication tests

This commit is contained in:
dogeystamp 2023-03-10 18:38:41 -05:00
parent 10dc7957a2
commit e165f6ef45
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
2 changed files with 47 additions and 46 deletions

View File

@ -84,49 +84,3 @@ def test_login(client, users):
assert resp_json.get("username") == "jeff"
token = resp_json.get("auth_token")
assert token is not None and token != ""
def test_userinfo(client, tokens, validate_info):
"""Test accessing the user information endpoint as a normal user."""
# access user info endpoint
resp = client.get(
"/users/jeff",
headers={
"Authorization": f"bearer {tokens['jeff']}"
}
)
assert resp.status_code == 200
validate_info("jeff", resp.get_json())
# access other user's info endpoint
resp = client.get(
"/users/administrator",
headers={
"Authorization": f"bearer {tokens['jeff']}"
}
)
assert resp.status_code == 403
def test_userinfo_admin(client, tokens, validate_info):
"""Test accessing other user's information as an admin."""
# first test that admin can access its own info
resp = client.get(
"/users/administrator",
headers={
"Authorization": f"bearer {tokens['administrator']}"
}
)
assert resp.status_code == 200
validate_info("administrator", resp.get_json())
# now test accessing other user's info
resp = client.get(
"/users/jeff",
headers={
"Authorization": f"bearer {tokens['administrator']}"
}
)
assert resp.status_code == 200
validate_info("jeff", resp.get_json())

47
tests/test_userinfo.py Normal file
View File

@ -0,0 +1,47 @@
import pytest
def test_userinfo(client, tokens, validate_info):
"""Test accessing the user information endpoint as a normal user."""
# access user info endpoint
resp = client.get(
"/users/jeff",
headers={
"Authorization": f"bearer {tokens['jeff']}"
}
)
assert resp.status_code == 200
validate_info("jeff", resp.get_json())
# access other user's info endpoint
resp = client.get(
"/users/administrator",
headers={
"Authorization": f"bearer {tokens['jeff']}"
}
)
assert resp.status_code == 403
def test_userinfo_admin(client, tokens, validate_info):
"""Test accessing other user's information as an admin."""
# first test that admin can access its own info
resp = client.get(
"/users/administrator",
headers={
"Authorization": f"bearer {tokens['administrator']}"
}
)
assert resp.status_code == 200
validate_info("administrator", resp.get_json())
# now test accessing other user's info
resp = client.get(
"/users/jeff",
headers={
"Authorization": f"bearer {tokens['administrator']}"
}
)
assert resp.status_code == 200
validate_info("jeff", resp.get_json())