import pytest from flask import url_for, session from sw.db import get_db 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 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") 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