test_nodeinfo.py 6.26 KB
Newer Older
1
2
import pytest

3
4
import funkwhale_api
from funkwhale_api.instance import nodeinfo
Eliot Berriot's avatar
Eliot Berriot committed
5
from funkwhale_api.federation import actors
6
from funkwhale_api.music import utils as music_utils
7
8
9


def test_nodeinfo_dump(preferences, mocker):
Eliot Berriot's avatar
Eliot Berriot committed
10
    preferences["instance__nodeinfo_stats_enabled"] = True
Eliot Berriot's avatar
Eliot Berriot committed
11
12
13
14
15
16
    preferences["moderation__unauthenticated_report_types"] = [
        "takedown_request",
        "other",
        "other_category_that_doesnt_exist",
    ]

17
    stats = {
18
        "users": {"total": 1, "active_halfyear": 12, "active_month": 13},
Eliot Berriot's avatar
Eliot Berriot committed
19
20
21
22
23
24
        "tracks": 2,
        "albums": 3,
        "artists": 4,
        "track_favorites": 5,
        "music_duration": 6,
        "listenings": 7,
25
    }
Eliot Berriot's avatar
Eliot Berriot committed
26
    mocker.patch("funkwhale_api.instance.stats.get", return_value=stats)
27
28

    expected = {
Eliot Berriot's avatar
Eliot Berriot committed
29
30
31
32
33
        "version": "2.0",
        "software": {"name": "funkwhale", "version": funkwhale_api.__version__},
        "protocols": ["activitypub"],
        "services": {"inbound": [], "outbound": []},
        "openRegistrations": preferences["users__registration_enabled"],
34
        "usage": {"users": {"total": 1, "activeHalfyear": 12, "activeMonth": 13}},
Eliot Berriot's avatar
Eliot Berriot committed
35
        "metadata": {
Eliot Berriot's avatar
Eliot Berriot committed
36
            "actorId": actors.get_service_actor().fid,
Eliot Berriot's avatar
Eliot Berriot committed
37
38
39
40
            "private": preferences["instance__nodeinfo_private"],
            "shortDescription": preferences["instance__short_description"],
            "longDescription": preferences["instance__long_description"],
            "nodeName": preferences["instance__name"],
41
            "terms": preferences["instance__terms"],
Eliot Berriot's avatar
Eliot Berriot committed
42
43
44
45
46
            "library": {
                "federationEnabled": preferences["federation__enabled"],
                "federationNeedsApproval": preferences[
                    "federation__music_needs_approval"
                ],
47
                "anonymousCanListen": not preferences[
Eliot Berriot's avatar
Eliot Berriot committed
48
49
50
51
52
53
                    "common__api_authentication_required"
                ],
                "tracks": {"total": stats["tracks"]},
                "artists": {"total": stats["artists"]},
                "albums": {"total": stats["albums"]},
                "music": {"hours": stats["music_duration"]},
54
            },
Eliot Berriot's avatar
Eliot Berriot committed
55
56
57
58
            "usage": {
                "favorites": {"tracks": {"total": stats["track_favorites"]}},
                "listenings": {"total": stats["listenings"]},
            },
59
            "supportedUploadExtensions": music_utils.SUPPORTED_EXTENSIONS,
60
            "allowList": {"enabled": False, "domains": None},
Eliot Berriot's avatar
Eliot Berriot committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            "reportTypes": [
                {
                    "type": "takedown_request",
                    "label": "Takedown request",
                    "anonymous": True,
                },
                {
                    "type": "invalid_metadata",
                    "label": "Invalid metadata",
                    "anonymous": False,
                },
                {
                    "type": "illegal_content",
                    "label": "Illegal content",
                    "anonymous": False,
                },
                {
                    "type": "offensive_content",
                    "label": "Offensive content",
                    "anonymous": False,
                },
                {"type": "other", "label": "Other", "anonymous": True},
            ],
Eliot Berriot's avatar
Eliot Berriot committed
84
        },
85
86
87
88
89
    }
    assert nodeinfo.get() == expected


def test_nodeinfo_dump_stats_disabled(preferences, mocker):
Eliot Berriot's avatar
Eliot Berriot committed
90
    preferences["instance__nodeinfo_stats_enabled"] = False
Eliot Berriot's avatar
Eliot Berriot committed
91
92
93
94
    preferences["moderation__unauthenticated_report_types"] = [
        "takedown_request",
        "other",
    ]
95
96

    expected = {
Eliot Berriot's avatar
Eliot Berriot committed
97
98
99
100
101
        "version": "2.0",
        "software": {"name": "funkwhale", "version": funkwhale_api.__version__},
        "protocols": ["activitypub"],
        "services": {"inbound": [], "outbound": []},
        "openRegistrations": preferences["users__registration_enabled"],
102
        "usage": {"users": {"total": 0, "activeHalfyear": 0, "activeMonth": 0}},
Eliot Berriot's avatar
Eliot Berriot committed
103
        "metadata": {
Eliot Berriot's avatar
Eliot Berriot committed
104
            "actorId": actors.get_service_actor().fid,
Eliot Berriot's avatar
Eliot Berriot committed
105
106
107
108
            "private": preferences["instance__nodeinfo_private"],
            "shortDescription": preferences["instance__short_description"],
            "longDescription": preferences["instance__long_description"],
            "nodeName": preferences["instance__name"],
109
            "terms": preferences["instance__terms"],
Eliot Berriot's avatar
Eliot Berriot committed
110
111
112
113
114
            "library": {
                "federationEnabled": preferences["federation__enabled"],
                "federationNeedsApproval": preferences[
                    "federation__music_needs_approval"
                ],
115
                "anonymousCanListen": not preferences[
Eliot Berriot's avatar
Eliot Berriot committed
116
117
                    "common__api_authentication_required"
                ],
118
            },
119
            "supportedUploadExtensions": music_utils.SUPPORTED_EXTENSIONS,
120
            "allowList": {"enabled": False, "domains": None},
Eliot Berriot's avatar
Eliot Berriot committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
            "reportTypes": [
                {
                    "type": "takedown_request",
                    "label": "Takedown request",
                    "anonymous": True,
                },
                {
                    "type": "invalid_metadata",
                    "label": "Invalid metadata",
                    "anonymous": False,
                },
                {
                    "type": "illegal_content",
                    "label": "Illegal content",
                    "anonymous": False,
                },
                {
                    "type": "offensive_content",
                    "label": "Offensive content",
                    "anonymous": False,
                },
                {"type": "other", "label": "Other", "anonymous": True},
            ],
Eliot Berriot's avatar
Eliot Berriot committed
144
        },
145
146
    }
    assert nodeinfo.get() == expected
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164


@pytest.mark.parametrize(
    "enabled, public, expected",
    [
        (True, True, {"enabled": True, "domains": ["allowed.example"]}),
        (True, False, {"enabled": True, "domains": None}),
        (False, False, {"enabled": False, "domains": None}),
    ],
)
def test_nodeinfo_allow_list_enabled(preferences, factories, enabled, public, expected):
    preferences["moderation__allow_list_enabled"] = enabled
    preferences["moderation__allow_list_public"] = public
    factories["federation.Domain"](name="allowed.example", allowed=True)
    factories["federation.Domain"](allowed=False)
    factories["federation.Domain"](allowed=None)

    assert nodeinfo.get()["metadata"]["allowList"] == expected