From e165f6ef450cc27fd8946117533193f494cb9b3c Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Fri, 10 Mar 2023 18:38:41 -0500 Subject: [PATCH] tests/test_userinfo.py: Split off authentication tests --- tests/test_auth.py | 46 ----------------------------------------- tests/test_userinfo.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 tests/test_userinfo.py diff --git a/tests/test_auth.py b/tests/test_auth.py index 479729d..8cf1564 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -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()) diff --git a/tests/test_userinfo.py b/tests/test_userinfo.py new file mode 100644 index 0000000..f50f894 --- /dev/null +++ b/tests/test_userinfo.py @@ -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())