test_views.py 10.3 KB
Newer Older
1
from django.urls import reverse
2
from django.core.paginator import Paginator
3 4 5

import pytest

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

Agate's avatar
Agate committed
13 14 15

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

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

Agate's avatar
Agate committed
28

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

    assert response.status_code == 405
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57


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

    assert response.status_code == 200
68
    assert response['Content-Type'] == 'application/jrd+json'
Agate's avatar
Agate committed
69
    assert response.data == serializer.data
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90


def test_audio_file_list_requires_authenticated_actor(
        db, settings, api_client):
    settings.FEDERATION_MUSIC_NEEDS_APPROVAL = True
    url = reverse('federation:music:files-list')
    response = api_client.get(url)

    assert response.status_code == 403


def test_audio_file_list_actor_no_page(
        db, settings, api_client, factories):
    settings.FEDERATION_MUSIC_NEEDS_APPROVAL = False
    settings.FEDERATION_COLLECTION_PAGE_SIZE = 2
    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
91
        'item_serializer': serializers.AudioSerializer,
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        '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(
        db, settings, api_client, factories):
    settings.FEDERATION_MUSIC_NEEDS_APPROVAL = False
    settings.FEDERATION_COLLECTION_PAGE_SIZE = 2
    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),
111
        'item_serializer': serializers.AudioSerializer,
112 113 114 115 116 117 118 119 120
        '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

121

122 123 124 125 126 127 128 129 130 131 132 133
def test_audio_file_list_actor_page_exclude_federated_files(
        db, settings, api_client, factories):
    settings.FEDERATION_MUSIC_NEEDS_APPROVAL = False
    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

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

def test_audio_file_list_actor_page_error(
        db, settings, api_client, factories):
    settings.FEDERATION_MUSIC_NEEDS_APPROVAL = False
    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(
        db, settings, api_client, factories):
    settings.FEDERATION_MUSIC_NEEDS_APPROVAL = False
    url = reverse('federation:music:files-list')
    response = api_client.get(url, data={'page': 5000})

    assert response.status_code == 404


def test_library_actor_includes_library_link(db, settings, api_client):
    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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183


def test_can_scan_library(superuser_api_client, mocker):
    result = {'test': 'test'}
    scan = mocker.patch(
        'funkwhale_api.federation.library.scan_from_account_name',
        return_value=result)

    url = reverse('api:v1:federation:libraries-scan')
    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')
184 185


186
def test_follow_library(superuser_api_client, mocker, factories, r_mock):
187
    library_actor = actors.SYSTEM_ACTORS['library'].get_actor_instance()
188
    actor = factories['federation.Actor']()
189
    follow = {'test': 'follow'}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    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,
    }
213 214 215

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

    assert response.status_code == 201

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

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

230 231
    on_commit.assert_called_once_with(
        activity.deliver,
232 233 234 235
        serializers.FollowSerializer(follow).data,
        on_behalf_of=library_actor,
        to=[actor.url]
    )
236 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


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
264 265 266 267 268 269 270 271 272 273 274 275 276 277


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,
    ]
278 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


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