From 0085c3a933791233e3c50e36fff42ea5dbd37c5c Mon Sep 17 00:00:00 2001 From: Loan Robert Date: Wed, 31 Jul 2024 21:06:17 +0200 Subject: [PATCH] Fix activitypub tests --- pod/activitypub/network.py | 9 ++-- .../video_creation_account_announce.json | 5 +-- .../video_creation_channel_announce.json | 5 +-- .../tests/fixtures/video_delete.json | 4 +- .../tests/fixtures/video_update.json | 28 ++++++------ .../tests/fixtures/video_view.json | 5 +-- pod/activitypub/tests/test_video_update.py | 43 ++++++++++++++++++- 7 files changed, 69 insertions(+), 30 deletions(-) diff --git a/pod/activitypub/network.py b/pod/activitypub/network.py index dfacad86cb..b8fd11bb20 100644 --- a/pod/activitypub/network.py +++ b/pod/activitypub/network.py @@ -145,9 +145,12 @@ def external_video_update(ap_video): def external_video_deletion(ap_video_id): logger.warning("ActivityPub task call ExternalVideo %s delete", ap_video_id) - external_video_to_delete = ExternalVideo.objects.get(ap_id=ap_video_id) - delete_es(media=external_video_to_delete) - external_video_to_delete.delete() + try: + external_video_to_delete = ExternalVideo.objects.get(ap_id=ap_video_id) + delete_es(media=external_video_to_delete) + external_video_to_delete.delete() + except ExternalVideo.DoesNotExist: + logger.warning("Received an ActivityPub delete event on a nonexistent ExternalVideo %s", ap_video_id) def send_video_announce_object(video: Video, follower: Follower): diff --git a/pod/activitypub/tests/fixtures/video_creation_account_announce.json b/pod/activitypub/tests/fixtures/video_creation_account_announce.json index 10281ddecf..45662e6259 100644 --- a/pod/activitypub/tests/fixtures/video_creation_account_announce.json +++ b/pod/activitypub/tests/fixtures/video_creation_account_announce.json @@ -12,9 +12,9 @@ ], "cc": [], "type": "Announce", - "id": "http://peertube.test/videos/watch/a425fb67-0d67-405f-a305-9cfc62ec00fd/announces/1", + "id": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/announces/1", "actor": "http://peertube.test/accounts/peertube", - "object": "http://peertube.test/videos/watch/a425fb67-0d67-405f-a305-9cfc62ec00fd", + "object": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d", "signature": { "type": "RsaSignature2017", "creator": "http://peertube.test/accounts/peertube", @@ -22,4 +22,3 @@ "signatureValue": "I0hzAFwpnlBthbAVas/eN+wAFC2G7mUBPi4ZefgvYKAxm990D0AopzQDUoW+P7Cc8CW7iUv2pX6GwM/m6Ez8LvKaDERmjNjPmAPIAC2Hm/V4/FTuCZ4wiQoIVvRNEsV/4O40Rr+ZyU+A9pJGhFh3RwJ3ezMsCdI36LVx2UHvgBM=" } } - diff --git a/pod/activitypub/tests/fixtures/video_creation_channel_announce.json b/pod/activitypub/tests/fixtures/video_creation_channel_announce.json index 36b0752f6a..b1243dc08d 100644 --- a/pod/activitypub/tests/fixtures/video_creation_channel_announce.json +++ b/pod/activitypub/tests/fixtures/video_creation_channel_announce.json @@ -12,9 +12,9 @@ ], "cc": [], "type": "Announce", - "id": "http://peertube.test/videos/watch/a425fb67-0d67-405f-a305-9cfc62ec00fd/announces/3", + "id": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/announces/3", "actor": "http://peertube.test/video-channels/root_channel", - "object": "http://peertube.test/videos/watch/a425fb67-0d67-405f-a305-9cfc62ec00fd", + "object": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d", "signature": { "type": "RsaSignature2017", "creator": "http://peertube.test/video-channels/root_channel", @@ -22,4 +22,3 @@ "signatureValue": "H6dBSuPhq0jqJVqHYq8+9SgyQNKwdH3wfTqvTyXGPAfM7xcLWU1o7nSViVkRoIMhvTMzFQYAGrBr5HCdTpfv+q6iyuxf3udTdIzaO7ilqmmihI9gu104nRg61tNPGkbWwhQ2IWLFlVGVlKsTzNdU2VteOQT98LpA1GpFa9ExDdk=" } } - diff --git a/pod/activitypub/tests/fixtures/video_delete.json b/pod/activitypub/tests/fixtures/video_delete.json index 7724eff481..dd7420cd37 100644 --- a/pod/activitypub/tests/fixtures/video_delete.json +++ b/pod/activitypub/tests/fixtures/video_delete.json @@ -14,9 +14,9 @@ ], "cc": [], "type": "Delete", - "id": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8/delete", + "id": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/delete", "actor": "http://peertube.test/accounts/root", - "object": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8", + "object": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d", "signature": { "type": "RsaSignature2017", "creator": "http://peertube.test/accounts/root", diff --git a/pod/activitypub/tests/fixtures/video_update.json b/pod/activitypub/tests/fixtures/video_update.json index 7e533a2995..5b120c2b28 100644 --- a/pod/activitypub/tests/fixtures/video_update.json +++ b/pod/activitypub/tests/fixtures/video_update.json @@ -109,7 +109,7 @@ "http://peertube.test/accounts/root/followers" ], "type": "Update", - "id": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8/updates/2024-05-15T12:40:45.399Z", + "id": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/updates/2024-05-15T12:40:45.399Z", "actor": "http://peertube.test/accounts/root", "object": { "to": [ @@ -119,10 +119,10 @@ "http://peertube.test/accounts/root/followers" ], "type": "Video", - "id": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8", + "id": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d", "name": "terre", "duration": "PT31S", - "uuid": "74b35c2c-5b41-47ee-b331-86ec80f317d8", + "uuid": "717c9d87-c912-4943-a392-49fadf2f235d", "views": 0, "sensitive": false, "waitTranscoding": true, @@ -177,12 +177,12 @@ { "type": "Link", "mediaType": "text/html", - "href": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8" + "href": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d" }, { "type": "Link", "mediaType": "application/x-mpegURL", - "href": "http://peertube.test/static/streaming-playlists/hls/74b35c2c-5b41-47ee-b331-86ec80f317d8/af301f93-0d7d-4940-8e5e-aba13049ac26-master.m3u8", + "href": "http://peertube.test/static/streaming-playlists/hls/717c9d87-c912-4943-a392-49fadf2f235d/af301f93-0d7d-4940-8e5e-aba13049ac26-master.m3u8", "tag": [ { "type": "Infohash", @@ -192,12 +192,12 @@ "type": "Link", "name": "sha256", "mediaType": "application/json", - "href": "http://peertube.test/static/streaming-playlists/hls/74b35c2c-5b41-47ee-b331-86ec80f317d8/dc1221ee-1090-40b8-bdfa-5a5f4cd67c98-segments-sha256.json" + "href": "http://peertube.test/static/streaming-playlists/hls/717c9d87-c912-4943-a392-49fadf2f235d/dc1221ee-1090-40b8-bdfa-5a5f4cd67c98-segments-sha256.json" }, { "type": "Link", "mediaType": "video/mp4", - "href": "http://peertube.test/static/streaming-playlists/hls/74b35c2c-5b41-47ee-b331-86ec80f317d8/519179ac-f8f9-4c50-a90c-2d6bf554e508-270-fragmented.mp4", + "href": "http://peertube.test/static/streaming-playlists/hls/717c9d87-c912-4943-a392-49fadf2f235d/519179ac-f8f9-4c50-a90c-2d6bf554e508-270-fragmented.mp4", "height": 270, "width": 480, "size": 653210, @@ -210,7 +210,7 @@ "video/mp4" ], "mediaType": "application/json", - "href": "http://peertube.test/api/v1/videos/74b35c2c-5b41-47ee-b331-86ec80f317d8/metadata/3", + "href": "http://peertube.test/api/v1/videos/717c9d87-c912-4943-a392-49fadf2f235d/metadata/3", "height": 270, "width": 480, "fps": 30 @@ -226,7 +226,7 @@ { "type": "Link", "mediaType": "application/x-bittorrent;x-scheme-handler/magnet", - "href": "magnet:?xs=http%3A%2F%2Fpeertube.localhost%3A9000%2Flazy-static%2Ftorrents%2Fb0e90759-ec93-47b1-a54b-124eecc7e920-270-hls.torrent&xt=urn:btih:5072ce02cf06578a6ea1c3aa95f80f25afc33949&dn=terre&tr=http%3A%2F%2Fpeertube.localhost%3A9000%2Ftracker%2Fannounce&tr=ws%3A%2F%2Fpeertube.localhost%3A9000%2Ftracker%2Fsocket&ws=http%3A%2F%2Fpeertube.localhost%3A9000%2Fstatic%2Fstreaming-playlists%2Fhls%2F74b35c2c-5b41-47ee-b331-86ec80f317d8%2F519179ac-f8f9-4c50-a90c-2d6bf554e508-270-fragmented.mp4", + "href": "magnet:?xs=http%3A%2F%2Fpeertube.localhost%3A9000%2Flazy-static%2Ftorrents%2Fb0e90759-ec93-47b1-a54b-124eecc7e920-270-hls.torrent&xt=urn:btih:5072ce02cf06578a6ea1c3aa95f80f25afc33949&dn=terre&tr=http%3A%2F%2Fpeertube.localhost%3A9000%2Ftracker%2Fannounce&tr=ws%3A%2F%2Fpeertube.localhost%3A9000%2Ftracker%2Fsocket&ws=http%3A%2F%2Fpeertube.localhost%3A9000%2Fstatic%2Fstreaming-playlists%2Fhls%2F717c9d87-c912-4943-a392-49fadf2f235d%2F519179ac-f8f9-4c50-a90c-2d6bf554e508-270-fragmented.mp4", "height": 270, "width": 480, "fps": 30 @@ -252,11 +252,11 @@ "href": "ws://peertube.test/tracker/socket" } ], - "likes": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8/likes", - "dislikes": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8/dislikes", - "shares": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8/announces", - "comments": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8/comments", - "hasParts": "http://peertube.test/videos/watch/74b35c2c-5b41-47ee-b331-86ec80f317d8/chapters", + "likes": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/likes", + "dislikes": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/dislikes", + "shares": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/announces", + "comments": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/comments", + "hasParts": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d/chapters", "attributedTo": [ { "type": "Person", diff --git a/pod/activitypub/tests/fixtures/video_view.json b/pod/activitypub/tests/fixtures/video_view.json index 31ac51f085..86f1701412 100644 --- a/pod/activitypub/tests/fixtures/video_view.json +++ b/pod/activitypub/tests/fixtures/video_view.json @@ -19,10 +19,10 @@ "http://peertube.test/accounts/root/followers" ], "cc": [], - "id": "http://peertube.test/accounts/peertube/views/videos/1/CBmE6B8qR3wmPvfXxlnzYLZj2aGxHbuP-a425fb67-0d67-405f-a305-9cfc62ec00fd", + "id": "http://peertube.test/accounts/peertube/views/videos/1/CBmE6B8qR3wmPvfXxlnzYLZj2aGxHbuP-717c9d87-c912-4943-a392-49fadf2f235d", "type": "View", "actor": "http://peertube.test/accounts/peertube", - "object": "http://peertube.test/videos/watch/a425fb67-0d67-405f-a305-9cfc62ec00fd", + "object": "http://peertube.test/videos/watch/717c9d87-c912-4943-a392-49fadf2f235d", "expires": "2024-05-15T11:17:56.017Z", "signature": { "type": "RsaSignature2017", @@ -31,4 +31,3 @@ "signatureValue": "xCmw4sWlGwss1ksoYSWAZ3g2+ZCy9ILiicz2YYgfeyCssstKDV405l6NfP+g7T0i4icQnjhyaOAYjveQlLSEvth1lhrwStBpY6WboAKbpct6aFV37a5FTAHHZD/zlGHRGTf2PJrPem4MMB0zI9Hr4K0gqylHGMDazyPElJl4S4E=" } } - diff --git a/pod/activitypub/tests/test_video_update.py b/pod/activitypub/tests/test_video_update.py index 29ee9b32cb..a3e9d9f40d 100644 --- a/pod/activitypub/tests/test_video_update.py +++ b/pod/activitypub/tests/test_video_update.py @@ -23,6 +23,8 @@ def test_video_creation(self): ) as fd: account_announce_payload = json.load(fd) + assert not len(ExternalVideo.objects.all()) + with httmock.HTTMock(self.mock_application_actor, self.mock_get_video): response = self.client.post( "/ap/inbox", @@ -32,6 +34,7 @@ def test_video_creation(self): ) self.assertEqual(response.status_code, 204) + assert len(ExternalVideo.objects.all()) == 1 # TODO: assert ExternalVideo is created with open( @@ -66,6 +69,24 @@ def test_video_view(self): def test_video_update(self): """Test the video update activity on the inbox""" + with open( + "pod/activitypub/tests/fixtures/video_creation_account_announce.json" + ) as fd: + account_announce_payload = json.load(fd) + + assert not len(ExternalVideo.objects.all()) + + with httmock.HTTMock(self.mock_application_actor, self.mock_get_video): + response = self.client.post( + "/ap/inbox", + json.dumps(account_announce_payload), + content_type="application/json", + **self.headers, + ) + self.assertEqual(response.status_code, 204) + + assert ExternalVideo.objects.first().title == "Titre de la vidéo" + with open("pod/activitypub/tests/fixtures/video_update.json") as fd: payload = json.load(fd) @@ -77,11 +98,29 @@ def test_video_update(self): ) self.assertEqual(response.status_code, 204) - # TODO: assert ExternalVideo is updated + assert ExternalVideo.objects.first().title == "terre" def test_video_delete(self): """Test the video update activity on the inbox""" + with open( + "pod/activitypub/tests/fixtures/video_creation_account_announce.json" + ) as fd: + account_announce_payload = json.load(fd) + + assert not len(ExternalVideo.objects.all()) + + with httmock.HTTMock(self.mock_application_actor, self.mock_get_video): + response = self.client.post( + "/ap/inbox", + json.dumps(account_announce_payload), + content_type="application/json", + **self.headers, + ) + self.assertEqual(response.status_code, 204) + + assert len(ExternalVideo.objects.all()) == 1 + with open("pod/activitypub/tests/fixtures/video_delete.json") as fd: payload = json.load(fd) @@ -94,4 +133,4 @@ def test_video_delete(self): ) self.assertEqual(response.status_code, 204) - # TODO: assert ExternalVideo is deleted + assert not len(ExternalVideo.objects.all())