test_views.py 12.8 KB
Newer Older
1
from django.core.paginator import Paginator
2 3
from django.urls import reverse
from django.utils import timezone
4 5 6

import pytest

Agate's avatar
Agate committed
7
from funkwhale_api.federation import actors
8
from funkwhale_api.federation import activity
9
from funkwhale_api.federation import models
Agate's avatar
Agate committed
10
from funkwhale_api.federation import serializers
11
from funkwhale_api.federation import utils
12 13
from funkwhale_api.federation import webfinger

Agate's avatar
Agate committed
14 15

@pytest.mark.parametrize('system_actor', actors.SYSTEM_ACTORS.keys())
16
def test_instance_actors(system_actor, db, api_client):
17
    actor = actors.SYSTEM_ACTORS[system_actor].get_actor_instance()
Agate's avatar
Agate committed
18 19 20
    url = reverse(
        'federation:instance-actors-detail',
        kwargs={'actor': system_actor})
21
    response = api_client.get(url)
Agate's avatar
Agate committed
22
    serializer = serializers.ActorSerializer(actor)
Agate's avatar
Agate committed
23

24 25
    if system_actor == 'library':
        response.data.pop('url')
Agate's avatar
Agate committed
26
    assert response.status_code == 200
Agate's avatar
Agate committed
27
    assert response.data == serializer.data
28

Agate's avatar
Agate committed
29

Agate's avatar
Agate committed
30 31 32 33 34 35
@pytest.mark.parametrize('route,kwargs', [
    ('instance-actors-outbox', {'actor': 'library'}),
    ('instance-actors-inbox', {'actor': 'library'}),
    ('instance-actors-detail', {'actor': 'library'}),
    ('well-known-webfinger', {}),
])
36
def test_instance_endpoints_405_if_federation_disabled(
37 38
        authenticated_actor, db, preferences, api_client, route, kwargs):
    preferences['federation__enabled'] = False
Agate's avatar
Agate committed
39 40 41 42
    url = reverse('federation:{}'.format(route), kwargs=kwargs)
    response = api_client.get(url)

    assert response.status_code == 405
43 44 45


def test_wellknown_webfinger_validates_resource(
Agate's avatar
Agate committed
46
        db, api_client, settings, mocker):
47 48 49 50 51 52 53 54 55 56 57 58
    clean = mocker.spy(webfinger, 'clean_resource')
    url = reverse('federation:well-known-webfinger')
    response = api_client.get(url, data={'resource': 'something'})

    clean.assert_called_once_with('something')
    assert url == '/.well-known/webfinger'
    assert response.status_code == 400
    assert response.data['errors']['resource'] == (
        'Missing webfinger resource type'
    )


Agate's avatar
Agate committed
59
@pytest.mark.parametrize('system_actor', actors.SYSTEM_ACTORS.keys())
60
def test_wellknown_webfinger_system(
Agate's avatar
Agate committed
61
        system_actor, db, api_client, settings, mocker):
62
    actor = actors.SYSTEM_ACTORS[system_actor].get_actor_instance()
63 64
    url = reverse('federation:well-known-webfinger')
    response = api_client.get(
Agate's avatar
Agate committed
65 66
        url, data={'resource': 'acct:{}'.format(actor.webfinger_subject)})
    serializer = serializers.ActorWebfingerSerializer(actor)
67 68

    assert response.status_code == 200
69
    assert response['Content-Type'] == 'application/jrd+json'
Agate's avatar
Agate committed
70
    assert response.data == serializer.data
71 72 73


def test_audio_file_list_requires_authenticated_actor(
74 75
        db, preferences, api_client):
    preferences['federation__music_needs_approval'] = True
76 77 78 79 80 81 82
    url = reverse('federation:music:files-list')
    response = api_client.get(url)

    assert response.status_code == 403


def test_audio_file_list_actor_no_page(
83 84 85
        db, preferences, api_client, factories):
    preferences['federation__music_needs_approval'] = False
    preferences['federation__collection_page_size'] = 2
86 87 88 89 90 91
    library = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    tfs = factories['music.TrackFile'].create_batch(size=5)
    conf = {
        'id': utils.full_url(reverse('federation:music:files-list')),
        'page_size': 2,
        'items': list(reversed(tfs)),  # we order by -creation_date
92
        'item_serializer': serializers.AudioSerializer,
93 94 95 96 97 98 99 100 101 102 103
        'actor': library
    }
    expected = serializers.PaginatedCollectionSerializer(conf).data
    url = reverse('federation:music:files-list')
    response = api_client.get(url)

    assert response.status_code == 200
    assert response.data == expected


def test_audio_file_list_actor_page(
104 105 106
        db, preferences, api_client, factories):
    preferences['federation__music_needs_approval'] = False
    preferences['federation__collection_page_size'] = 2
107 108 109 110 111
    library = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    tfs = factories['music.TrackFile'].create_batch(size=5)
    conf = {
        'id': utils.full_url(reverse('federation:music:files-list')),
        'page': Paginator(list(reversed(tfs)), 2).page(2),
112
        'item_serializer': serializers.AudioSerializer,
113 114 115 116 117 118 119 120 121
        'actor': library
    }
    expected = serializers.CollectionPageSerializer(conf).data
    url = reverse('federation:music:files-list')
    response = api_client.get(url, data={'page': 2})

    assert response.status_code == 200
    assert response.data == expected

122

123
def test_audio_file_list_actor_page_exclude_federated_files(
124 125
        db, preferences, api_client, factories):
    preferences['federation__music_needs_approval'] = False
126 127 128 129 130 131 132 133 134
    library = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    tfs = factories['music.TrackFile'].create_batch(size=5, federation=True)

    url = reverse('federation:music:files-list')
    response = api_client.get(url)

    assert response.status_code == 200
    assert response.data['totalItems'] == 0

135 136

def test_audio_file_list_actor_page_error(
137 138
        db, preferences, api_client, factories):
    preferences['federation__music_needs_approval'] = False
139 140 141 142 143 144 145
    url = reverse('federation:music:files-list')
    response = api_client.get(url, data={'page': 'nope'})

    assert response.status_code == 400


def test_audio_file_list_actor_page_error_too_far(
146 147
        db, preferences, api_client, factories):
    preferences['federation__music_needs_approval'] = False
148 149 150 151 152 153
    url = reverse('federation:music:files-list')
    response = api_client.get(url, data={'page': 5000})

    assert response.status_code == 404


154
def test_library_actor_includes_library_link(db, preferences, api_client):
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    actor = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    url = reverse(
        'federation:instance-actors-detail',
        kwargs={'actor': 'library'})
    response = api_client.get(url)
    expected_links = [
        {
            'type': 'Link',
            'name': 'library',
            'mediaType': 'application/activity+json',
            'href': utils.full_url(reverse('federation:music:files-list'))
        }
    ]
    assert response.status_code == 200
    assert response.data['url'] == expected_links
170 171


172
def test_can_fetch_library(superuser_api_client, mocker):
173 174 175 176 177
    result = {'test': 'test'}
    scan = mocker.patch(
        'funkwhale_api.federation.library.scan_from_account_name',
        return_value=result)

178
    url = reverse('api:v1:federation:libraries-fetch')
179 180 181 182 183 184
    response = superuser_api_client.get(
        url, data={'account': 'test@test.library'})

    assert response.status_code == 200
    assert response.data == result
    scan.assert_called_once_with('test@test.library')
185 186


187
def test_follow_library(superuser_api_client, mocker, factories, r_mock):
188
    library_actor = actors.SYSTEM_ACTORS['library'].get_actor_instance()
189
    actor = factories['federation.Actor']()
190
    follow = {'test': 'follow'}
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    on_commit = mocker.patch(
        'funkwhale_api.common.utils.on_commit')
    actor_data = serializers.ActorSerializer(actor).data
    actor_data['url'] = [{
        'href': 'https://test.library',
        'name': 'library',
        'type': 'Link',
    }]
    library_conf = {
        'id': 'https://test.library',
        'items': range(10),
        'actor': actor,
        'page_size': 5,
    }
    library_data = serializers.PaginatedCollectionSerializer(library_conf).data
    r_mock.get(actor.url, json=actor_data)
    r_mock.get('https://test.library', json=library_data)
    data = {
        'actor': actor.url,
        'autoimport': False,
        'federation_enabled': True,
        'download_files': False,
    }
214 215 216

    url = reverse('api:v1:federation:libraries-list')
    response = superuser_api_client.post(
217
        url, data)
218 219 220 221 222 223 224 225

    assert response.status_code == 201

    follow = models.Follow.objects.get(
        actor=library_actor,
        target=actor,
        approved=None,
    )
226 227 228 229
    library = follow.library

    assert response.data == serializers.APILibraryCreateSerializer(
        library).data
230

231 232
    on_commit.assert_called_once_with(
        activity.deliver,
233 234 235 236
        serializers.FollowSerializer(follow).data,
        on_behalf_of=library_actor,
        to=[actor.url]
    )
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264


def test_can_list_system_actor_following(factories, superuser_api_client):
    library_actor = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    follow1 = factories['federation.Follow'](actor=library_actor)
    follow2 = factories['federation.Follow']()

    url = reverse('api:v1:federation:libraries-following')
    response = superuser_api_client.get(url)

    assert response.status_code == 200
    assert response.data['results'] == [
        serializers.APIFollowSerializer(follow1).data
    ]


def test_can_list_system_actor_followers(factories, superuser_api_client):
    library_actor = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    follow1 = factories['federation.Follow'](actor=library_actor)
    follow2 = factories['federation.Follow'](target=library_actor)

    url = reverse('api:v1:federation:libraries-followers')
    response = superuser_api_client.get(url)

    assert response.status_code == 200
    assert response.data['results'] == [
        serializers.APIFollowSerializer(follow2).data
    ]
Agate's avatar
Agate committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278


def test_can_list_libraries(factories, superuser_api_client):
    library1 = factories['federation.Library']()
    library2 = factories['federation.Library']()

    url = reverse('api:v1:federation:libraries-list')
    response = superuser_api_client.get(url)

    assert response.status_code == 200
    assert response.data['results'] == [
        serializers.APILibrarySerializer(library1).data,
        serializers.APILibrarySerializer(library2).data,
    ]
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309


def test_can_detail_library(factories, superuser_api_client):
    library = factories['federation.Library']()

    url = reverse(
        'api:v1:federation:libraries-detail',
        kwargs={'uuid': str(library.uuid)})
    response = superuser_api_client.get(url)

    assert response.status_code == 200
    assert response.data == serializers.APILibrarySerializer(library).data


def test_can_patch_library(factories, superuser_api_client):
    library = factories['federation.Library']()
    data = {
        'federation_enabled': not library.federation_enabled,
        'download_files': not library.download_files,
        'autoimport': not library.autoimport,
    }
    url = reverse(
        'api:v1:federation:libraries-detail',
        kwargs={'uuid': str(library.uuid)})
    response = superuser_api_client.patch(url, data)

    assert response.status_code == 200
    library.refresh_from_db()

    for k, v in data.items():
        assert getattr(library, k) == v
310 311 312 313 314


def test_scan_library(factories, mocker, superuser_api_client):
    scan = mocker.patch(
        'funkwhale_api.federation.tasks.scan_library.delay',
Agate's avatar
Agate committed
315
        return_value=mocker.Mock(id='id'))
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    library = factories['federation.Library']()
    now = timezone.now()
    data = {
        'until': now,
    }
    url = reverse(
        'api:v1:federation:libraries-scan',
        kwargs={'uuid': str(library.uuid)})
    response = superuser_api_client.post(url, data)

    assert response.status_code == 200
    assert response.data == {'task': 'id'}
    scan.assert_called_once_with(
        library_id=library.pk,
        until=now
    )
Agate's avatar
Agate committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348


def test_list_library_tracks(factories, superuser_api_client):
    library = factories['federation.Library']()
    lts = list(reversed(factories['federation.LibraryTrack'].create_batch(
        size=5, library=library)))
    factories['federation.LibraryTrack'].create_batch(size=5)
    url = reverse('api:v1:federation:library-tracks-list')
    response = superuser_api_client.get(url, {'library': library.uuid})

    assert response.status_code == 200
    assert response.data == {
        'results': serializers.APILibraryTrackSerializer(lts, many=True).data,
        'count': 5,
        'previous': None,
        'next': None,
    }
Agate's avatar
Agate committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382


def test_can_update_follow_status(factories, superuser_api_client, mocker):
    patched_accept = mocker.patch(
        'funkwhale_api.federation.activity.accept_follow'
    )
    library_actor = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    follow = factories['federation.Follow'](target=library_actor)

    payload = {
        'follow': follow.pk,
        'approved': True
    }
    url = reverse('api:v1:federation:libraries-followers')
    response = superuser_api_client.patch(url, payload)
    follow.refresh_from_db()

    assert response.status_code == 200
    assert follow.approved is True
    patched_accept.assert_called_once_with(follow)


def test_can_filter_pending_follows(factories, superuser_api_client):
    library_actor = actors.SYSTEM_ACTORS['library'].get_actor_instance()
    follow = factories['federation.Follow'](
        target=library_actor,
        approved=True)

    params = {'pending': True}
    url = reverse('api:v1:federation:libraries-followers')
    response = superuser_api_client.get(url, params)

    assert response.status_code == 200
    assert len(response.data['results']) == 0