diff options
| author | Tom Harley | 2021-07-10 21:41:09 +0000 |
|---|---|---|
| committer | Tom Harley | 2021-07-11 14:23:35 +0000 |
| commit | c4e848c21c028ca7c52846ea1abd7f3a38aff7d6 (patch) | |
| tree | fb886b06dcdd4d9ae4cc54ee9ea9f481ec124166 /tests | |
| parent | Add basic inbox/outbox functionality (diff) | |
| download | swap-master.tar.gz swap-master.zip | |
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 'tests')
| -rw-r--r-- | tests/test_auth.py | 32 | ||||
| -rw-r--r-- | tests/test_db.py | 14 | ||||
| -rw-r--r-- | tests/test_users.py | 48 |
3 files changed, 78 insertions, 16 deletions
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 |
