diff options
| author | Tom Harley | 2021-07-07 15:41:42 +0000 |
|---|---|---|
| committer | Tom Harley | 2021-07-07 15:41:42 +0000 |
| commit | 6e58d1e712068de46f68a1188328d68273605f5b (patch) | |
| tree | 9df3ed956ecc6fba0aac1b2f121188ef7b5b2811 /tests/test_auth.py | |
| parent | Begin work on adding user interaction (diff) | |
| download | swap-6e58d1e712068de46f68a1188328d68273605f5b.tar.gz swap-6e58d1e712068de46f68a1188328d68273605f5b.zip | |
Add testing for authorisation decorators
Test addition commits always look big lmao
As mentioned in the previous commit, this commit adds a check for the correct user when accessing someone’s /inbox.
Diffstat (limited to 'tests/test_auth.py')
| -rw-r--r-- | tests/test_auth.py | 92 |
1 files changed, 90 insertions, 2 deletions
diff --git a/tests/test_auth.py b/tests/test_auth.py index 067746d..4e28e93 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,8 +1,11 @@ import pytest -from flask import g, session, json +from flask import g, session, json, url_for from sw.db import get_db +from sw.auth import login_required, specific_user_required + +from werkzeug.datastructures import Headers def test_register(client, app): @@ -21,6 +24,15 @@ def test_register(client, app): assert db_result is not None +def test_login_right_credentials(client, auth): + assert auth.login().status_code == 200 + + with client: + client.get("/") + assert session["user_id"] == 1 + assert g.user["username"] == "test" + + @pytest.mark.parametrize( ("username", "password", "message"), ( @@ -35,6 +47,82 @@ def test_register_validate_input(client, username, password, message): response = client.post( "/auth/register", data=json.dumps({"username": username, "password": password,}), - content_type="application/json", + headers={"Content-Type": "application/json"}, ) assert message in response.data + + +def test_login_required_no_user(client, app): + test_endpoint = "/test/dummy_endpoint" + + @app.route(test_endpoint) + @login_required + def dummy_endpoint(): + return "Should be unreachable", 200 + + response = client.get(test_endpoint) + + assert response.status_code == 403 + assert b"Should be unreachable" not in response.data + + +def test_login_required_with_user(client, app, auth): + test_endpoint = "/test/dummy_endpoint" + + @app.route(test_endpoint) + @login_required + def dummy_endpoint(): + return "Should succeed", 200 + + auth.login() + with client: + response = client.get(test_endpoint) + + assert response.status_code == 200 + assert b"Should succeed" in response.data + + +def test_specific_user_required_no_user(client, app): + test_endpoint = "/test/dummy_endpoint/<username>" + + @app.route(test_endpoint) + @specific_user_required + def dummy_endpoint(username): + return "Should be unreachable", 200 + + response = client.get("/test/dummy_endpoint/test") + + assert response.status_code == 403 + assert b"Should be unreachable" not in response.data + + +def test_specific_user_required_wrong_user(client, app, auth): + test_endpoint = "/test/dummy_endpoint/<username>" + + @app.route(test_endpoint) + @specific_user_required + def dummy_endpoint(username): + return "Should be unreachable", 200 + + auth.login() + with client: + response = client.get("/test/dummy_endpoint/other") + + assert response.status_code == 403 + assert b"Should be unreachable" not in response.data + + +def test_specific_user_required_right_user(client, app, auth): + test_endpoint = "/test/dummy_endpoint/<username>" + + @app.route(test_endpoint) + @specific_user_required + def dummy_endpoint(username): + return "Should succeed", 200 + + auth.login() + with client: + response = client.get("/test/dummy_endpoint/test") + + assert response.status_code == 200 + assert b"Should succeed" in response.data |
