1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest do
6 use Pleroma.DataCase, async: true
8 alias Pleroma.Web.ActivityPub.ObjectValidator
9 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator
10 alias Pleroma.Web.ActivityPub.Utils
12 import Pleroma.Factory
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 "id" => Utils.generate_activity_id(),
27 "actor" => user.ap_id,
28 "to" => [user.follower_address],
30 "content" => "Hellow this is content.",
31 "published" => "2021-01-01T00:00:00Z",
36 %{user: user, note: note}
39 test "a basic note validates", %{note: note} do
40 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
43 test "a note from factory validates" do
45 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note.data)
48 test "a note with a remote replies collection should validate", _ do
49 insert(:user, %{ap_id: "https://bookwyrm.com/user/TestUser"})
50 collection = File.read!("test/fixtures/bookwyrm-replies-collection.json")
54 url: "https://bookwyrm.com/user/TestUser/review/17/replies?page=1"
59 headers: HttpRequestMock.activitypub_object_headers()
63 note = Jason.decode!(File.read!("test/fixtures/bookwyrm-article.json"))
65 %{valid?: true, changes: %{replies: ["https://bookwyrm.com/user/TestUser/status/18"]}} =
66 ArticleNotePageValidator.cast_and_validate(note)
69 test "a note without a published field should not validate", _ do
70 insert(:user, %{ap_id: "http://mastodon.example.org/users/admin"})
71 note = Jason.decode!(File.read!("test/fixtures/mastodon/note-without-published.json"))
72 %{valid?: false} = ArticleNotePageValidator.cast_and_validate(note)
75 test "a note with an attachment should work", _ do
76 insert(:user, %{ap_id: "https://owncast.localhost.localdomain/federation/user/streamer"})
79 "test/fixtures/owncast-note-with-attachment.json"
83 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
86 test "a misskey MFM status with a content field should work and be linked", _ do
88 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
92 nickname: "remote_user",
93 ap_id: "http://misskey.local.live/users/remote_user"
96 full_tag_remote_user =
98 nickname: "full_tag_remote_user",
99 ap_id: "http://misskey.local.live/users/full_tag_remote_user"
102 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
105 "test/fixtures/misskey/mfm_x_format.json"
114 "mediaType" => "text/x.misskeymarkdown"
117 } = ArticleNotePageValidator.cast_and_validate(note)
120 "<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>"
123 "<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>"
126 "<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>"
128 assert content =~ "@oops_not_a_mention"
131 "<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"
134 test "a misskey MFM status with a _misskey_content field should work and be linked", _ do
136 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
138 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
141 "test/fixtures/misskey/mfm_underscore_format.json"
145 changes = ArticleNotePageValidator.cast_and_validate(note)
152 "mediaType" => "text/x.misskeymarkdown",
153 "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa"
159 "<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>"
163 test "a Note without replies/first/items validates" do
164 insert(:user, ap_id: "https://mastodon.social/users/emelie")
167 "test/fixtures/tesla_mock/status.emelie.json"
170 |> pop_in(["replies", "first", "items"])
173 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
176 describe "Note with history" do
179 {:ok, activity} = Pleroma.Web.CommonAPI.post(user, %{status: "mew mew :dinosaur:"})
180 {:ok, edit} = Pleroma.Web.CommonAPI.update(user, activity, %{status: "edited :blank:"})
182 {:ok, %{"object" => external_rep}} =
183 Pleroma.Web.ActivityPub.Transmogrifier.prepare_outgoing(edit.data)
185 %{external_rep: external_rep}
188 test "edited note", %{external_rep: external_rep} do
189 assert %{"formerRepresentations" => %{"orderedItems" => [%{"tag" => [_]}]}} = external_rep
191 {:ok, validate_res, []} = ObjectValidator.validate(external_rep, [])
193 assert %{"formerRepresentations" => %{"orderedItems" => [%{"emoji" => %{"dinosaur" => _}}]}} =
197 test "edited note, badly-formed formerRepresentations", %{external_rep: external_rep} do
198 external_rep = Map.put(external_rep, "formerRepresentations", %{})
200 assert {:error, _} = ObjectValidator.validate(external_rep, [])
203 test "edited note, badly-formed history item", %{external_rep: external_rep} do
205 Enum.at(external_rep["formerRepresentations"]["orderedItems"], 0)
206 |> Map.put("type", "Foo")
211 ["formerRepresentations", "orderedItems"],
215 assert {:error, _} = ObjectValidator.validate(external_rep, [])