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 | |
| 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')
| -rw-r--r-- | tests/conftest.py | 6 | ||||
| -rw-r--r-- | tests/test_auth.py | 92 |
2 files changed, 95 insertions, 3 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index fb8c751..c18f9de 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,9 @@ import os import tempfile +import json import pytest + from sw import create_app from sw.db import get_db, init_db @@ -41,7 +43,9 @@ class AuthActions(object): def login(self, username="test", password="test"): return self._client.post( - "/auth/login", data=jsonify({"username": username, "password": password,}) + "/auth/login", + data=json.dumps({"username": username, "password": password,}), + headers={"content-type": "application/json"}, ) def logout(self): 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 |
