summaryrefslogtreecommitdiff
path: root/sw
diff options
context:
space:
mode:
authorTom Harley2021-07-10 21:41:09 +0000
committerTom Harley2021-07-11 14:23:35 +0000
commitc4e848c21c028ca7c52846ea1abd7f3a38aff7d6 (patch)
treefb886b06dcdd4d9ae4cc54ee9ea9f481ec124166 /sw
parentAdd basic inbox/outbox functionality (diff)
downloadswap-master.tar.gz
swap-master.zip

Add more inbox/outbox tests

HEADmaster

Still not even total coverage!

Need to:

  • write more tests for outbox manipulation
  • look into and implement receiving content from other servers (+ write tests)

off the top of my head.

Diffstat (limited to 'sw')
-rw-r--r--sw/__init__.py7
-rw-r--r--sw/ap.py9
-rw-r--r--sw/auth.py16
-rw-r--r--sw/db.py4
-rw-r--r--sw/schema.sql34
-rw-r--r--sw/users.py20
6 files changed, 17 insertions, 73 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
diff --git a/sw/auth.py b/sw/auth.py
index 85212ec..4fd0f68 100644
--- a/sw/auth.py
+++ b/sw/auth.py
@@ -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):
diff --git a/sw/db.py b/sw/db.py
index dff8616..55c0bfe 100644
--- a/sw/db.py
+++ b/sw/db.py
@@ -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 (?,?,?) "
";",