generate-keys-at-registration-time (#181)
[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 full_tag_remote_user =
84 insert(:user, %{
85 nickname: "full_tag_remote_user",
86 ap_id: "http://misskey.local.live/users/full_tag_remote_user"
87 })
88
89 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
90
91 note =
92 "test/fixtures/misskey/mfm_x_format.json"
93 |> File.read!()
94 |> Jason.decode!()
95
96 %{
97 valid?: true,
98 changes: %{
99 content: content,
100 source: %{
101 "mediaType" => "text/x.misskeymarkdown"
102 }
103 }
104 } = ArticleNotePageValidator.cast_and_validate(note)
105
106 assert content =~
107 "<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>"
108
109 assert content =~
110 "<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>"
111
112 assert content =~
113 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{full_tag_remote_user.id}\" href=\"#{full_tag_remote_user.ap_id}\" rel=\"ugc\">@<span>full_tag_remote_user</span></a></span>"
114
115 assert content =~ "@oops_not_a_mention"
116
117 assert content =~
118 "<span class=\"mfm _mfm_jelly_\" style=\"display: inline-block; animation: 1s linear 0s infinite normal both running mfm-rubberBand;\">mfm goes here</span> </p>aaa"
119 end
120
121 test "a misskey MFM status with a _misskey_content field should work and be linked", _ do
122 local_user =
123 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
124
125 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
126
127 note =
128 "test/fixtures/misskey/mfm_underscore_format.json"
129 |> File.read!()
130 |> Jason.decode!()
131
132 changes = ArticleNotePageValidator.cast_and_validate(note)
133
134 %{
135 valid?: true,
136 changes: %{
137 content: content,
138 source: %{
139 "mediaType" => "text/x.misskeymarkdown",
140 "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa"
141 }
142 }
143 } = changes
144
145 assert content =~
146 "<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>"
147 end
148 end
149 end