430b8b89dc3785a9402474a03c0477cc87ab57c2
[anni] / test / pleroma / web / mastodon_api / controllers / list_controller_test.exs
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
5 defmodule Pleroma.Web.MastodonAPI.ListControllerTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.Repo
9
10   import Pleroma.Factory
11
12   test "creating a list" do
13     %{conn: conn} = oauth_access(["write:lists"])
14
15     assert %{"title" => "cuties"} =
16              conn
17              |> put_req_header("content-type", "application/json")
18              |> post("/api/v1/lists", %{"title" => "cuties"})
19              |> json_response_and_validate_schema(:ok)
20   end
21
22   test "renders error for invalid params" do
23     %{conn: conn} = oauth_access(["write:lists"])
24
25     conn =
26       conn
27       |> put_req_header("content-type", "application/json")
28       |> post("/api/v1/lists", %{"title" => nil})
29
30     assert %{"error" => "title - null value where string expected."} =
31              json_response_and_validate_schema(conn, 400)
32   end
33
34   test "listing a user's lists" do
35     %{conn: conn} = oauth_access(["read:lists", "write:lists"])
36
37     conn
38     |> put_req_header("content-type", "application/json")
39     |> post("/api/v1/lists", %{"title" => "cuties"})
40     |> json_response_and_validate_schema(:ok)
41
42     conn
43     |> put_req_header("content-type", "application/json")
44     |> post("/api/v1/lists", %{"title" => "cofe"})
45     |> json_response_and_validate_schema(:ok)
46
47     conn = get(conn, "/api/v1/lists")
48
49     assert [
50              %{"id" => _, "title" => "cofe"},
51              %{"id" => _, "title" => "cuties"}
52            ] = json_response_and_validate_schema(conn, :ok)
53   end
54
55   test "adding users to a list" do
56     %{user: user, conn: conn} = oauth_access(["write:lists"])
57     other_user = insert(:user)
58     third_user = insert(:user)
59     {:ok, list} = Pleroma.List.create("name", user)
60
61     assert %{} ==
62              conn
63              |> put_req_header("content-type", "application/json")
64              |> post("/api/v1/lists/#{list.id}/accounts", %{
65                "account_ids" => [other_user.id, third_user.id]
66              })
67              |> json_response_and_validate_schema(:ok)
68
69     %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
70     assert length(following) == 2
71     assert other_user.follower_address in following
72     assert third_user.follower_address in following
73   end
74
75   test "removing users from a list, body params" do
76     %{user: user, conn: conn} = oauth_access(["write:lists"])
77     other_user = insert(:user)
78     third_user = insert(:user)
79     fourth_user = insert(:user)
80     {:ok, list} = Pleroma.List.create("name", user)
81     {:ok, list} = Pleroma.List.follow(list, other_user)
82     {:ok, list} = Pleroma.List.follow(list, third_user)
83     {:ok, list} = Pleroma.List.follow(list, fourth_user)
84
85     assert %{} ==
86              conn
87              |> put_req_header("content-type", "application/json")
88              |> delete("/api/v1/lists/#{list.id}/accounts", %{
89                "account_ids" => [other_user.id, fourth_user.id]
90              })
91              |> json_response_and_validate_schema(:ok)
92
93     %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
94     assert following == [third_user.follower_address]
95   end
96
97   test "removing users from a list, query params" do
98     %{user: user, conn: conn} = oauth_access(["write:lists"])
99     other_user = insert(:user)
100     third_user = insert(:user)
101     {:ok, list} = Pleroma.List.create("name", user)
102     {:ok, list} = Pleroma.List.follow(list, other_user)
103     {:ok, list} = Pleroma.List.follow(list, third_user)
104
105     assert %{} ==
106              conn
107              |> put_req_header("content-type", "application/json")
108              |> delete("/api/v1/lists/#{list.id}/accounts?account_ids[]=#{other_user.id}")
109              |> json_response_and_validate_schema(:ok)
110
111     %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
112     assert following == [third_user.follower_address]
113   end
114
115   test "listing users in a list" do
116     %{user: user, conn: conn} = oauth_access(["read:lists"])
117     other_user = insert(:user)
118     {:ok, list} = Pleroma.List.create("name", user)
119     {:ok, list} = Pleroma.List.follow(list, other_user)
120
121     conn =
122       conn
123       |> assign(:user, user)
124       |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
125
126     assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
127     assert id == to_string(other_user.id)
128   end
129
130   test "retrieving a list" do
131     %{user: user, conn: conn} = oauth_access(["read:lists"])
132     {:ok, list} = Pleroma.List.create("name", user)
133
134     conn =
135       conn
136       |> assign(:user, user)
137       |> get("/api/v1/lists/#{list.id}")
138
139     assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
140     assert id == to_string(list.id)
141   end
142
143   test "renders 404 if list is not found" do
144     %{conn: conn} = oauth_access(["read:lists"])
145
146     conn = get(conn, "/api/v1/lists/666")
147
148     assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
149   end
150
151   test "renaming a list" do
152     %{user: user, conn: conn} = oauth_access(["write:lists"])
153     {:ok, list} = Pleroma.List.create("name", user)
154
155     assert %{"title" => "newname"} =
156              conn
157              |> put_req_header("content-type", "application/json")
158              |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
159              |> json_response_and_validate_schema(:ok)
160   end
161
162   test "validates title when renaming a list" do
163     %{user: user, conn: conn} = oauth_access(["write:lists"])
164     {:ok, list} = Pleroma.List.create("name", user)
165
166     conn =
167       conn
168       |> assign(:user, user)
169       |> put_req_header("content-type", "application/json")
170       |> put("/api/v1/lists/#{list.id}", %{"title" => "  "})
171
172     assert %{"error" => "can't be blank"} ==
173              json_response_and_validate_schema(conn, :unprocessable_entity)
174   end
175
176   test "deleting a list" do
177     %{user: user, conn: conn} = oauth_access(["write:lists"])
178     {:ok, list} = Pleroma.List.create("name", user)
179
180     conn = delete(conn, "/api/v1/lists/#{list.id}")
181
182     assert %{} = json_response_and_validate_schema(conn, 200)
183     assert is_nil(Repo.get(Pleroma.List, list.id))
184   end
185 end