1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.Web.ConnCase
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
16 setup do: clear_config([:instance, :show_reactions])
18 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
20 other_user = insert(:user)
22 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
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)
31 # We return the status, but this our implementation detail.
32 assert %{"id" => id} = result
33 assert to_string(activity.id) == id
35 assert result["pleroma"]["emoji_reactions"] == [
36 %{"name" => "☕", "count" => 1, "me" => true}
39 # Reacting with a non-emoji
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)
47 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
49 other_user = insert(:user)
51 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
52 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
54 ObanHelpers.perform_all()
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/☕")
62 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
63 assert to_string(activity.id) == id
65 ObanHelpers.perform_all()
67 object = Object.get_by_ap_id(activity.data["object"])
69 assert object.data["reaction_count"] == 0
72 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
74 other_user = insert(:user)
75 doomed_user = insert(:user)
77 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
81 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
82 |> json_response_and_validate_schema(200)
86 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
87 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
89 User.perform(:delete, doomed_user)
93 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
94 |> json_response_and_validate_schema(200)
96 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
98 assert represented_user["id"] == other_user.id
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)
107 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
111 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
112 Pleroma.Config.put([:instance, :show_reactions], false)
115 other_user = insert(:user)
117 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
118 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
122 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
123 |> json_response_and_validate_schema(200)
128 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
130 other_user = insert(:user)
132 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
136 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
137 |> json_response_and_validate_schema(200)
141 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
142 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
144 assert [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] =
146 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
147 |> json_response_and_validate_schema(200)
149 assert represented_user["id"] == other_user.id