aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/tests/api/test_post_featuring.py
blob: d83d4b6b05f634f19720b79cfd631d38e589b608 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from unittest.mock import patch

import pytest

from szurubooru import api, db, errors, model
from szurubooru.func import posts, snapshots


@pytest.fixture(autouse=True)
def inject_config(config_injector):
    config_injector(
        {
            "privileges": {
                "posts:feature": model.User.RANK_REGULAR,
                "posts:view": model.User.RANK_REGULAR,
                "posts:view:featured": model.User.RANK_REGULAR,
            },
        }
    )


def test_featuring(user_factory, post_factory, context_factory):
    auth_user = user_factory(rank=model.User.RANK_REGULAR)
    post = post_factory(id=1)
    db.session.add(post)
    db.session.flush()
    assert not posts.get_post_by_id(1).is_featured
    with patch("szurubooru.func.posts.serialize_post"), patch(
        "szurubooru.func.snapshots.modify"
    ):
        posts.serialize_post.return_value = "serialized post"
        result = api.post_api.set_featured_post(
            context_factory(params={"id": 1}, user=auth_user)
        )
        assert result == "serialized post"
        assert posts.try_get_featured_post() is not None
        assert posts.try_get_featured_post().post_id == 1
        assert posts.get_post_by_id(1).is_featured
        result = api.post_api.get_featured_post(
            context_factory(user=user_factory(rank=model.User.RANK_REGULAR))
        )
        assert result == "serialized post"
        snapshots.modify.assert_called_once_with(post, auth_user)


def test_trying_to_omit_required_parameter(user_factory, context_factory):
    with pytest.raises(errors.MissingRequiredParameterError):
        api.post_api.set_featured_post(
            context_factory(user=user_factory(rank=model.User.RANK_REGULAR))
        )


def test_trying_to_feature_the_same_post_twice(
    user_factory, post_factory, context_factory
):
    db.session.add(post_factory(id=1))
    db.session.commit()
    with patch("szurubooru.func.posts.serialize_post"), patch(
        "szurubooru.func.snapshots._post_to_webhooks"
    ):
        api.post_api.set_featured_post(
            context_factory(
                params={"id": 1},
                user=user_factory(rank=model.User.RANK_REGULAR),
            )
        )
        with pytest.raises(posts.PostAlreadyFeaturedError):
            api.post_api.set_featured_post(
                context_factory(
                    params={"id": 1},
                    user=user_factory(rank=model.User.RANK_REGULAR),
                )
            )


def test_featuring_one_post_after_another(
    user_factory, post_factory, context_factory, fake_datetime
):
    db.session.add(post_factory(id=1))
    db.session.add(post_factory(id=2))
    db.session.commit()
    assert posts.try_get_featured_post() is None
    assert not posts.get_post_by_id(1).is_featured
    assert not posts.get_post_by_id(2).is_featured
    with patch("szurubooru.func.posts.serialize_post"), patch(
        "szurubooru.func.snapshots._post_to_webhooks"
    ):
        with fake_datetime("1997"):
            api.post_api.set_featured_post(
                context_factory(
                    params={"id": 1},
                    user=user_factory(rank=model.User.RANK_REGULAR),
                )
            )
        with fake_datetime("1998"):
            api.post_api.set_featured_post(
                context_factory(
                    params={"id": 2},
                    user=user_factory(rank=model.User.RANK_REGULAR),
                )
            )
        assert posts.try_get_featured_post() is not None
        assert posts.try_get_featured_post().post_id == 2
        assert not posts.get_post_by_id(1).is_featured
        assert posts.get_post_by_id(2).is_featured


def test_trying_to_feature_non_existing(user_factory, context_factory):
    with pytest.raises(posts.PostNotFoundError):
        api.post_api.set_featured_post(
            context_factory(
                params={"id": 1},
                user=user_factory(rank=model.User.RANK_REGULAR),
            )
        )


def test_trying_to_retrieve_without_privileges(user_factory, context_factory):
    with pytest.raises(errors.AuthError):
        api.post_api.get_featured_post(
            context_factory(user=user_factory(rank=model.User.RANK_ANONYMOUS))
        )


def test_trying_to_feature_without_privileges(user_factory, context_factory):
    with pytest.raises(errors.AuthError):
        api.post_api.set_featured_post(
            context_factory(user=user_factory(rank=model.User.RANK_ANONYMOUS))
        )

© 2015 - 2026 Jakob L. Kreuze