logo

pleroma

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

conversation_controller_test.exs (4601B)


  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.ConversationControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Conversation.Participation
  7. alias Pleroma.Repo
  8. alias Pleroma.User
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. test "/api/v1/pleroma/conversations/:id" do
  12. user = insert(:user)
  13. %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
  14. {:ok, _activity} =
  15. CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
  16. [participation] = Participation.for_user(other_user)
  17. result =
  18. conn
  19. |> get("/api/v1/pleroma/conversations/#{participation.id}")
  20. |> json_response_and_validate_schema(200)
  21. assert result["id"] == participation.id |> to_string()
  22. end
  23. test "/api/v1/pleroma/conversations/:id/statuses" do
  24. user = insert(:user)
  25. %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
  26. third_user = insert(:user)
  27. {:ok, _activity} =
  28. CommonAPI.post(user, %{status: "Hi @#{third_user.nickname}!", visibility: "direct"})
  29. {:ok, activity} =
  30. CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
  31. [participation] = Participation.for_user(other_user)
  32. {:ok, activity_two} =
  33. CommonAPI.post(other_user, %{
  34. status: "Hi!",
  35. in_reply_to_status_id: activity.id,
  36. in_reply_to_conversation_id: participation.id
  37. })
  38. result =
  39. conn
  40. |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
  41. |> json_response_and_validate_schema(200)
  42. assert length(result) == 2
  43. id_one = activity.id
  44. id_two = activity_two.id
  45. assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
  46. {:ok, %{id: id_three}} =
  47. CommonAPI.post(other_user, %{
  48. status: "Bye!",
  49. in_reply_to_status_id: activity.id,
  50. in_reply_to_conversation_id: participation.id
  51. })
  52. assert [%{"id" => ^id_two}, %{"id" => ^id_three}] =
  53. conn
  54. |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?limit=2")
  55. |> json_response_and_validate_schema(:ok)
  56. assert [%{"id" => ^id_three}] =
  57. conn
  58. |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?min_id=#{id_two}")
  59. |> json_response_and_validate_schema(:ok)
  60. end
  61. test "PATCH /api/v1/pleroma/conversations/:id" do
  62. %{user: user, conn: conn} = oauth_access(["write:conversations"])
  63. other_user = insert(:user)
  64. {:ok, _activity} = CommonAPI.post(user, %{status: "Hi", visibility: "direct"})
  65. [participation] = Participation.for_user(user)
  66. participation = Repo.preload(participation, :recipients)
  67. user = User.get_cached_by_id(user.id)
  68. assert [user] == participation.recipients
  69. assert other_user not in participation.recipients
  70. query = "recipients[]=#{user.id}&recipients[]=#{other_user.id}"
  71. result =
  72. conn
  73. |> patch("/api/v1/pleroma/conversations/#{participation.id}?#{query}")
  74. |> json_response_and_validate_schema(200)
  75. assert result["id"] == participation.id |> to_string
  76. [participation] = Participation.for_user(user)
  77. participation = Repo.preload(participation, :recipients)
  78. assert user in participation.recipients
  79. assert other_user in participation.recipients
  80. end
  81. test "POST /api/v1/pleroma/conversations/read" do
  82. user = insert(:user)
  83. %{user: other_user, conn: conn} = oauth_access(["write:conversations"])
  84. {:ok, _activity} =
  85. CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
  86. {:ok, _activity} =
  87. CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
  88. [participation2, participation1] = Participation.for_user(other_user)
  89. assert Participation.get(participation2.id).read == false
  90. assert Participation.get(participation1.id).read == false
  91. assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
  92. [%{"unread" => false}, %{"unread" => false}] =
  93. conn
  94. |> post("/api/v1/pleroma/conversations/read", %{})
  95. |> json_response_and_validate_schema(200)
  96. [participation2, participation1] = Participation.for_user(other_user)
  97. assert Participation.get(participation2.id).read == true
  98. assert Participation.get(participation1.id).read == true
  99. assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
  100. end
  101. end