[#58] pre-link MFM content (#59)
[akkoma] / test / pleroma / web / activity_pub / object_validators / article_note_page_validator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest do
6 use Pleroma.DataCase, async: true
7
8 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator
9 alias Pleroma.Web.ActivityPub.Utils
10
11 import Pleroma.Factory
12
13 setup_all do
14 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
15
16 :ok
17 end
18
19 describe "Notes" do
20 setup do
21 user = insert(:user)
22
23 note = %{
24 "id" => Utils.generate_activity_id(),
25 "type" => "Note",
26 "actor" => user.ap_id,
27 "to" => [user.follower_address],
28 "cc" => [],
29 "content" => "Hellow this is content.",
30 "context" => "xxx",
31 "summary" => "a post"
32 }
33
34 %{user: user, note: note}
35 end
36
37 test "a basic note validates", %{note: note} do
38 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
39 end
40
41 test "a note with a remote replies collection should validate", _ do
42 insert(:user, %{ap_id: "https://bookwyrm.com/user/TestUser"})
43 collection = File.read!("test/fixtures/bookwyrm-replies-collection.json")
44
45 Tesla.Mock.mock(fn %{
46 method: :get,
47 url: "https://bookwyrm.com/user/TestUser/review/17/replies?page=1"
48 } ->
49 %Tesla.Env{
50 status: 200,
51 body: collection,
52 headers: HttpRequestMock.activitypub_object_headers()
53 }
54 end)
55
56 note = Jason.decode!(File.read!("test/fixtures/bookwyrm-article.json"))
57
58 %{valid?: true, changes: %{replies: ["https://bookwyrm.com/user/TestUser/status/18"]}} =
59 ArticleNotePageValidator.cast_and_validate(note)
60 end
61
62 test "a note with an attachment should work", _ do
63 insert(:user, %{ap_id: "https://owncast.localhost.localdomain/federation/user/streamer"})
64
65 note =
66 "test/fixtures/owncast-note-with-attachment.json"
67 |> File.read!()
68 |> Jason.decode!()
69
70 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
71 end
72
73 test "a misskey MFM status with a content field should work and be linked", _ do
74 local_user = insert(:user, %{nickname: "akkoma_user"})
75
76 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
77
78 note =
79 "test/fixtures/misskey/mfm_x_format.json"
80 |> File.read!()
81 |> Jason.decode!()
82
83 expected_content =
84 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{local_user.id}\" href=\"#{local_user.ap_id}\" rel=\"ugc\">@<span>akkoma_user</span></a></span> linkifylink <a class=\"hashtag\" data-tag=\"dancedance\" href=\"http://localhost:4001/tag/dancedance\" rel=\"tag ugc\">#dancedance</a> $[jelly mfm goes here] <br><br>## aaa"
85
86 %{
87 valid?: true,
88 changes: %{
89 content: ^expected_content,
90 source: %{
91 "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa",
92 "mediaType" => "text/x.misskeymarkdown"
93 }
94 }
95 } = ArticleNotePageValidator.cast_and_validate(note)
96 end
97
98 test "a misskey MFM status with a _misskey_content field should work and be linked", _ do
99 local_user = insert(:user, %{nickname: "akkoma_user"})
100
101 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
102
103 note =
104 "test/fixtures/misskey/mfm_underscore_format.json"
105 |> File.read!()
106 |> Jason.decode!()
107
108 expected_content =
109 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{local_user.id}\" href=\"#{local_user.ap_id}\" rel=\"ugc\">@<span>akkoma_user</span></a></span> linkifylink <a class=\"hashtag\" data-tag=\"dancedance\" href=\"http://localhost:4001/tag/dancedance\" rel=\"tag ugc\">#dancedance</a> $[jelly mfm goes here] <br><br>## aaa"
110
111 %{
112 valid?: true,
113 changes: %{
114 content: ^expected_content,
115 source: %{
116 "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa",
117 "mediaType" => "text/x.misskeymarkdown"
118 }
119 }
120 } = ArticleNotePageValidator.cast_and_validate(note)
121 end
122 end
123 end