logo

pleroma

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

list_controller_test.exs (5929B)


  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.MastodonAPI.ListControllerTest do
  5. use Pleroma.Web.ConnCase
  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. {:ok, list} = Pleroma.List.create("name", user)
  45. assert %{} ==
  46. conn
  47. |> put_req_header("content-type", "application/json")
  48. |> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
  49. |> json_response_and_validate_schema(:ok)
  50. %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
  51. assert following == [other_user.follower_address]
  52. end
  53. test "removing users from a list, body params" do
  54. %{user: user, conn: conn} = oauth_access(["write:lists"])
  55. other_user = insert(:user)
  56. third_user = insert(:user)
  57. {:ok, list} = Pleroma.List.create("name", user)
  58. {:ok, list} = Pleroma.List.follow(list, other_user)
  59. {:ok, list} = Pleroma.List.follow(list, third_user)
  60. assert %{} ==
  61. conn
  62. |> put_req_header("content-type", "application/json")
  63. |> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
  64. |> json_response_and_validate_schema(:ok)
  65. %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
  66. assert following == [third_user.follower_address]
  67. end
  68. test "removing users from a list, query params" do
  69. %{user: user, conn: conn} = oauth_access(["write:lists"])
  70. other_user = insert(:user)
  71. third_user = insert(:user)
  72. {:ok, list} = Pleroma.List.create("name", user)
  73. {:ok, list} = Pleroma.List.follow(list, other_user)
  74. {:ok, list} = Pleroma.List.follow(list, third_user)
  75. assert %{} ==
  76. conn
  77. |> put_req_header("content-type", "application/json")
  78. |> delete("/api/v1/lists/#{list.id}/accounts?account_ids[]=#{other_user.id}")
  79. |> json_response_and_validate_schema(:ok)
  80. %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
  81. assert following == [third_user.follower_address]
  82. end
  83. test "listing users in a list" do
  84. %{user: user, conn: conn} = oauth_access(["read:lists"])
  85. other_user = insert(:user)
  86. {:ok, list} = Pleroma.List.create("name", user)
  87. {:ok, list} = Pleroma.List.follow(list, other_user)
  88. conn =
  89. conn
  90. |> assign(:user, user)
  91. |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
  92. assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
  93. assert id == to_string(other_user.id)
  94. end
  95. test "retrieving a list" do
  96. %{user: user, conn: conn} = oauth_access(["read:lists"])
  97. {:ok, list} = Pleroma.List.create("name", user)
  98. conn =
  99. conn
  100. |> assign(:user, user)
  101. |> get("/api/v1/lists/#{list.id}")
  102. assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
  103. assert id == to_string(list.id)
  104. end
  105. test "renders 404 if list is not found" do
  106. %{conn: conn} = oauth_access(["read:lists"])
  107. conn = get(conn, "/api/v1/lists/666")
  108. assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
  109. end
  110. test "renaming a list" do
  111. %{user: user, conn: conn} = oauth_access(["write:lists"])
  112. {:ok, list} = Pleroma.List.create("name", user)
  113. assert %{"title" => "newname"} =
  114. conn
  115. |> put_req_header("content-type", "application/json")
  116. |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
  117. |> json_response_and_validate_schema(:ok)
  118. end
  119. test "validates title when renaming a list" do
  120. %{user: user, conn: conn} = oauth_access(["write:lists"])
  121. {:ok, list} = Pleroma.List.create("name", user)
  122. conn =
  123. conn
  124. |> assign(:user, user)
  125. |> put_req_header("content-type", "application/json")
  126. |> put("/api/v1/lists/#{list.id}", %{"title" => " "})
  127. assert %{"error" => "can't be blank"} ==
  128. json_response_and_validate_schema(conn, :unprocessable_entity)
  129. end
  130. test "deleting a list" do
  131. %{user: user, conn: conn} = oauth_access(["write:lists"])
  132. {:ok, list} = Pleroma.List.create("name", user)
  133. conn = delete(conn, "/api/v1/lists/#{list.id}")
  134. assert %{} = json_response_and_validate_schema(conn, 200)
  135. assert is_nil(Repo.get(Pleroma.List, list.id))
  136. end
  137. end