1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPITest do
9 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
13 test "it de-duplicates tags" do
15 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
17 assert activity.data["object"]["tag"] == ["2hu"]
20 test "it adds emoji in the object" do
22 {:ok, activity} = CommonAPI.post(user, %{"status" => ":moominmamma:"})
24 assert activity.data["object"]["emoji"]["moominmamma"]
27 test "it adds emoji when updating profiles" do
28 user = insert(:user, %{name: ":karjalanpiirakka:"})
30 CommonAPI.update(user)
31 user = User.get_cached_by_ap_id(user.ap_id)
32 [karjalanpiirakka] = user.info.source_data["tag"]
34 assert karjalanpiirakka["name"] == ":karjalanpiirakka:"
38 test "it filters out obviously bad tags when accepting a post as HTML" do
41 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
44 CommonAPI.post(user, %{
46 "content_type" => "text/html"
49 content = activity.data["object"]["content"]
50 assert content == "<p><b>2hu</b></p>alert('xss')"
53 test "it filters out obviously bad tags when accepting a post as Markdown" do
56 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
59 CommonAPI.post(user, %{
61 "content_type" => "text/markdown"
64 content = activity.data["object"]["content"]
65 assert content == "<p><b>2hu</b></p>alert('xss')"
69 describe "reactions" do
70 test "repeating a status" do
72 other_user = insert(:user)
74 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
76 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
79 test "favoriting a status" do
81 other_user = insert(:user)
83 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
85 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
88 test "retweeting a status twice returns an error" do
90 other_user = insert(:user)
92 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
93 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
94 {:error, _} = CommonAPI.repeat(activity.id, user)
97 test "favoriting a status twice returns an error" do
99 other_user = insert(:user)
101 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
102 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
103 {:error, _} = CommonAPI.favorite(activity.id, user)
107 describe "pinned statuses" do
109 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
112 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
114 [user: user, activity: activity]
117 test "pin status", %{user: user, activity: activity} do
118 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
121 user = refresh_record(user)
123 assert %User{info: %{pinned_activities: [^id]}} = user
126 test "only self-authored can be pinned", %{activity: activity} do
129 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
132 test "max pinned statuses", %{user: user, activity: activity_one} do
133 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
135 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
137 user = refresh_record(user)
139 assert {:error, "You have already pinned the maximum number of statuses"} =
140 CommonAPI.pin(activity_two.id, user)
143 test "unpin status", %{user: user, activity: activity} do
144 {:ok, activity} = CommonAPI.pin(activity.id, user)
146 user = refresh_record(user)
148 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
150 user = refresh_record(user)
152 assert %User{info: %{pinned_activities: []}} = user
155 test "should unpin when deleting a status", %{user: user, activity: activity} do
156 {:ok, activity} = CommonAPI.pin(activity.id, user)
158 user = refresh_record(user)
160 assert {:ok, _} = CommonAPI.delete(activity.id, user)
162 user = refresh_record(user)
164 assert %User{info: %{pinned_activities: []}} = user
168 describe "mute tests" do
172 activity = insert(:note_activity)
174 [user: user, activity: activity]
177 test "add mute", %{user: user, activity: activity} do
178 {:ok, _} = CommonAPI.add_mute(user, activity)
179 assert CommonAPI.thread_muted?(user, activity)
182 test "remove mute", %{user: user, activity: activity} do
183 CommonAPI.add_mute(user, activity)
184 {:ok, _} = CommonAPI.remove_mute(user, activity)
185 refute CommonAPI.thread_muted?(user, activity)
188 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
189 CommonAPI.add_mute(user, activity)
190 {:error, _} = CommonAPI.add_mute(user, activity)
194 describe "reports" do
195 test "creates a report" do
196 reporter = insert(:user)
197 target_user = insert(:user)
199 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
201 reporter_ap_id = reporter.ap_id
202 target_ap_id = target_user.ap_id
203 activity_ap_id = activity.data["id"]
207 "account_id" => target_user.id,
208 "comment" => comment,
209 "status_ids" => [activity.id]
212 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
215 actor: ^reporter_ap_id,
218 "content" => ^comment,
219 "object" => [^target_ap_id, ^activity_ap_id]
225 describe "reblog muting" do
227 muter = insert(:user)
229 muted = insert(:user)
231 [muter: muter, muted: muted]
234 test "add a reblog mute", %{muter: muter, muted: muted} do
235 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
237 assert Pleroma.User.showing_reblogs?(muter, muted) == false
240 test "remove a reblog mute", %{muter: muter, muted: muted} do
241 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
242 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
244 assert Pleroma.User.showing_reblogs?(muter, muted) == true