summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTom Harley2021-07-09 15:16:54 +0000
committerTom Harley2021-07-09 15:16:54 +0000
commit57e7488149ee093e34774d83ffe161fb305a7dde (patch)
treeed922a052cd8eb6fc5b77c21970cb0d0b02b9cb3 /tests
parentAdd testing for authorisation decorators (diff)
downloadswap-57e7488149ee093e34774d83ffe161fb305a7dde.tar.gz
swap-57e7488149ee093e34774d83ffe161fb305a7dde.zip

Add basic inbox/outbox functionality

Lots of added TODOs, and very little test coverage for the new functions.

Next step is to get 100% coverage of what we have so far.

Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py4
-rw-r--r--tests/data.sql49
-rw-r--r--tests/test_auth.py44
-rw-r--r--tests/test_users.py71
4 files changed, 162 insertions, 6 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index c18f9de..1e41314 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -15,7 +15,9 @@ with open(os.path.join(os.path.dirname(__file__), "data.sql"), "rb") as f:
def app():
db_fd, db_path = tempfile.mkstemp()
- app = create_app({"TESTING": True, "DATABASE": db_path,})
+ app = create_app(
+ {"TESTING": True, "DATABASE": db_path, "SERVER_NAME": "test.localdomain"}
+ )
with app.app_context():
init_db()
diff --git a/tests/data.sql b/tests/data.sql
index eb1f36e..5add3a2 100644
--- a/tests/data.sql
+++ b/tests/data.sql
@@ -1,4 +1,47 @@
-insert into users (username, password)
+insert into users (username, password, inbox_size)
values
- ('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'),
- ('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79');
+ ('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f', 40),
+ ('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79', 0);
+
+insert into inbox (recipient, ap_content, received)
+values
+ ('test', '1', '2021-07-09T11:21:58+0000'),
+ ('test', '2', '2021-07-09T11:21:58+0000'),
+ ('test', '3', '2021-07-09T11:21:58+0000'),
+ ('test', '4', '2021-07-09T11:21:58+0000'),
+ ('test', '5', '2021-07-09T11:21:58+0000'),
+ ('test', '6', '2021-07-09T11:21:58+0000'),
+ ('test', '7', '2021-07-09T11:21:58+0000'),
+ ('test', '8', '2021-07-09T11:21:58+0000'),
+ ('test', '9', '2021-07-09T11:21:58+0000'),
+ ('test', '10', '2021-07-09T11:21:58+0000'),
+ ('test', '11', '2021-07-09T11:21:58+0000'),
+ ('test', '12', '2021-07-09T11:21:58+0000'),
+ ('test', '13', '2021-07-09T11:21:58+0000'),
+ ('test', '14', '2021-07-09T11:21:58+0000'),
+ ('test', '15', '2021-07-09T11:21:58+0000'),
+ ('test', '16', '2021-07-09T11:21:58+0000'),
+ ('test', '17', '2021-07-09T11:21:58+0000'),
+ ('test', '18', '2021-07-09T11:21:58+0000'),
+ ('test', '19', '2021-07-09T11:21:58+0000'),
+ ('test', '20', '2021-07-09T11:21:58+0000'),
+ ('test', '21', '2021-07-09T11:21:58+0000'),
+ ('test', '22', '2021-07-09T11:21:58+0000'),
+ ('test', '23', '2021-07-09T11:21:58+0000'),
+ ('test', '24', '2021-07-09T11:21:58+0000'),
+ ('test', '25', '2021-07-09T11:21:58+0000'),
+ ('test', '26', '2021-07-09T11:21:58+0000'),
+ ('test', '27', '2021-07-09T11:21:58+0000'),
+ ('test', '28', '2021-07-09T11:21:58+0000'),
+ ('test', '29', '2021-07-09T11:21:58+0000'),
+ ('test', '30', '2021-07-09T11:21:58+0000'),
+ ('test', '31', '2021-07-09T11:21:58+0000'),
+ ('test', '32', '2021-07-09T11:21:58+0000'),
+ ('test', '33', '2021-07-09T11:21:58+0000'),
+ ('test', '34', '2021-07-09T11:21:58+0000'),
+ ('test', '35', '2021-07-09T11:21:58+0000'),
+ ('test', '36', '2021-07-09T11:21:58+0000'),
+ ('test', '37', '2021-07-09T11:21:58+0000'),
+ ('test', '38', '2021-07-09T11:21:58+0000'),
+ ('test', '39', '2021-07-09T11:21:58+0000'),
+ ('test', '40', '2021-07-09T11:21:58+0000');
diff --git a/tests/test_auth.py b/tests/test_auth.py
index 4e28e93..c96d14f 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -1,9 +1,9 @@
import pytest
-from flask import g, session, json, url_for
+from flask import g, session, json, url_for, request
from sw.db import get_db
-from sw.auth import login_required, specific_user_required
+from sw.auth import login_required, specific_user_required, json_input
from werkzeug.datastructures import Headers
@@ -126,3 +126,43 @@ def test_specific_user_required_right_user(client, app, auth):
assert response.status_code == 200
assert b"Should succeed" in response.data
+
+
+@pytest.mark.parametrize(
+ ("input_data", "content_type", "expected_response"),
+ (
+ (None, None, 422),
+ ("", None, 422),
+ ("invalid JSON string", None, 422),
+ ('{ "valid": "JSON"}', None, 422),
+ (None, "", 422),
+ ("", "", 422),
+ ("invalid JSON string", "", 422),
+ ('{ "valid": "JSON"}', "", 422),
+ (None, "application/json", 400),
+ ("", "application/json", 400),
+ ("invalid JSON string", "application/json", 400),
+ ('{ "valid": "JSON"}', "application/json", 201),
+ (None, "application/ld+json", 400),
+ ("", "application/ld+json", 400),
+ ("invalid JSON string", "application/ld+json", 400),
+ ('{ "valid": "JSON"}', "application/ld+json", 201),
+ ),
+)
+def test_json_input_empty_input(
+ client, app, input_data, content_type, expected_response
+):
+ test_endpoint = "/test/dummy_endpoint"
+
+ @app.route(test_endpoint, methods=["POST"])
+ @json_input
+ def dummy_endpoint():
+ data = request.json
+ return data, 201
+
+ with client:
+ response = client.post(
+ test_endpoint, data=input_data, headers={"Content-Type": content_type}
+ )
+
+ assert response.status_code == expected_response
diff --git a/tests/test_users.py b/tests/test_users.py
new file mode 100644
index 0000000..dfd7a06
--- /dev/null
+++ b/tests/test_users.py
@@ -0,0 +1,71 @@
+import pytest
+
+from flask import url_for
+
+
+def test_get_user_info_existing_user(app, client):
+ with app.app_context():
+ user_id = url_for("users.get_user_info", username="test")
+ response = client.get(user_id)
+ assert response.status_code == 200
+ assert response.is_json
+ assert response.json.get("id") in user_id
+ assert {"inbox", "outbox"} <= set(response.json)
+ assert "password" not in response.json
+
+
+def test_get_user_info_unknown_user(app, client):
+ with app.app_context():
+ user_id = url_for("users.get_user_info", username="unknown_user")
+ response = client.get(user_id)
+ assert response.status_code == 404
+
+
+def test_get_user_inbox_unauthorised(app, client):
+ with app.app_context():
+ user_id = url_for("users.get_user_inbox", username="test")
+ response = client.get(user_id)
+ assert response.status_code == 403
+
+
+def test_get_user_inbox_authorised(app, client, auth):
+ with app.app_context():
+ inbox_id = url_for("users.get_user_inbox", username="test")
+ auth.login()
+ response = client.get(inbox_id)
+ assert response.json.get("id") in inbox_id
+ assert response.json.get("type") == "OrderedCollection"
+ assert "first" in response.json
+ assert response.json.get("totalItems") == 40
+
+
+def test_get_user_inbox_wrong_user(app, client, auth):
+ with app.app_context():
+ inbox_id = url_for("users.get_user_inbox", username="other")
+ auth.login()
+ response = client.get(inbox_id)
+ assert response.status_code == 403
+
+
+def test_get_user_inbox_page_authorised(app, client, auth):
+ with app.app_context():
+ inbox_id = url_for("users.get_user_inbox", username="test")
+ auth.login()
+ inbox = client.get(inbox_id).json
+ page_id = inbox["first"]
+ response = client.get(page_id)
+ assert {"items", "id"} <= set(response.json)
+ assert ("next" in response.json) or (
+ len(response.json["items"]) == inbox["totalItems"]
+ )
+
+
+def test_get_user_inbox_page_after_logout(app, client, auth):
+ with app.app_context():
+ inbox_id = url_for("users.get_user_inbox", username="test")
+ auth.login()
+ inbox = client.get(inbox_id).json
+ page_id = inbox["first"]
+ auth.logout()
+ response = client.get(page_id)
+ assert response.status_code == 403