Merge remote-tracking branch 'upstream/develop' into hide-reactions
[akkoma] / test / web / pleroma_api / controllers / emoji_reaction_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.Web.ConnCase
8
9 alias Pleroma.Object
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13
14 import Pleroma.Factory
15
16 setup do: clear_config([:instance, :show_reactions])
17
18 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
19 user = insert(:user)
20 other_user = insert(:user)
21
22 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
23
24 result =
25 conn
26 |> assign(:user, other_user)
27 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
28 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
29 |> json_response_and_validate_schema(200)
30
31 # We return the status, but this our implementation detail.
32 assert %{"id" => id} = result
33 assert to_string(activity.id) == id
34
35 assert result["pleroma"]["emoji_reactions"] == [
36 %{"name" => "☕", "count" => 1, "me" => true}
37 ]
38
39 # Reacting with a non-emoji
40 assert conn
41 |> assign(:user, other_user)
42 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
43 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
44 |> json_response_and_validate_schema(400)
45 end
46
47 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
48 user = insert(:user)
49 other_user = insert(:user)
50
51 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
52 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
53
54 ObanHelpers.perform_all()
55
56 result =
57 conn
58 |> assign(:user, other_user)
59 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
60 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
61
62 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
63 assert to_string(activity.id) == id
64
65 ObanHelpers.perform_all()
66
67 object = Object.get_by_ap_id(activity.data["object"])
68
69 assert object.data["reaction_count"] == 0
70 end
71
72 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
73 user = insert(:user)
74 other_user = insert(:user)
75 doomed_user = insert(:user)
76
77 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
78
79 result =
80 conn
81 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
82 |> json_response_and_validate_schema(200)
83
84 assert result == []
85
86 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
87 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
88
89 User.perform(:delete, doomed_user)
90
91 result =
92 conn
93 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
94 |> json_response_and_validate_schema(200)
95
96 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
97
98 assert represented_user["id"] == other_user.id
99
100 result =
101 conn
102 |> assign(:user, other_user)
103 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
104 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
105 |> json_response_and_validate_schema(200)
106
107 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
108 result
109 end
110
111 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
112 Pleroma.Config.put([:instance, :show_reactions], false)
113
114 user = insert(:user)
115 other_user = insert(:user)
116
117 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
118 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
119
120 result =
121 conn
122 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
123 |> json_response_and_validate_schema(200)
124
125 assert result == []
126 end
127
128 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
129 user = insert(:user)
130 other_user = insert(:user)
131
132 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
133
134 result =
135 conn
136 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
137 |> json_response_and_validate_schema(200)
138
139 assert result == []
140
141 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
142 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
143
144 assert [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] =
145 conn
146 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
147 |> json_response_and_validate_schema(200)
148
149 assert represented_user["id"] == other_user.id
150 end
151 end