Blacked the code
This commit is contained in:
parent
b6fc0051fa
commit
62ca3bd736
279 changed files with 8890 additions and 9556 deletions
|
|
@ -6,102 +6,76 @@ from funkwhale_api.instance import nodeinfo
|
|||
|
||||
|
||||
def test_nodeinfo_dump(preferences, mocker):
|
||||
preferences['instance__nodeinfo_stats_enabled'] = True
|
||||
preferences["instance__nodeinfo_stats_enabled"] = True
|
||||
stats = {
|
||||
'users': 1,
|
||||
'tracks': 2,
|
||||
'albums': 3,
|
||||
'artists': 4,
|
||||
'track_favorites': 5,
|
||||
'music_duration': 6,
|
||||
'listenings': 7,
|
||||
"users": 1,
|
||||
"tracks": 2,
|
||||
"albums": 3,
|
||||
"artists": 4,
|
||||
"track_favorites": 5,
|
||||
"music_duration": 6,
|
||||
"listenings": 7,
|
||||
}
|
||||
mocker.patch('funkwhale_api.instance.stats.get', return_value=stats)
|
||||
mocker.patch("funkwhale_api.instance.stats.get", return_value=stats)
|
||||
|
||||
expected = {
|
||||
'version': '2.0',
|
||||
'software': {
|
||||
'name': 'funkwhale',
|
||||
'version': funkwhale_api.__version__
|
||||
},
|
||||
'protocols': ['activitypub'],
|
||||
'services': {
|
||||
'inbound': [],
|
||||
'outbound': []
|
||||
},
|
||||
'openRegistrations': preferences['users__registration_enabled'],
|
||||
'usage': {
|
||||
'users': {
|
||||
'total': stats['users'],
|
||||
}
|
||||
},
|
||||
'metadata': {
|
||||
'private': preferences['instance__nodeinfo_private'],
|
||||
'shortDescription': preferences['instance__short_description'],
|
||||
'longDescription': preferences['instance__long_description'],
|
||||
'nodeName': preferences['instance__name'],
|
||||
'library': {
|
||||
'federationEnabled': preferences['federation__enabled'],
|
||||
'federationNeedsApproval': preferences['federation__music_needs_approval'],
|
||||
'anonymousCanListen': preferences['common__api_authentication_required'],
|
||||
'tracks': {
|
||||
'total': stats['tracks'],
|
||||
},
|
||||
'artists': {
|
||||
'total': stats['artists'],
|
||||
},
|
||||
'albums': {
|
||||
'total': stats['albums'],
|
||||
},
|
||||
'music': {
|
||||
'hours': stats['music_duration']
|
||||
},
|
||||
"version": "2.0",
|
||||
"software": {"name": "funkwhale", "version": funkwhale_api.__version__},
|
||||
"protocols": ["activitypub"],
|
||||
"services": {"inbound": [], "outbound": []},
|
||||
"openRegistrations": preferences["users__registration_enabled"],
|
||||
"usage": {"users": {"total": stats["users"]}},
|
||||
"metadata": {
|
||||
"private": preferences["instance__nodeinfo_private"],
|
||||
"shortDescription": preferences["instance__short_description"],
|
||||
"longDescription": preferences["instance__long_description"],
|
||||
"nodeName": preferences["instance__name"],
|
||||
"library": {
|
||||
"federationEnabled": preferences["federation__enabled"],
|
||||
"federationNeedsApproval": preferences[
|
||||
"federation__music_needs_approval"
|
||||
],
|
||||
"anonymousCanListen": preferences[
|
||||
"common__api_authentication_required"
|
||||
],
|
||||
"tracks": {"total": stats["tracks"]},
|
||||
"artists": {"total": stats["artists"]},
|
||||
"albums": {"total": stats["albums"]},
|
||||
"music": {"hours": stats["music_duration"]},
|
||||
},
|
||||
'usage': {
|
||||
'favorites': {
|
||||
'tracks': {
|
||||
'total': stats['track_favorites'],
|
||||
}
|
||||
},
|
||||
'listenings': {
|
||||
'total': stats['listenings']
|
||||
}
|
||||
}
|
||||
}
|
||||
"usage": {
|
||||
"favorites": {"tracks": {"total": stats["track_favorites"]}},
|
||||
"listenings": {"total": stats["listenings"]},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert nodeinfo.get() == expected
|
||||
|
||||
|
||||
def test_nodeinfo_dump_stats_disabled(preferences, mocker):
|
||||
preferences['instance__nodeinfo_stats_enabled'] = False
|
||||
preferences["instance__nodeinfo_stats_enabled"] = False
|
||||
|
||||
expected = {
|
||||
'version': '2.0',
|
||||
'software': {
|
||||
'name': 'funkwhale',
|
||||
'version': funkwhale_api.__version__
|
||||
},
|
||||
'protocols': ['activitypub'],
|
||||
'services': {
|
||||
'inbound': [],
|
||||
'outbound': []
|
||||
},
|
||||
'openRegistrations': preferences['users__registration_enabled'],
|
||||
'usage': {
|
||||
'users': {
|
||||
'total': 0,
|
||||
}
|
||||
},
|
||||
'metadata': {
|
||||
'private': preferences['instance__nodeinfo_private'],
|
||||
'shortDescription': preferences['instance__short_description'],
|
||||
'longDescription': preferences['instance__long_description'],
|
||||
'nodeName': preferences['instance__name'],
|
||||
'library': {
|
||||
'federationEnabled': preferences['federation__enabled'],
|
||||
'federationNeedsApproval': preferences['federation__music_needs_approval'],
|
||||
'anonymousCanListen': preferences['common__api_authentication_required'],
|
||||
"version": "2.0",
|
||||
"software": {"name": "funkwhale", "version": funkwhale_api.__version__},
|
||||
"protocols": ["activitypub"],
|
||||
"services": {"inbound": [], "outbound": []},
|
||||
"openRegistrations": preferences["users__registration_enabled"],
|
||||
"usage": {"users": {"total": 0}},
|
||||
"metadata": {
|
||||
"private": preferences["instance__nodeinfo_private"],
|
||||
"shortDescription": preferences["instance__short_description"],
|
||||
"longDescription": preferences["instance__long_description"],
|
||||
"nodeName": preferences["instance__name"],
|
||||
"library": {
|
||||
"federationEnabled": preferences["federation__enabled"],
|
||||
"federationNeedsApproval": preferences[
|
||||
"federation__music_needs_approval"
|
||||
],
|
||||
"anonymousCanListen": preferences[
|
||||
"common__api_authentication_required"
|
||||
],
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
assert nodeinfo.get() == expected
|
||||
|
|
|
|||
|
|
@ -6,12 +6,13 @@ from dynamic_preferences.api import serializers
|
|||
|
||||
|
||||
def test_can_list_settings_via_api(preferences, api_client):
|
||||
url = reverse('api:v1:instance:settings')
|
||||
url = reverse("api:v1:instance:settings")
|
||||
all_preferences = preferences.model.objects.all()
|
||||
expected_preferences = {
|
||||
p.preference.identifier(): p
|
||||
for p in all_preferences
|
||||
if getattr(p.preference, 'show_in_api', False)}
|
||||
if getattr(p.preference, "show_in_api", False)
|
||||
}
|
||||
|
||||
assert len(expected_preferences) > 0
|
||||
|
||||
|
|
@ -20,15 +21,18 @@ def test_can_list_settings_via_api(preferences, api_client):
|
|||
assert len(response.data) == len(expected_preferences)
|
||||
|
||||
for p in response.data:
|
||||
i = '__'.join([p['section'], p['name']])
|
||||
i = "__".join([p["section"], p["name"]])
|
||||
assert i in expected_preferences
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pref,value', [
|
||||
('instance__name', 'My instance'),
|
||||
('instance__short_description', 'For music lovers'),
|
||||
('instance__long_description', 'For real music lovers'),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"pref,value",
|
||||
[
|
||||
("instance__name", "My instance"),
|
||||
("instance__short_description", "For music lovers"),
|
||||
("instance__long_description", "For real music lovers"),
|
||||
],
|
||||
)
|
||||
def test_instance_settings(pref, value, preferences):
|
||||
preferences[pref] = value
|
||||
|
||||
|
|
|
|||
|
|
@ -4,14 +4,13 @@ from funkwhale_api.instance import stats
|
|||
|
||||
|
||||
def test_get_users(mocker):
|
||||
mocker.patch(
|
||||
'funkwhale_api.users.models.User.objects.count', return_value=42)
|
||||
mocker.patch("funkwhale_api.users.models.User.objects.count", return_value=42)
|
||||
|
||||
assert stats.get_users() == 42
|
||||
|
||||
|
||||
def test_get_music_duration(factories):
|
||||
factories['music.TrackFile'].create_batch(size=5, duration=360)
|
||||
factories["music.TrackFile"].create_batch(size=5, duration=360)
|
||||
|
||||
# duration is in hours
|
||||
assert stats.get_music_duration() == 0.5
|
||||
|
|
@ -19,56 +18,48 @@ def test_get_music_duration(factories):
|
|||
|
||||
def test_get_listenings(mocker):
|
||||
mocker.patch(
|
||||
'funkwhale_api.history.models.Listening.objects.count',
|
||||
return_value=42)
|
||||
"funkwhale_api.history.models.Listening.objects.count", return_value=42
|
||||
)
|
||||
assert stats.get_listenings() == 42
|
||||
|
||||
|
||||
def test_get_track_favorites(mocker):
|
||||
mocker.patch(
|
||||
'funkwhale_api.favorites.models.TrackFavorite.objects.count',
|
||||
return_value=42)
|
||||
"funkwhale_api.favorites.models.TrackFavorite.objects.count", return_value=42
|
||||
)
|
||||
assert stats.get_track_favorites() == 42
|
||||
|
||||
|
||||
def test_get_tracks(mocker):
|
||||
mocker.patch(
|
||||
'funkwhale_api.music.models.Track.objects.count',
|
||||
return_value=42)
|
||||
mocker.patch("funkwhale_api.music.models.Track.objects.count", return_value=42)
|
||||
assert stats.get_tracks() == 42
|
||||
|
||||
|
||||
def test_get_albums(mocker):
|
||||
mocker.patch(
|
||||
'funkwhale_api.music.models.Album.objects.count',
|
||||
return_value=42)
|
||||
mocker.patch("funkwhale_api.music.models.Album.objects.count", return_value=42)
|
||||
assert stats.get_albums() == 42
|
||||
|
||||
|
||||
def test_get_artists(mocker):
|
||||
mocker.patch(
|
||||
'funkwhale_api.music.models.Artist.objects.count',
|
||||
return_value=42)
|
||||
mocker.patch("funkwhale_api.music.models.Artist.objects.count", return_value=42)
|
||||
assert stats.get_artists() == 42
|
||||
|
||||
|
||||
def test_get(mocker):
|
||||
keys = [
|
||||
'users',
|
||||
'tracks',
|
||||
'albums',
|
||||
'artists',
|
||||
'track_favorites',
|
||||
'listenings',
|
||||
'music_duration',
|
||||
"users",
|
||||
"tracks",
|
||||
"albums",
|
||||
"artists",
|
||||
"track_favorites",
|
||||
"listenings",
|
||||
"music_duration",
|
||||
]
|
||||
mocks = [
|
||||
mocker.patch.object(stats, 'get_{}'.format(k), return_value=i)
|
||||
mocker.patch.object(stats, "get_{}".format(k), return_value=i)
|
||||
for i, k in enumerate(keys)
|
||||
]
|
||||
|
||||
expected = {
|
||||
k: i for i, k in enumerate(keys)
|
||||
}
|
||||
expected = {k: i for i, k in enumerate(keys)}
|
||||
|
||||
assert stats.get() == expected
|
||||
|
|
|
|||
|
|
@ -5,58 +5,53 @@ from django.urls import reverse
|
|||
from funkwhale_api.instance import views
|
||||
|
||||
|
||||
@pytest.mark.parametrize('view,permissions', [
|
||||
(views.AdminSettings, ['settings']),
|
||||
])
|
||||
@pytest.mark.parametrize("view,permissions", [(views.AdminSettings, ["settings"])])
|
||||
def test_permissions(assert_user_permission, view, permissions):
|
||||
assert_user_permission(view, permissions)
|
||||
|
||||
|
||||
def test_nodeinfo_endpoint(db, api_client, mocker):
|
||||
payload = {
|
||||
'test': 'test'
|
||||
}
|
||||
payload = {"test": "test"}
|
||||
mocked_nodeinfo = mocker.patch(
|
||||
'funkwhale_api.instance.nodeinfo.get', return_value=payload)
|
||||
url = reverse('api:v1:instance:nodeinfo-2.0')
|
||||
"funkwhale_api.instance.nodeinfo.get", return_value=payload
|
||||
)
|
||||
url = reverse("api:v1:instance:nodeinfo-2.0")
|
||||
response = api_client.get(url)
|
||||
ct = 'application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8' # noqa
|
||||
ct = "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8" # noqa
|
||||
assert response.status_code == 200
|
||||
assert response['Content-Type'] == ct
|
||||
assert response["Content-Type"] == ct
|
||||
assert response.data == payload
|
||||
|
||||
|
||||
def test_nodeinfo_endpoint_disabled(db, api_client, preferences):
|
||||
preferences['instance__nodeinfo_enabled'] = False
|
||||
url = reverse('api:v1:instance:nodeinfo-2.0')
|
||||
preferences["instance__nodeinfo_enabled"] = False
|
||||
url = reverse("api:v1:instance:nodeinfo-2.0")
|
||||
response = api_client.get(url)
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_settings_only_list_public_settings(db, api_client, preferences):
|
||||
url = reverse('api:v1:instance:settings')
|
||||
url = reverse("api:v1:instance:settings")
|
||||
response = api_client.get(url)
|
||||
|
||||
for conf in response.data:
|
||||
p = preferences.model.objects.get(
|
||||
section=conf['section'], name=conf['name'])
|
||||
p = preferences.model.objects.get(section=conf["section"], name=conf["name"])
|
||||
assert p.preference.show_in_api is True
|
||||
|
||||
|
||||
def test_admin_settings_restrict_access(db, logged_in_api_client, preferences):
|
||||
url = reverse('api:v1:instance:admin-settings-list')
|
||||
url = reverse("api:v1:instance:admin-settings-list")
|
||||
response = logged_in_api_client.get(url)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_admin_settings_correct_permission(
|
||||
db, logged_in_api_client, preferences):
|
||||
def test_admin_settings_correct_permission(db, logged_in_api_client, preferences):
|
||||
user = logged_in_api_client.user
|
||||
user.permission_settings = True
|
||||
user.save()
|
||||
url = reverse('api:v1:instance:admin-settings-list')
|
||||
url = reverse("api:v1:instance:admin-settings-list")
|
||||
response = logged_in_api_client.get(url)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue