test_views.py 2.24 KB
Newer Older
1 2 3 4
from django.urls import reverse

import pytest

Agate's avatar
Agate committed
5
from funkwhale_api.federation import actors
Agate's avatar
Agate committed
6
from funkwhale_api.federation import serializers
7 8 9
from funkwhale_api.federation import webfinger


Agate's avatar
Agate committed
10 11 12 13 14 15 16

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

    assert response.status_code == 200
Agate's avatar
Agate committed
21
    assert response.data == serializer.data
22

Agate's avatar
Agate committed
23

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


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

    assert response.status_code == 200
63
    assert response['Content-Type'] == 'application/jrd+json'
Agate's avatar
Agate committed
64
    assert response.data == serializer.data