test_views.py 5.85 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
Agate's avatar
Agate committed
7
from funkwhale_api.federation import serializers
8
from funkwhale_api.federation import utils
9 10
from funkwhale_api.federation import webfinger

Agate's avatar
Agate committed
11 12 13

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

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

Agate's avatar
Agate committed
26

Agate's avatar
Agate committed
27 28 29 30 31 32
@pytest.mark.parametrize('route,kwargs', [
    ('instance-actors-outbox', {'actor': 'library'}),
    ('instance-actors-inbox', {'actor': 'library'}),
    ('instance-actors-detail', {'actor': 'library'}),
    ('well-known-webfinger', {}),
])
33
def test_instance_endpoints_405_if_federation_disabled(
Agate's avatar
Agate committed
34 35 36 37 38 39
        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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55


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

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


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
89
        'item_serializer': serializers.AudioSerializer,
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        '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),
109
        'item_serializer': serializers.AudioSerializer,
110 111 112 113 114 115 116 117 118
        '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

119 120 121 122 123 124 125 126 127 128 129 130
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

131 132 133 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

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