logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

emoji_reaction_controller_test.exs (4891B)


  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. defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
  5. use Oban.Testing, repo: Pleroma.Repo
  6. use Pleroma.Web.ConnCase
  7. alias Pleroma.Object
  8. alias Pleroma.Tests.ObanHelpers
  9. alias Pleroma.User
  10. alias Pleroma.Web.CommonAPI
  11. import Pleroma.Factory
  12. test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
  13. user = insert(:user)
  14. other_user = insert(:user)
  15. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  16. result =
  17. conn
  18. |> assign(:user, other_user)
  19. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  20. |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
  21. |> json_response_and_validate_schema(200)
  22. # We return the status, but this our implementation detail.
  23. assert %{"id" => id} = result
  24. assert to_string(activity.id) == id
  25. assert result["pleroma"]["emoji_reactions"] == [
  26. %{"name" => "☕", "count" => 1, "me" => true}
  27. ]
  28. # Reacting with a non-emoji
  29. assert conn
  30. |> assign(:user, other_user)
  31. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  32. |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
  33. |> json_response_and_validate_schema(400)
  34. end
  35. test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
  36. user = insert(:user)
  37. other_user = insert(:user)
  38. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  39. {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  40. ObanHelpers.perform_all()
  41. result =
  42. conn
  43. |> assign(:user, other_user)
  44. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  45. |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
  46. assert %{"id" => id} = json_response_and_validate_schema(result, 200)
  47. assert to_string(activity.id) == id
  48. ObanHelpers.perform_all()
  49. object = Object.get_by_ap_id(activity.data["object"])
  50. assert object.data["reaction_count"] == 0
  51. end
  52. test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
  53. user = insert(:user)
  54. other_user = insert(:user)
  55. doomed_user = insert(:user)
  56. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  57. result =
  58. conn
  59. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  60. |> json_response_and_validate_schema(200)
  61. assert result == []
  62. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
  63. {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
  64. User.perform(:delete, doomed_user)
  65. result =
  66. conn
  67. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  68. |> json_response_and_validate_schema(200)
  69. [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
  70. assert represented_user["id"] == other_user.id
  71. result =
  72. conn
  73. |> assign(:user, other_user)
  74. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
  75. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  76. |> json_response_and_validate_schema(200)
  77. assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
  78. result
  79. end
  80. test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
  81. clear_config([:instance, :show_reactions], false)
  82. user = insert(:user)
  83. other_user = insert(:user)
  84. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  85. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
  86. result =
  87. conn
  88. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  89. |> json_response_and_validate_schema(200)
  90. assert result == []
  91. end
  92. test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
  93. user = insert(:user)
  94. other_user = insert(:user)
  95. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  96. result =
  97. conn
  98. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
  99. |> json_response_and_validate_schema(200)
  100. assert result == []
  101. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
  102. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  103. assert [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] =
  104. conn
  105. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
  106. |> json_response_and_validate_schema(200)
  107. assert represented_user["id"] == other_user.id
  108. end
  109. end