Merge branch 'feature/create-tombstone-instead-of-delete' into 'develop'
[akkoma] / test / web / common_api / common_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.CommonAPI.Test do
6 use Pleroma.DataCase
7 alias Pleroma.Web.CommonAPI
8 alias Pleroma.User
9 alias Pleroma.Activity
10
11 import Pleroma.Factory
12
13 test "it de-duplicates tags" do
14 user = insert(:user)
15 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
16
17 assert activity.data["object"]["tag"] == ["2hu"]
18 end
19
20 test "it adds emoji when updating profiles" do
21 user = insert(:user, %{name: ":karjalanpiirakka:"})
22
23 CommonAPI.update(user)
24 user = User.get_cached_by_ap_id(user.ap_id)
25 [karjalanpiirakka] = user.info.source_data["tag"]
26
27 assert karjalanpiirakka["name"] == ":karjalanpiirakka:"
28 end
29
30 describe "posting" do
31 test "it filters out obviously bad tags when accepting a post as HTML" do
32 user = insert(:user)
33
34 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
35
36 {:ok, activity} =
37 CommonAPI.post(user, %{
38 "status" => post,
39 "content_type" => "text/html"
40 })
41
42 content = activity.data["object"]["content"]
43 assert content == "<p><b>2hu</b></p>alert('xss')"
44 end
45
46 test "it filters out obviously bad tags when accepting a post as Markdown" do
47 user = insert(:user)
48
49 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
50
51 {:ok, activity} =
52 CommonAPI.post(user, %{
53 "status" => post,
54 "content_type" => "text/markdown"
55 })
56
57 content = activity.data["object"]["content"]
58 assert content == "<p><b>2hu</b></p>alert('xss')"
59 end
60 end
61
62 describe "reactions" do
63 test "repeating a status" do
64 user = insert(:user)
65 other_user = insert(:user)
66
67 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
68
69 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
70 end
71
72 test "favoriting a status" do
73 user = insert(:user)
74 other_user = insert(:user)
75
76 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
77
78 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
79 end
80
81 test "retweeting a status twice returns an error" do
82 user = insert(:user)
83 other_user = insert(:user)
84
85 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
86 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
87 {:error, _} = CommonAPI.repeat(activity.id, user)
88 end
89
90 test "favoriting a status twice returns an error" do
91 user = insert(:user)
92 other_user = insert(:user)
93
94 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
95 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
96 {:error, _} = CommonAPI.favorite(activity.id, user)
97 end
98 end
99 end