Merge branch 'announce-validator' into 'develop'
[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 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
17 user = insert(:user)
18 other_user = insert(:user)
19
20 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
21
22 result =
23 conn
24 |> assign(:user, other_user)
25 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
26 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
27 |> json_response_and_validate_schema(200)
28
29 # We return the status, but this our implementation detail.
30 assert %{"id" => id} = result
31 assert to_string(activity.id) == id
32
33 assert result["pleroma"]["emoji_reactions"] == [
34 %{"name" => "☕", "count" => 1, "me" => true}
35 ]
36 end
37
38 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
39 user = insert(:user)
40 other_user = insert(:user)
41
42 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
43 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
44
45 ObanHelpers.perform_all()
46
47 result =
48 conn
49 |> assign(:user, other_user)
50 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
51 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
52
53 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
54 assert to_string(activity.id) == id
55
56 ObanHelpers.perform_all()
57
58 object = Object.get_by_ap_id(activity.data["object"])
59
60 assert object.data["reaction_count"] == 0
61 end
62
63 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
64 user = insert(:user)
65 other_user = insert(:user)
66 doomed_user = insert(:user)
67
68 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
69
70 result =
71 conn
72 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
73 |> json_response_and_validate_schema(200)
74
75 assert result == []
76
77 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
78 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
79
80 User.perform(:delete, doomed_user)
81
82 result =
83 conn
84 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
85 |> json_response_and_validate_schema(200)
86
87 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
88
89 assert represented_user["id"] == other_user.id
90
91 result =
92 conn
93 |> assign(:user, other_user)
94 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
95 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
96 |> json_response_and_validate_schema(200)
97
98 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
99 result
100 end
101
102 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
103 user = insert(:user)
104 other_user = insert(:user)
105
106 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
107
108 result =
109 conn
110 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
111 |> json_response_and_validate_schema(200)
112
113 assert result == []
114
115 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
116 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
117
118 assert [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] =
119 conn
120 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
121 |> json_response_and_validate_schema(200)
122
123 assert represented_user["id"] == other_user.id
124 end
125 end