summaryrefslogtreecommitdiff
path: root/tests/test_users.py
blob: e75492a11b6f386783856141e6ca5908f9360d9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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