diff options
| -rw-r--r-- | sw/__init__.py | 7 | ||||
| -rw-r--r-- | sw/ap.py | 9 | ||||
| -rw-r--r-- | sw/auth.py | 16 | ||||
| -rw-r--r-- | sw/db.py | 4 | ||||
| -rw-r--r-- | sw/schema.sql | 34 | ||||
| -rw-r--r-- | sw/users.py | 20 | ||||
| -rw-r--r-- | tests/test_auth.py | 32 | ||||
| -rw-r--r-- | tests/test_db.py | 14 | ||||
| -rw-r--r-- | tests/test_users.py | 48 |
9 files changed, 95 insertions, 89 deletions
diff --git a/sw/__init__.py b/sw/__init__.py index b4224fb..6f2869a 100644 --- a/sw/__init__.py +++ b/sw/__init__.py @@ -2,7 +2,7 @@ import os from flask import Flask -from sw import auth, ap, db, users +from sw import auth, db, users def create_app(test_config=None): @@ -23,14 +23,9 @@ def create_app(test_config=None): except OSError: pass - @app.route("/hello") - def hello(): - return "Hello, World!" - db.init_app(app) app.register_blueprint(auth.bp) - app.register_blueprint(ap.bp) app.register_blueprint(users.bp) return app diff --git a/sw/ap.py b/sw/ap.py deleted file mode 100644 index c3301f2..0000000 --- a/sw/ap.py +++ /dev/null @@ -1,9 +0,0 @@ -from flask import Blueprint - -bp = Blueprint("ap", __name__) - -bp.route("/u/<user>", methods=["GET"]) - - -def get_user(user): - return 500 @@ -57,14 +57,8 @@ def register(): data = request.json - if "username" not in data: - return "Username required", 400 - - if "password" not in data: - return "Password required", 400 - - username = data["username"] - password = data["password"] + username = data.get("username") + password = data.get("password") if username is None: return "Username required", 400 @@ -99,17 +93,15 @@ def register(): @bp.route("/login", methods=["POST"]) +@json_input def login(): - if not request.is_json: - return "Request body is not JSON", 422 - username = request.json["username"] password = request.json["password"] db = get_db() user = db.execute("select * from users where username=?", (username,)).fetchone() - if user is None: + if (user is None) or (password is None): return "Username or password incorrect", 403 if not check_password_hash(user["password"], password): @@ -18,10 +18,6 @@ def get_db(): return g.db -def get_db_cursor(): - return get_db().cursor() - - def close_db(e=None): db = g.pop("db", None) diff --git a/sw/schema.sql b/sw/schema.sql index 409729c..35aae2d 100644 --- a/sw/schema.sql +++ b/sw/schema.sql @@ -4,39 +4,7 @@ create table inbox ( recipient text, received text, - ap_attachment text, - ap_attributedTo text, - ap_audience text, - ap_content text, - ap_context text, - ap_name text, - ap_endTime text, - ap_generator text, - ap_icon text, - ap_image text, - ap_inReplyTo text, - ap_location text, - ap_preview text, - ap_published text, - ap_replies text, - ap_startTime text, - ap_summary text, - ap_tag text, - ap_updated text, - ap_url text, - ap_to text, - ap_bto text, - ap_cc text, - ap_bcc text, - ap_mediaType text, - ap_duration text, - - ap_actor text, - ap_object text, - ap_target text, - ap_result text, - ap_origin text, - ap_instrument text + ap_content text ); drop table if exists outbox; diff --git a/sw/users.py b/sw/users.py index 08302b3..71258c1 100644 --- a/sw/users.py +++ b/sw/users.py @@ -1,4 +1,4 @@ -from flask import Blueprint, jsonify, url_for, request, abort +from flask import Blueprint, jsonify, url_for, request, abort, g, Response import json @@ -45,8 +45,10 @@ def get_user_inbox(username): "select inbox_size from users where username=?", (username,) ).fetchone()[0] - if inbox_size is None: - abort(404, "User not found") + # since a logged-in user is required to access this endpoint, there + # is no way that a non-existant user can be queried (in other + # words, the user will not have the clearance to see an unreal + # user's inbox) return jsonify( { @@ -139,12 +141,12 @@ def post_user_inbox_item(username): db = get_db() time = now() - res = db.exec( - "insert into inbox (recipient, received, content) values (?,?,?) ;", - (username, time, data), + res = db.execute( + "insert into inbox (recipient, received, ap_content) values (?,?,?) ;", + (username, time, str(data)), ) - db.exec( + db.execute( "update users set inbox_size = inbox_size + 1 where id = ? ;", (g.user["id"],), ) @@ -235,7 +237,7 @@ def post_user_outbox_item(username): item_id = None if "id" not in data: res = db.exec( - "insert into items (created, updated, content) " + "insert into items (created, updated, ap_content) " "output Inserted.id " "values (?,?,?) " ";", @@ -254,7 +256,7 @@ def post_user_outbox_item(username): if not is_ap_activity(data): activity = create_wrapper(item_id) res = db.exec( - "insert into items (created, updated, content) " + "insert into items (created, updated, ap_content) " "output Inserted.id " "values (?,?,?) " ";", diff --git a/tests/test_auth.py b/tests/test_auth.py index c96d14f..96dd959 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,6 +1,6 @@ import pytest -from flask import g, session, json, url_for, request +from flask import g, json, url_for, request from sw.db import get_db from sw.auth import login_required, specific_user_required, json_input @@ -24,15 +24,6 @@ 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"), ( @@ -46,12 +37,26 @@ def test_login_right_credentials(client, auth): def test_register_validate_input(client, username, password, message): response = client.post( "/auth/register", - data=json.dumps({"username": username, "password": password,}), + data=json.dumps({"username": username, "password": password}), headers={"Content-Type": "application/json"}, ) assert message in response.data +@pytest.mark.parametrize( + ("username", "password", "expected_status_code"), + ( + ("test", "test", 200), + ("test", "other", 403), + ("test", None, 403), + (None, "test", 403), + ), +) +def test_login(client, username, auth, password, expected_status_code): + response = auth.login(username=username, password=password) + assert response.status_code == expected_status_code + + def test_login_required_no_user(client, app): test_endpoint = "/test/dummy_endpoint" @@ -63,7 +68,6 @@ def test_login_required_no_user(client, app): 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): @@ -149,9 +153,7 @@ def test_specific_user_required_right_user(client, app, auth): ('{ "valid": "JSON"}', "application/ld+json", 201), ), ) -def test_json_input_empty_input( - client, app, input_data, content_type, expected_response -): +def test_json_input(client, app, input_data, content_type, expected_response): test_endpoint = "/test/dummy_endpoint" @app.route(test_endpoint, methods=["POST"]) diff --git a/tests/test_db.py b/tests/test_db.py index 7a35593..f11a95a 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -28,3 +28,17 @@ def test_init_db_command(runner, monkeypatch): assert "Initialised" in result.output assert Recorder.called + + +def test_add_user_command(app, runner): + with app.app_context(): + db = get_db() + old_count = db.execute("select count(*) from users;").fetchone()["count(*)"] + runner.invoke(args=["add-user", "mag", "pass"]) + new_count = db.execute("select count(*) from users;").fetchone()["count(*)"] + assert new_count == old_count + 1 + + user_entries = db.execute( + "select count(*) from users where username=?", ("mag",) + ).fetchall() + assert len(user_entries) == 1 diff --git a/tests/test_users.py b/tests/test_users.py index dfd7a06..8bfd058 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -1,6 +1,8 @@ import pytest -from flask import url_for +from flask import url_for, session + +from sw.db import get_db def test_get_user_info_existing_user(app, client): @@ -69,3 +71,47 @@ def test_get_user_inbox_page_after_logout(app, client, auth): auth.logout() response = client.get(page_id) assert response.status_code == 403 + + +def test_get_user_inbox_page_latest(app, client, auth): + with app.app_context(): + target_url = url_for( + "users.get_user_inbox_page", username="test", page_index="latest" + ) + auth.login() + response = client.get(target_url) + assert response.status_code == 200 + assert response.is_json + + +# Still TODO server verification, though that will mostly be checked through from_trusted_server tests + + +@pytest.mark.parametrize( + ("payload", "expected_status_code", "expected_inbox_size_increase"), + (('{"id": "dummy_id"}', 200, 1), ("", 400, 0),), +) +def test_post_user_inbox_item( + app, client, auth, payload, expected_status_code, expected_inbox_size_increase +): + db = None + + with app.app_context(): + db = get_db() + + with client: + auth.login() + + old_inbox_size = db.execute( + "select inbox_size from users where username=?;", ("test",) + ).fetchone()["inbox_size"] + inbox_id = url_for("users.post_user_inbox_item", username="test") + response = client.post( + inbox_id, data=payload, headers={"Content-Type": "application/ld+json"} + ) + new_inbox_size = db.execute( + "select inbox_size from users where username=?;", ("test",) + ).fetchone()["inbox_size"] + + assert response.status_code == expected_status_code + assert new_inbox_size - old_inbox_size == expected_inbox_size_increase |
