1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
8 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
10 alias Pleroma.Web.OStatus
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Activity
14 import Pleroma.Factory
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 test "a note with null content" do
23 note = insert(:note_activity)
27 |> put_in(["object", "content"], nil)
31 |> Map.put(:data, data)
33 User.get_cached_by_ap_id(note.data["actor"])
35 status = StatusView.render("status.json", %{activity: note})
37 assert status.content == ""
40 test "a note activity" do
41 note = insert(:note_activity)
42 user = User.get_cached_by_ap_id(note.data["actor"])
44 status = StatusView.render("status.json", %{activity: note})
47 (note.data["object"]["published"] || "")
48 |> String.replace(~r/\.\d+Z/, ".000Z")
51 id: to_string(note.id),
52 uri: note.data["object"]["id"],
53 url: note.data["object"]["id"],
54 account: AccountView.render("account.json", %{user: user}),
56 in_reply_to_account_id: nil,
58 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
59 created_at: created_at,
68 spoiler_text: note.data["object"]["summary"],
70 media_attachments: [],
74 name: "#{note.data["object"]["tag"]}",
75 url: "/tag/#{note.data["object"]["tag"]}"
87 static_url: "corndog.png",
88 visible_in_picker: false
93 assert status == expected
97 note = insert(:note_activity)
101 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
103 status = StatusView.render("status.json", %{activity: activity})
105 assert status.in_reply_to_id == to_string(note.id)
107 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
109 assert status.in_reply_to_id == to_string(note.id)
112 test "contains mentions" do
113 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
114 # a user with this ap id might be in the cache.
115 recipient = "https://pleroma.soykaf.com/users/lain"
116 user = insert(:user, %{ap_id: recipient})
118 {:ok, [activity]} = OStatus.handle_incoming(incoming)
120 status = StatusView.render("status.json", %{activity: activity})
122 actor = Repo.get_by(User, ap_id: activity.actor)
124 assert status.mentions ==
125 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
128 test "attachments" do
133 "mediaType" => "image/png",
144 remote_url: "someurl",
145 preview_url: "someurl",
150 assert expected == StatusView.render("attachment.json", %{attachment: object})
152 # If theres a "id", use that instead of the generated one
153 object = Map.put(object, "id", 2)
154 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
159 activity = insert(:note_activity)
161 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
163 represented = StatusView.render("status.json", %{for: user, activity: reblog})
165 assert represented[:id] == to_string(reblog.id)
166 assert represented[:reblog][:id] == to_string(activity.id)
167 assert represented[:emojis] == []
170 test "a peertube video" do
174 ActivityPub.fetch_object_from_id(
175 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
178 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
180 represented = StatusView.render("status.json", %{for: user, activity: activity})
182 assert represented[:id] == to_string(activity.id)
183 assert length(represented[:media_attachments]) == 1
186 describe "build_tags/1" do
187 test "it returns a a dictionary tags" do
193 "href" => "https://kawen.space/users/lain",
194 "name" => "@lain@kawen.space",
199 assert StatusView.build_tags(object_tags) == [
200 %{name: "fediverse", url: "/tag/fediverse"},
201 %{name: "mastodon", url: "/tag/mastodon"},
202 %{name: "nextcloud", url: "/tag/nextcloud"}