From 57e7488149ee093e34774d83ffe161fb305a7dde Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 9 Jul 2021 15:16:54 +0000 Subject: 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. --- sw/auth.py | 19 ++++- sw/schema.sql | 22 ++++- sw/users.py | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++-- tests/conftest.py | 4 +- tests/data.sql | 49 ++++++++++- tests/test_auth.py | 44 +++++++++- tests/test_users.py | 71 ++++++++++++++++ 7 files changed, 423 insertions(+), 15 deletions(-) create mode 100644 tests/test_users.py diff --git a/sw/auth.py b/sw/auth.py index 2f03a04..85212ec 100644 --- a/sw/auth.py +++ b/sw/auth.py @@ -35,10 +35,25 @@ def load_logged_in_user(): ) +def json_input(view): + @functools.wraps(view) + def wrapped_view(**kwargs): + if not request.is_json: + return "Request body is not JSON", 422 + + return view(**kwargs) + + return wrapped_view + + +def from_trusted_server(view): + # TODO + return view + + @bp.route("/register", methods=["POST"]) +@json_input def register(): - if not request.is_json: - return "Request body is not JSON", 422 data = request.json diff --git a/sw/schema.sql b/sw/schema.sql index a51a7b9..409729c 100644 --- a/sw/schema.sql +++ b/sw/schema.sql @@ -2,7 +2,7 @@ drop table if exists inbox; create table inbox ( recipient text, - received int, + received text, ap_attachment text, ap_attributedTo text, @@ -39,6 +39,26 @@ create table inbox ( ap_instrument text ); +drop table if exists outbox; + +create table outbox ( + sender text, + sent text, + + ap_content text +); + +drop table if exists items; + +create table items ( + id integer primary key, + + created text, + updated text, + + content text +); + drop table if exists users; create table users ( diff --git a/sw/users.py b/sw/users.py index dffae93..08302b3 100644 --- a/sw/users.py +++ b/sw/users.py @@ -4,7 +4,7 @@ import json from sw.db import get_db from sw.time import now -from sw.auth import specific_user_required +from sw.auth import specific_user_required, json_input, from_trusted_server bp = Blueprint("users", __name__, url_prefix="/u") @@ -17,7 +17,7 @@ def ap(r): res["id"] = url_for(".get_user_info", username=r["username"]) res["inbox"] = url_for(".get_user_inbox", username=r["username"]) - # res["outbox"] = url_for(".get_user_outbox", username=r["username"]) + res["outbox"] = url_for(".get_user_outbox", username=r["username"]) # res["following"] = url_for(".get_user_following", username=r["username"]) # res["followers"] = url_for(".get_user_followers", username=r["username"]) @@ -48,13 +48,230 @@ def get_user_inbox(username): if inbox_size is None: abort(404, "User not found") - request_time = request.date or now() - return jsonify( { + "id": url_for(".get_user_inbox", username=username), "type": "OrderedCollection", "totalItems": inbox_size, - "first": url_for(".get_user_inbox", username=username, before=request_time), - "current": url_for(".get_user_inbox", username=username), + "first": url_for( + ".get_user_inbox_page", username=username, page_index=now() + ), + # "last": url_for(".get_user_inbox_page", username=username, page=1), + } + ) + + +@bp.route("//inbox/") +@specific_user_required +def get_user_inbox_page(username, page_index): + db = get_db() + + inbox_fetch_limit = 10 # TODO: give this variable a home + + if page_index == "latest": + page_index = now() + + res = db.execute( + "select * from inbox " + "where recipient=? " + "and received/inbox", methods=["POST"]) +@from_trusted_server +@json_input +def post_user_inbox_item(username): + data = request.json + db = get_db() + time = now() + + res = db.exec( + "insert into inbox (recipient, received, content) values (?,?,?) ;", + (username, time, data), + ) + + db.exec( + "update users set inbox_size = inbox_size + 1 where id = ? ;", (g.user["id"],), + ) + + db.commit() + + return Response(status=200) + + +@bp.route("//outbox") +def get_user_outbox(username): + db = get_db() + + outbox_size = db.execute( + "select outbox_size from users where username=?", (username,) + ).fetchone()["outbox_size"] + + if outbox_size is None: + abort(404, "User not found") + + # TODO: outbox contents not filtered by recipient data + + return jsonify( + { + "id": url_for(".get_user_inbox", username=username), + "type": "OrderedCollection", + "totalItems": outbox_size, + "first": url_for( + ".get_user_outbox_page", username=username, page_index=now() + ), + } + ) + + +@bp.route("/username/outbox/") +def get_user_outbox_page(username, page_index): + db = get_db() + + outbox_fetch_limit = 10 # TODO: give this variable a home + + if page_index == "latest": + page_index = now() + + res = db.execute( + "select * from outbox " + "where sender=? " + "and sent/outbox", methods=["POST"]) +@specific_user_required +@json_input +def post_user_outbox_item(username): + data = request.json + db = get_db() + time = now() + + item_id = None + if "id" not in data: + res = db.exec( + "insert into items (created, updated, content) " + "output Inserted.id " + "values (?,?,?) " + ";", + time, + time, + data, + ) + + item_id = res.fetchone()["id"] + + db.exec( + "update users set outbox_size = outbox_size + 1 where id = ? ;", + (g.user["id"],), + ) + + if not is_ap_activity(data): + activity = create_wrapper(item_id) + res = db.exec( + "insert into items (created, updated, content) " + "output Inserted.id " + "values (?,?,?) " + ";", + time, + time, + activity, + ) + + item_id = res.fetchone()["id"] + + db.exec( + "update users set outbox_size = outbox_size + 1 where id = ? ;", + (g.user["id"],), + ) + + db.commit() + + # TODO: get a background thread(?) to process new outbox contents + + return Response(status=201, headers={"Location": get_item_ap_id(item_id)}) 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 -- cgit v1.2.1