4ea7a5bc029f375630f6a01479afbd91ff4d223c
[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 =
75 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
76
77 remote_user =
78 insert(:user, %{
79 nickname: "remote_user",
80 ap_id: "http://misskey.local.live/users/remote_user"
81 })
82
83 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
84
85 note =
86 "test/fixtures/misskey/mfm_x_format.json"
87 |> File.read!()
88 |> Jason.decode!()
89
90 %{
91 valid?: true,
92 changes: %{
93 content: content,
94 source: %{
95 "content" =>
96 "@akkoma_user @remote_user @oops_not_a_mention linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa",
97 "mediaType" => "text/x.misskeymarkdown"
98 }
99 }
100 } = ArticleNotePageValidator.cast_and_validate(note)
101
102 assert content =~
103 "<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>"
104
105 assert content =~
106 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{remote_user.id}\" href=\"#{remote_user.ap_id}\" rel=\"ugc\">@<span>remote_user</span></a></span>"
107
108 assert content =~ "@oops_not_a_mention"
109 assert content =~ "$[jelly mfm goes here] <br><br>## aaa"
110 end
111
112 test "a misskey MFM status with a _misskey_content field should work and be linked", _ do
113 local_user =
114 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
115
116 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
117
118 note =
119 "test/fixtures/misskey/mfm_underscore_format.json"
120 |> File.read!()
121 |> Jason.decode!()
122
123 expected_content =
124 "<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"
125
126 %{
127 valid?: true,
128 changes: %{
129 content: ^expected_content,
130 source: %{
131 "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa",
132 "mediaType" => "text/x.misskeymarkdown"
133 }
134 }
135 } = ArticleNotePageValidator.cast_and_validate(note)
136 end
137 end
138 end