logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

list_controller_test.exs (6237B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.ListControllerTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.Repo
  7. import Pleroma.Factory
  8. test "creating a list" do
  9. %{conn: conn} = oauth_access(["write:lists"])
  10. assert %{"title" => "cuties"} =
  11. conn
  12. |> put_req_header("content-type", "application/json")
  13. |> post("/api/v1/lists", %{"title" => "cuties"})
  14. |> json_response_and_validate_schema(:ok)
  15. end
  16. test "renders error for invalid params" do
  17. %{conn: conn} = oauth_access(["write:lists"])
  18. conn =
  19. conn
  20. |> put_req_header("content-type", "application/json")
  21. |> post("/api/v1/lists", %{"title" => nil})
  22. assert %{"error" => "title - null value where string expected."} =
  23. json_response_and_validate_schema(conn, 400)
  24. end
  25. test "listing a user's lists" do
  26. %{conn: conn} = oauth_access(["read:lists", "write:lists"])
  27. conn
  28. |> put_req_header("content-type", "application/json")
  29. |> post("/api/v1/lists", %{"title" => "cuties"})
  30. |> json_response_and_validate_schema(:ok)
  31. conn
  32. |> put_req_header("content-type", "application/json")
  33. |> post("/api/v1/lists", %{"title" => "cofe"})
  34. |> json_response_and_validate_schema(:ok)
  35. conn = get(conn, "/api/v1/lists")
  36. assert [
  37. %{"id" => _, "title" => "cofe"},
  38. %{"id" => _, "title" => "cuties"}
  39. ] = json_response_and_validate_schema(conn, :ok)
  40. end
  41. test "adding users to a list" do
  42. %{user: user, conn: conn} = oauth_access(["write:lists"])
  43. other_user = insert(:user)
  44. third_user = insert(:user)
  45. {:ok, list} = Pleroma.List.create("name", user)
  46. assert %{} ==
  47. conn
  48. |> put_req_header("content-type", "application/json")
  49. |> post("/api/v1/lists/#{list.id}/accounts", %{
  50. "account_ids" => [other_user.id, third_user.id]
  51. })
  52. |> json_response_and_validate_schema(:ok)
  53. %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
  54. assert length(following) == 2
  55. assert other_user.follower_address in following
  56. assert third_user.follower_address in following
  57. end
  58. test "removing users from a list, body params" do
  59. %{user: user, conn: conn} = oauth_access(["write:lists"])
  60. other_user = insert(:user)
  61. third_user = insert(:user)
  62. fourth_user = insert(:user)
  63. {:ok, list} = Pleroma.List.create("name", user)
  64. {:ok, list} = Pleroma.List.follow(list, other_user)
  65. {:ok, list} = Pleroma.List.follow(list, third_user)
  66. {:ok, list} = Pleroma.List.follow(list, fourth_user)
  67. assert %{} ==
  68. conn
  69. |> put_req_header("content-type", "application/json")
  70. |> delete("/api/v1/lists/#{list.id}/accounts", %{
  71. "account_ids" => [other_user.id, fourth_user.id]
  72. })
  73. |> json_response_and_validate_schema(:ok)
  74. %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
  75. assert following == [third_user.follower_address]
  76. end
  77. test "removing users from a list, query params" do
  78. %{user: user, conn: conn} = oauth_access(["write:lists"])
  79. other_user = insert(:user)
  80. third_user = insert(:user)
  81. {:ok, list} = Pleroma.List.create("name", user)
  82. {:ok, list} = Pleroma.List.follow(list, other_user)
  83. {:ok, list} = Pleroma.List.follow(list, third_user)
  84. assert %{} ==
  85. conn
  86. |> put_req_header("content-type", "application/json")
  87. |> delete("/api/v1/lists/#{list.id}/accounts?account_ids[]=#{other_user.id}")
  88. |> json_response_and_validate_schema(:ok)
  89. %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
  90. assert following == [third_user.follower_address]
  91. end
  92. test "listing users in a list" do
  93. %{user: user, conn: conn} = oauth_access(["read:lists"])
  94. other_user = insert(:user)
  95. {:ok, list} = Pleroma.List.create("name", user)
  96. {:ok, list} = Pleroma.List.follow(list, other_user)
  97. conn =
  98. conn
  99. |> assign(:user, user)
  100. |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
  101. assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
  102. assert id == to_string(other_user.id)
  103. end
  104. test "retrieving a list" do
  105. %{user: user, conn: conn} = oauth_access(["read:lists"])
  106. {:ok, list} = Pleroma.List.create("name", user)
  107. conn =
  108. conn
  109. |> assign(:user, user)
  110. |> get("/api/v1/lists/#{list.id}")
  111. assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
  112. assert id == to_string(list.id)
  113. end
  114. test "renders 404 if list is not found" do
  115. %{conn: conn} = oauth_access(["read:lists"])
  116. conn = get(conn, "/api/v1/lists/666")
  117. assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
  118. end
  119. test "renaming a list" do
  120. %{user: user, conn: conn} = oauth_access(["write:lists"])
  121. {:ok, list} = Pleroma.List.create("name", user)
  122. assert %{"title" => "newname"} =
  123. conn
  124. |> put_req_header("content-type", "application/json")
  125. |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
  126. |> json_response_and_validate_schema(:ok)
  127. end
  128. test "validates title when renaming a list" do
  129. %{user: user, conn: conn} = oauth_access(["write:lists"])
  130. {:ok, list} = Pleroma.List.create("name", user)
  131. conn =
  132. conn
  133. |> assign(:user, user)
  134. |> put_req_header("content-type", "application/json")
  135. |> put("/api/v1/lists/#{list.id}", %{"title" => " "})
  136. assert %{"error" => "can't be blank"} ==
  137. json_response_and_validate_schema(conn, :unprocessable_entity)
  138. end
  139. test "deleting a list" do
  140. %{user: user, conn: conn} = oauth_access(["write:lists"])
  141. {:ok, list} = Pleroma.List.create("name", user)
  142. conn = delete(conn, "/api/v1/lists/#{list.id}")
  143. assert %{} = json_response_and_validate_schema(conn, 200)
  144. assert is_nil(Repo.get(Pleroma.List, list.id))
  145. end
  146. end