mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-05 03:29:23 +02:00
Audio federation
This commit is contained in:
parent
6992c567fb
commit
e49a460203
85 changed files with 2598 additions and 1204 deletions
|
@ -11,27 +11,27 @@ from funkwhale_api.federation import tasks
|
|||
def test_clean_federation_music_cache_if_no_listen(preferences, factories):
|
||||
preferences["federation__music_cache_duration"] = 60
|
||||
remote_library = factories["music.Library"]()
|
||||
tf1 = factories["music.TrackFile"](
|
||||
upload1 = factories["music.Upload"](
|
||||
library=remote_library, accessed_date=timezone.now()
|
||||
)
|
||||
tf2 = factories["music.TrackFile"](
|
||||
upload2 = factories["music.Upload"](
|
||||
library=remote_library,
|
||||
accessed_date=timezone.now() - datetime.timedelta(minutes=61),
|
||||
)
|
||||
tf3 = factories["music.TrackFile"](library=remote_library, accessed_date=None)
|
||||
path1 = tf1.audio_file.path
|
||||
path2 = tf2.audio_file.path
|
||||
path3 = tf3.audio_file.path
|
||||
upload3 = factories["music.Upload"](library=remote_library, accessed_date=None)
|
||||
path1 = upload1.audio_file.path
|
||||
path2 = upload2.audio_file.path
|
||||
path3 = upload3.audio_file.path
|
||||
|
||||
tasks.clean_music_cache()
|
||||
|
||||
tf1.refresh_from_db()
|
||||
tf2.refresh_from_db()
|
||||
tf3.refresh_from_db()
|
||||
upload1.refresh_from_db()
|
||||
upload2.refresh_from_db()
|
||||
upload3.refresh_from_db()
|
||||
|
||||
assert bool(tf1.audio_file) is True
|
||||
assert bool(tf2.audio_file) is False
|
||||
assert bool(tf3.audio_file) is False
|
||||
assert bool(upload1.audio_file) is True
|
||||
assert bool(upload2.audio_file) is False
|
||||
assert bool(upload3.audio_file) is False
|
||||
assert os.path.exists(path1) is True
|
||||
assert os.path.exists(path2) is False
|
||||
assert os.path.exists(path3) is False
|
||||
|
@ -46,16 +46,16 @@ def test_clean_federation_music_cache_orphaned(settings, preferences, factories)
|
|||
os.makedirs(os.path.dirname(remove_path), exist_ok=True)
|
||||
pathlib.Path(keep_path).touch()
|
||||
pathlib.Path(remove_path).touch()
|
||||
tf = factories["music.TrackFile"](
|
||||
upload = factories["music.Upload"](
|
||||
accessed_date=timezone.now(), audio_file__path=keep_path
|
||||
)
|
||||
|
||||
tasks.clean_music_cache()
|
||||
|
||||
tf.refresh_from_db()
|
||||
upload.refresh_from_db()
|
||||
|
||||
assert bool(tf.audio_file) is True
|
||||
assert os.path.exists(tf.audio_file.path) is True
|
||||
assert bool(upload.audio_file) is True
|
||||
assert os.path.exists(upload.audio_file.path) is True
|
||||
assert os.path.exists(remove_path) is False
|
||||
|
||||
|
||||
|
@ -73,168 +73,47 @@ def test_handle_in(factories, mocker, now, queryset_equal_list):
|
|||
a.payload, context={"actor": a.actor, "activity": a, "inbox_items": [ii1, ii2]}
|
||||
)
|
||||
|
||||
ii1.refresh_from_db()
|
||||
ii2.refresh_from_db()
|
||||
|
||||
assert ii1.is_delivered is True
|
||||
assert ii2.is_delivered is True
|
||||
assert ii1.last_delivery_date == now
|
||||
assert ii2.last_delivery_date == now
|
||||
|
||||
|
||||
def test_handle_in_error(factories, mocker, now):
|
||||
mocker.patch(
|
||||
"funkwhale_api.federation.routes.inbox.dispatch", side_effect=Exception()
|
||||
)
|
||||
r1 = factories["users.User"](with_actor=True).actor
|
||||
r2 = factories["users.User"](with_actor=True).actor
|
||||
|
||||
a = factories["federation.Activity"](payload={"hello": "world"})
|
||||
factories["federation.InboxItem"](activity=a, actor=r1)
|
||||
factories["federation.InboxItem"](activity=a, actor=r2)
|
||||
|
||||
with pytest.raises(Exception):
|
||||
tasks.dispatch_inbox(activity_id=a.pk)
|
||||
|
||||
assert a.inbox_items.filter(is_delivered=False).count() == 2
|
||||
|
||||
|
||||
def test_dispatch_outbox_to_inbox(factories, mocker):
|
||||
def test_dispatch_outbox(factories, mocker):
|
||||
mocked_inbox = mocker.patch("funkwhale_api.federation.tasks.dispatch_inbox.delay")
|
||||
mocked_deliver_to_remote_inbox = mocker.patch(
|
||||
"funkwhale_api.federation.tasks.deliver_to_remote_inbox.delay"
|
||||
mocked_deliver_to_remote = mocker.patch(
|
||||
"funkwhale_api.federation.tasks.deliver_to_remote.delay"
|
||||
)
|
||||
activity = factories["federation.Activity"](actor__local=True)
|
||||
factories["federation.InboxItem"](activity=activity, actor__local=True)
|
||||
remote_ii = factories["federation.InboxItem"](
|
||||
activity=activity,
|
||||
actor__shared_inbox_url=None,
|
||||
actor__inbox_url="https://test.inbox",
|
||||
)
|
||||
factories["federation.InboxItem"](activity=activity)
|
||||
delivery = factories["federation.Delivery"](activity=activity)
|
||||
tasks.dispatch_outbox(activity_id=activity.pk)
|
||||
mocked_inbox.assert_called_once_with(activity_id=activity.pk)
|
||||
mocked_deliver_to_remote_inbox.assert_called_once_with(
|
||||
activity_id=activity.pk, inbox_url=remote_ii.actor.inbox_url
|
||||
)
|
||||
mocked_deliver_to_remote.assert_called_once_with(delivery_id=delivery.pk)
|
||||
|
||||
|
||||
def test_dispatch_outbox_to_shared_inbox_url(factories, mocker):
|
||||
mocked_deliver_to_remote_inbox = mocker.patch(
|
||||
"funkwhale_api.federation.tasks.deliver_to_remote_inbox.delay"
|
||||
)
|
||||
activity = factories["federation.Activity"](actor__local=True)
|
||||
# shared inbox
|
||||
remote_ii_shared1 = factories["federation.InboxItem"](
|
||||
activity=activity, actor__shared_inbox_url="https://shared.inbox"
|
||||
)
|
||||
# another on the same shared inbox
|
||||
factories["federation.InboxItem"](
|
||||
activity=activity, actor__shared_inbox_url="https://shared.inbox"
|
||||
)
|
||||
# one on a dedicated inbox
|
||||
remote_ii_single = factories["federation.InboxItem"](
|
||||
activity=activity,
|
||||
actor__shared_inbox_url=None,
|
||||
actor__inbox_url="https://single.inbox",
|
||||
)
|
||||
tasks.dispatch_outbox(activity_id=activity.pk)
|
||||
def test_deliver_to_remote_success_mark_as_delivered(factories, r_mock, now):
|
||||
delivery = factories["federation.Delivery"]()
|
||||
r_mock.post(delivery.inbox_url)
|
||||
tasks.deliver_to_remote(delivery_id=delivery.pk)
|
||||
|
||||
assert mocked_deliver_to_remote_inbox.call_count == 2
|
||||
mocked_deliver_to_remote_inbox.assert_any_call(
|
||||
activity_id=activity.pk,
|
||||
shared_inbox_url=remote_ii_shared1.actor.shared_inbox_url,
|
||||
)
|
||||
mocked_deliver_to_remote_inbox.assert_any_call(
|
||||
activity_id=activity.pk, inbox_url=remote_ii_single.actor.inbox_url
|
||||
)
|
||||
|
||||
|
||||
def test_deliver_to_remote_inbox_inbox_url(factories, r_mock):
|
||||
activity = factories["federation.Activity"]()
|
||||
url = "https://test.shared/"
|
||||
r_mock.post(url)
|
||||
|
||||
tasks.deliver_to_remote_inbox(activity_id=activity.pk, inbox_url=url)
|
||||
delivery.refresh_from_db()
|
||||
|
||||
request = r_mock.request_history[0]
|
||||
|
||||
assert delivery.is_delivered is True
|
||||
assert delivery.attempts == 1
|
||||
assert delivery.last_attempt_date == now
|
||||
assert r_mock.called is True
|
||||
assert r_mock.call_count == 1
|
||||
assert request.url == url
|
||||
assert request.url == delivery.inbox_url
|
||||
assert request.headers["content-type"] == "application/activity+json"
|
||||
assert request.json() == activity.payload
|
||||
assert request.json() == delivery.activity.payload
|
||||
|
||||
|
||||
def test_deliver_to_remote_inbox_shared_inbox_url(factories, r_mock):
|
||||
activity = factories["federation.Activity"]()
|
||||
url = "https://test.shared/"
|
||||
r_mock.post(url)
|
||||
def test_deliver_to_remote_error(factories, r_mock, now):
|
||||
delivery = factories["federation.Delivery"]()
|
||||
r_mock.post(delivery.inbox_url, status_code=404)
|
||||
|
||||
tasks.deliver_to_remote_inbox(activity_id=activity.pk, shared_inbox_url=url)
|
||||
|
||||
request = r_mock.request_history[0]
|
||||
|
||||
assert r_mock.called is True
|
||||
assert r_mock.call_count == 1
|
||||
assert request.url == url
|
||||
assert request.headers["content-type"] == "application/activity+json"
|
||||
assert request.json() == activity.payload
|
||||
|
||||
|
||||
def test_deliver_to_remote_inbox_success_shared_inbox_marks_inbox_items_as_delivered(
|
||||
factories, r_mock, now
|
||||
):
|
||||
activity = factories["federation.Activity"]()
|
||||
url = "https://test.shared/"
|
||||
r_mock.post(url)
|
||||
ii = factories["federation.InboxItem"](
|
||||
activity=activity, actor__shared_inbox_url=url
|
||||
)
|
||||
other_ii = factories["federation.InboxItem"](
|
||||
activity=activity, actor__shared_inbox_url="https://other.url"
|
||||
)
|
||||
tasks.deliver_to_remote_inbox(activity_id=activity.pk, shared_inbox_url=url)
|
||||
|
||||
ii.refresh_from_db()
|
||||
other_ii.refresh_from_db()
|
||||
|
||||
assert ii.is_delivered is True
|
||||
assert ii.last_delivery_date == now
|
||||
assert other_ii.is_delivered is False
|
||||
assert other_ii.last_delivery_date is None
|
||||
|
||||
|
||||
def test_deliver_to_remote_inbox_success_single_inbox_marks_inbox_items_as_delivered(
|
||||
factories, r_mock, now
|
||||
):
|
||||
activity = factories["federation.Activity"]()
|
||||
url = "https://test.single/"
|
||||
r_mock.post(url)
|
||||
ii = factories["federation.InboxItem"](activity=activity, actor__inbox_url=url)
|
||||
other_ii = factories["federation.InboxItem"](
|
||||
activity=activity, actor__inbox_url="https://other.url"
|
||||
)
|
||||
tasks.deliver_to_remote_inbox(activity_id=activity.pk, inbox_url=url)
|
||||
|
||||
ii.refresh_from_db()
|
||||
other_ii.refresh_from_db()
|
||||
|
||||
assert ii.is_delivered is True
|
||||
assert ii.last_delivery_date == now
|
||||
assert other_ii.is_delivered is False
|
||||
assert other_ii.last_delivery_date is None
|
||||
|
||||
|
||||
def test_deliver_to_remote_inbox_error(factories, r_mock, now):
|
||||
activity = factories["federation.Activity"]()
|
||||
url = "https://test.single/"
|
||||
r_mock.post(url, status_code=404)
|
||||
ii = factories["federation.InboxItem"](activity=activity, actor__inbox_url=url)
|
||||
with pytest.raises(tasks.RequestException):
|
||||
tasks.deliver_to_remote_inbox(activity_id=activity.pk, inbox_url=url)
|
||||
tasks.deliver_to_remote(delivery_id=delivery.pk)
|
||||
|
||||
ii.refresh_from_db()
|
||||
delivery.refresh_from_db()
|
||||
|
||||
assert ii.is_delivered is False
|
||||
assert ii.last_delivery_date == now
|
||||
assert ii.delivery_attempts == 1
|
||||
assert delivery.is_delivered is False
|
||||
assert delivery.attempts == 1
|
||||
assert delivery.last_attempt_date == now
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue