refactor: upgrade code to >=python3.7 (pre-commit)

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2189>
This commit is contained in:
jo 2022-11-23 22:36:56 +01:00 committed by Marge
commit 8d9946d35a
143 changed files with 454 additions and 582 deletions

View file

@ -352,7 +352,7 @@ def test_inbox_routing_send_to_channel(factories, mocker):
ii.refresh_from_db()
group_send.assert_called_once_with(
"user.{}.inbox".format(ii.actor.user.pk),
f"user.{ii.actor.user.pk}.inbox",
{
"type": "event.send",
"text": "",
@ -455,10 +455,10 @@ def test_outbox_router_dispatch_allow_list(mocker, factories, preferences, now):
router.connect({"type": "Noop"}, handler)
router.dispatch({"type": "Noop"}, {"summary": "hello"})
prepare_deliveries_and_inbox_items.assert_any_call(
[r1], "to", allowed_domains=set([r1.domain_id])
[r1], "to", allowed_domains={r1.domain_id}
)
prepare_deliveries_and_inbox_items.assert_any_call(
[r2], "cc", allowed_domains=set([r1.domain_id])
[r2], "cc", allowed_domains={r1.domain_id}
)
@ -579,7 +579,7 @@ def test_prepare_deliveries_and_inbox_items_allow_list(factories, preferences):
recipients = [remote_actor1, remote_actor2]
inbox_items, deliveries, urls = activity.prepare_deliveries_and_inbox_items(
recipients, "to", allowed_domains=set([remote_actor1.domain_id])
recipients, "to", allowed_domains={remote_actor1.domain_id}
)
expected_inbox_items = []

View file

@ -93,7 +93,7 @@ def test_user_cannot_edit_someone_else_library_follow(
logged_in_api_client.user.create_actor()
follow = factories["federation.LibraryFollow"]()
url = reverse(
"api:v1:federation:library-follows-{}".format(action),
f"api:v1:federation:library-follows-{action}",
kwargs={"uuid": follow.uuid},
)
response = logged_in_api_client.post(url)
@ -111,7 +111,7 @@ def test_user_can_accept_or_reject_own_follows(
actor = logged_in_api_client.user.create_actor()
follow = factories["federation.LibraryFollow"](target__actor=actor)
url = reverse(
"api:v1:federation:library-follows-{}".format(action),
f"api:v1:federation:library-follows-{action}",
kwargs={"uuid": follow.uuid},
)
response = logged_in_api_client.post(url)

View file

@ -37,7 +37,7 @@ def test_authenticate(factories, mocker, api_request):
**{
"HTTP_DATE": prepared.headers["date"],
"HTTP_SIGNATURE": prepared.headers["signature"],
}
},
)
authenticator = authentication.SignatureAuthentication()
user, _ = authenticator.authenticate(django_request)
@ -52,7 +52,7 @@ def test_authenticate(factories, mocker, api_request):
def test_authenticate_skips_blocked_domain(factories, api_request):
policy = factories["moderation.InstancePolicy"](block_all=True, for_domain=True)
private, public = keys.get_key_pair()
actor_url = "https://{}/actor".format(policy.target_domain.name)
actor_url = f"https://{policy.target_domain.name}/actor"
signed_request = factories["federation.SignedRequest"](
auth__key=private, auth__key_id=actor_url + "#main-key", auth__headers=["date"]
@ -63,7 +63,7 @@ def test_authenticate_skips_blocked_domain(factories, api_request):
**{
"HTTP_DATE": prepared.headers["date"],
"HTTP_SIGNATURE": prepared.headers["signature"],
}
},
)
authenticator = authentication.SignatureAuthentication()
@ -85,7 +85,7 @@ def test_authenticate_skips_blocked_actor(factories, api_request):
**{
"HTTP_DATE": prepared.headers["date"],
"HTTP_SIGNATURE": prepared.headers["signature"],
}
},
)
authenticator = authentication.SignatureAuthentication()
@ -98,7 +98,7 @@ def test_authenticate_ignore_inactive_policy(factories, api_request, mocker):
block_all=True, for_domain=True, is_active=False
)
private, public = keys.get_key_pair()
actor_url = "https://{}/actor".format(policy.target_domain.name)
actor_url = f"https://{policy.target_domain.name}/actor"
signed_request = factories["federation.SignedRequest"](
auth__key=private, auth__key_id=actor_url + "#main-key", auth__headers=["date"]
@ -126,7 +126,7 @@ def test_authenticate_ignore_inactive_policy(factories, api_request, mocker):
**{
"HTTP_DATE": prepared.headers["date"],
"HTTP_SIGNATURE": prepared.headers["signature"],
}
},
)
authenticator = authentication.SignatureAuthentication()
authenticator.authenticate(django_request)
@ -169,7 +169,7 @@ def test_autenthicate_supports_blind_key_rotation(factories, mocker, api_request
**{
"HTTP_DATE": prepared.headers["date"],
"HTTP_SIGNATURE": prepared.headers["signature"],
}
},
)
authenticator = authentication.SignatureAuthentication()
user, _ = authenticator.authenticate(django_request)
@ -186,7 +186,7 @@ def test_authenticate_checks_signature_with_allow_list(
preferences["moderation__allow_list_enabled"] = True
domain = factories["federation.Domain"](allowed=False)
private, public = keys.get_key_pair()
actor_url = "https://{}/actor".format(domain.name)
actor_url = f"https://{domain.name}/actor"
signed_request = factories["federation.SignedRequest"](
auth__key=private, auth__key_id=actor_url + "#main-key", auth__headers=["date"]
@ -197,7 +197,7 @@ def test_authenticate_checks_signature_with_allow_list(
**{
"HTTP_DATE": prepared.headers["date"],
"HTTP_SIGNATURE": prepared.headers["signature"],
}
},
)
authenticator = authentication.SignatureAuthentication()

View file

@ -39,9 +39,7 @@ def test_fetches_route_create(factories, api_request, mocker):
def test_fetches_route_create_local(factories, api_request, mocker, settings):
user = factories["users.User"]()
user.create_actor()
track = factories["music.Track"](
fid="https://{}/test".format(settings.FEDERATION_HOSTNAME)
)
track = factories["music.Track"](fid=f"https://{settings.FEDERATION_HOSTNAME}/test")
view = V.as_view({"post": "fetches"})
request = api_request.post("/", format="json")

View file

@ -23,7 +23,7 @@ def test_cannot_duplicate_follow(factories):
def test_follow_federation_url(factories):
follow = factories["federation.Follow"](local=True)
expected = "{}#follows/{}".format(follow.actor.fid, follow.uuid)
expected = f"{follow.actor.fid}#follows/{follow.uuid}"
assert follow.get_federation_id() == expected
@ -179,7 +179,7 @@ def test_actor_can_manage_attributed_to(mocker, factories):
def test_actor_can_manage_domain_not_service_actor(mocker, factories):
actor = factories["federation.Actor"]()
obj = mocker.Mock(fid="https://{}/hello".format(actor.domain_id))
obj = mocker.Mock(fid=f"https://{actor.domain_id}/hello")
assert actor.can_manage(obj) is False
@ -188,7 +188,7 @@ def test_actor_can_manage_domain_service_actor(mocker, factories):
actor = factories["federation.Actor"]()
actor.domain.service_actor = actor
actor.domain.save()
obj = mocker.Mock(fid="https://{}/hello".format(actor.domain_id))
obj = mocker.Mock(fid=f"https://{actor.domain_id}/hello")
assert actor.can_manage(obj) is True

View file

@ -42,7 +42,7 @@ def test_inbox_routes(route, handler):
matching = [
handler for r, handler in routes.inbox.routes if activity.match_route(r, route)
]
assert len(matching) == 1, "Inbox route {} not found".format(route)
assert len(matching) == 1, f"Inbox route {route} not found"
assert matching[0] == handler
@ -88,7 +88,7 @@ def test_outbox_routes(route, handler):
matching = [
handler for r, handler in routes.outbox.routes if activity.match_route(r, route)
]
assert len(matching) == 1, "Outbox route {} not found".format(route)
assert len(matching) == 1, f"Outbox route {route} not found"
assert matching[0] == handler

View file

@ -1940,7 +1940,7 @@ def test_report_serializer_to_ap(factories):
"actor": actors.get_service_actor().fid,
"content": report.summary,
"object": [report.target.fid],
"tag": [{"type": "Hashtag", "name": "#{}".format(report.type)}],
"tag": [{"type": "Hashtag", "name": f"#{report.type}"}],
}
serializer = serializers.FlagSerializer(report)
assert serializer.data == expected

View file

@ -4,7 +4,7 @@ from funkwhale_api.common import utils
def test_channel_detail(spa_html, no_api_auth, client, factories, settings):
icon = factories["common.Attachment"]()
actor = factories["federation.Actor"](local=True, attachment_icon=icon)
url = "/@{}".format(actor.preferred_username)
url = f"/@{actor.preferred_username}"
response = client.get(url)

View file

@ -214,7 +214,7 @@ def test_update_domain_nodeinfo(factories, mocker, now, service_actor):
def test_update_domain_nodeinfo_error(factories, r_mock, now):
domain = factories["federation.Domain"](nodeinfo_fetch_date=None)
wellknown_url = "https://{}/.well-known/nodeinfo".format(domain.name)
wellknown_url = f"https://{domain.name}/.well-known/nodeinfo"
r_mock.get(wellknown_url, status_code=500)
@ -225,7 +225,7 @@ def test_update_domain_nodeinfo_error(factories, r_mock, now):
assert domain.nodeinfo_fetch_date == now
assert domain.nodeinfo == {
"status": "error",
"error": "500 Server Error: None for url: {}".format(wellknown_url),
"error": f"500 Server Error: None for url: {wellknown_url}",
}
@ -406,14 +406,12 @@ def test_fetch_success(factories, r_mock, mocker):
def test_fetch_webfinger(factories, r_mock, mocker):
actor = factories["federation.Actor"]()
fetch = factories["federation.Fetch"](
url="webfinger://{}".format(actor.full_username)
)
fetch = factories["federation.Fetch"](url=f"webfinger://{actor.full_username}")
payload = serializers.ActorSerializer(actor).data
init = mocker.spy(serializers.ActorSerializer, "__init__")
save = mocker.spy(serializers.ActorSerializer, "save")
webfinger_payload = {
"subject": "acct:{}".format(actor.full_username),
"subject": f"acct:{actor.full_username}",
"aliases": ["https://test.webfinger"],
"links": [
{"rel": "self", "type": "application/activity+json", "href": actor.fid}
@ -542,7 +540,7 @@ def test_fetch_honor_instance_policy_domain(factories):
domain = factories["moderation.InstancePolicy"](
block_all=True, for_domain=True
).target_domain
fid = "https://{}/test".format(domain.name)
fid = f"https://{domain.name}/test"
fetch = factories["federation.Fetch"](url=fid)
tasks.fetch(fetch_id=fetch.pk)
@ -588,7 +586,7 @@ def test_fetch_honor_instance_policy_different_url_and_id(r_mock, factories):
block_all=True, for_domain=True
).target_domain
fid = "https://ok/test"
r_mock.get(fid, json={"id": "http://{}/test".format(domain.name)})
r_mock.get(fid, json={"id": f"http://{domain.name}/test"})
fetch = factories["federation.Fetch"](url=fid)
tasks.fetch(fetch_id=fetch.pk)
fetch.refresh_from_db()

View file

@ -67,7 +67,7 @@ def test_retrieve_ap_object_honor_instance_policy_domain(factories):
domain = factories["moderation.InstancePolicy"](
block_all=True, for_domain=True
).target_domain
fid = "https://{}/test".format(domain.name)
fid = f"https://{domain.name}/test"
with pytest.raises(exceptions.BlockedActorOrDomain):
utils.retrieve_ap_object(fid, actor=None)
@ -106,7 +106,7 @@ def test_retrieve_ap_object_honor_instance_policy_different_url_and_id(
block_all=True, for_domain=True
).target_domain
fid = "https://ok/test"
r_mock.get(fid, json={"id": "http://{}/test".format(domain.name)})
r_mock.get(fid, json={"id": f"http://{domain.name}/test"})
with pytest.raises(exceptions.BlockedActorOrDomain):
utils.retrieve_ap_object(fid, actor=None)

View file

@ -164,7 +164,7 @@ def test_wellknown_webfinger_local(factories, api_client, settings, mocker):
url = reverse("federation:well-known-webfinger")
response = api_client.get(
url,
data={"resource": "acct:{}".format(user.actor.webfinger_subject)},
data={"resource": f"acct:{user.actor.webfinger_subject}"},
HTTP_ACCEPT="application/jrd+json",
)
serializer = serializers.ActorWebfingerSerializer(user.actor)
@ -327,10 +327,8 @@ def test_music_library_retrieve_page_follow(
def test_music_local_entity_detail(
factories, api_client, factory, serializer_class, namespace, settings
):
obj = factories[factory](fid="http://{}/1".format(settings.FEDERATION_HOSTNAME))
url = reverse(
"federation:music:{}-detail".format(namespace), kwargs={"uuid": obj.uuid}
)
obj = factories[factory](fid=f"http://{settings.FEDERATION_HOSTNAME}/1")
url = reverse(f"federation:music:{namespace}-detail", kwargs={"uuid": obj.uuid})
response = api_client.get(url)
assert response.status_code == 200
@ -345,9 +343,7 @@ def test_music_non_local_entity_detail(
factories, api_client, factory, namespace, settings
):
obj = factories[factory](fid="http://wrong-domain/1")
url = reverse(
"federation:music:{}-detail".format(namespace), kwargs={"uuid": obj.uuid}
)
url = reverse(f"federation:music:{namespace}-detail", kwargs={"uuid": obj.uuid})
response = api_client.get(url)
assert response.status_code == 404
@ -539,7 +535,7 @@ def test_artist_retrieve_redirects_to_html_if_header_set(
@pytest.mark.parametrize("index", ["channels", "libraries"])
def test_public_index_disabled(index, api_client, preferences):
preferences["federation__public_index"] = False
url = reverse("federation:index:index-{}".format(index))
url = reverse(f"federation:index:index-{index}")
response = api_client.get(url)
assert response.status_code == 405

View file

@ -60,7 +60,7 @@ def test_webfinger_get_resource(r_mock):
],
}
r_mock.get(
"https://test.webfinger/.well-known/webfinger?resource={}".format(resource),
f"https://test.webfinger/.well-known/webfinger?resource={resource}",
json=payload,
)