Added remote library scanning logic end endpoint

This commit is contained in:
Eliot Berriot 2018-04-08 13:35:37 +02:00
commit 097707dec4
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
8 changed files with 243 additions and 6 deletions

View file

@ -0,0 +1,66 @@
from funkwhale_api.federation import library
from funkwhale_api.federation import serializers
def test_library_scan_from_account_name(mocker, factories):
actor = factories['federation.Actor'](
preferred_username='library',
domain='test.library'
)
get_resource_result = {'actor_url': actor.url}
get_resource = mocker.patch(
'funkwhale_api.federation.webfinger.get_resource',
return_value=get_resource_result)
actor_data = serializers.ActorSerializer(actor).data
actor_data['manuallyApprovesFollowers'] = False
actor_data['url'] = [{
'type': 'Link',
'name': 'library',
'mediaType': 'application/activity+json',
'href': 'https://test.library'
}]
get_actor_data = mocker.patch(
'funkwhale_api.federation.actors.get_actor_data',
return_value=actor_data)
get_library_data_result = {'test': 'test'}
get_library_data = mocker.patch(
'funkwhale_api.federation.library.get_library_data',
return_value=get_library_data_result)
result = library.scan_from_account_name('library@test.actor')
get_resource.assert_called_once_with('acct:library@test.actor')
get_actor_data.assert_called_once_with(actor.url)
get_library_data.assert_called_once_with(actor_data['url'][0]['href'])
assert result == {
'webfinger': get_resource_result,
'actor': actor_data,
'library': get_library_data_result,
}
def test_get_library_data(r_mock, factories):
actor = factories['federation.Actor']()
url = 'https://test.library'
conf = {
'id': url,
'items': [],
'actor': actor,
'page_size': 5,
}
data = serializers.PaginatedCollectionSerializer(conf).data
r_mock.get(url, json=data)
result = library.get_library_data(url)
for f in ['totalItems', 'actor', 'id', 'type']:
assert result[f] == data[f]
def test_get_library_data_requires_authentication(r_mock, factories):
url = 'https://test.library'
r_mock.get(url, status_code=403)
result = library.get_library_data(url)
assert result['errors'] == ['This library requires authentication']

View file

@ -164,3 +164,18 @@ def test_library_actor_includes_library_link(db, settings, api_client):
]
assert response.status_code == 200
assert response.data['url'] == expected_links
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')