Merge branch 'develop' into refactor/discoverable_user_field
[akkoma] / test / pleroma / web / mastodon_api / controllers / list_controller_test.exs
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
5 defmodule Pleroma.Web.MastodonAPI.ListControllerTest do
6 use Pleroma.Web.ConnCase
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 {:ok, list} = Pleroma.List.create("name", user)
59
60 assert %{} ==
61 conn
62 |> put_req_header("content-type", "application/json")
63 |> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
64 |> json_response_and_validate_schema(:ok)
65
66 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
67 assert following == [other_user.follower_address]
68 end
69
70 test "removing users from a list, body params" do
71 %{user: user, conn: conn} = oauth_access(["write:lists"])
72 other_user = insert(:user)
73 third_user = insert(:user)
74 {:ok, list} = Pleroma.List.create("name", user)
75 {:ok, list} = Pleroma.List.follow(list, other_user)
76 {:ok, list} = Pleroma.List.follow(list, third_user)
77
78 assert %{} ==
79 conn
80 |> put_req_header("content-type", "application/json")
81 |> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
82 |> json_response_and_validate_schema(:ok)
83
84 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
85 assert following == [third_user.follower_address]
86 end
87
88 test "removing users from a list, query params" do
89 %{user: user, conn: conn} = oauth_access(["write:lists"])
90 other_user = insert(:user)
91 third_user = insert(:user)
92 {:ok, list} = Pleroma.List.create("name", user)
93 {:ok, list} = Pleroma.List.follow(list, other_user)
94 {:ok, list} = Pleroma.List.follow(list, third_user)
95
96 assert %{} ==
97 conn
98 |> put_req_header("content-type", "application/json")
99 |> delete("/api/v1/lists/#{list.id}/accounts?account_ids[]=#{other_user.id}")
100 |> json_response_and_validate_schema(:ok)
101
102 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
103 assert following == [third_user.follower_address]
104 end
105
106 test "listing users in a list" do
107 %{user: user, conn: conn} = oauth_access(["read:lists"])
108 other_user = insert(:user)
109 {:ok, list} = Pleroma.List.create("name", user)
110 {:ok, list} = Pleroma.List.follow(list, other_user)
111
112 conn =
113 conn
114 |> assign(:user, user)
115 |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
116
117 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
118 assert id == to_string(other_user.id)
119 end
120
121 test "retrieving a list" do
122 %{user: user, conn: conn} = oauth_access(["read:lists"])
123 {:ok, list} = Pleroma.List.create("name", user)
124
125 conn =
126 conn
127 |> assign(:user, user)
128 |> get("/api/v1/lists/#{list.id}")
129
130 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
131 assert id == to_string(list.id)
132 end
133
134 test "renders 404 if list is not found" do
135 %{conn: conn} = oauth_access(["read:lists"])
136
137 conn = get(conn, "/api/v1/lists/666")
138
139 assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
140 end
141
142 test "renaming a list" do
143 %{user: user, conn: conn} = oauth_access(["write:lists"])
144 {:ok, list} = Pleroma.List.create("name", user)
145
146 assert %{"title" => "newname"} =
147 conn
148 |> put_req_header("content-type", "application/json")
149 |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
150 |> json_response_and_validate_schema(:ok)
151 end
152
153 test "validates title when renaming a list" do
154 %{user: user, conn: conn} = oauth_access(["write:lists"])
155 {:ok, list} = Pleroma.List.create("name", user)
156
157 conn =
158 conn
159 |> assign(:user, user)
160 |> put_req_header("content-type", "application/json")
161 |> put("/api/v1/lists/#{list.id}", %{"title" => " "})
162
163 assert %{"error" => "can't be blank"} ==
164 json_response_and_validate_schema(conn, :unprocessable_entity)
165 end
166
167 test "deleting a list" do
168 %{user: user, conn: conn} = oauth_access(["write:lists"])
169 {:ok, list} = Pleroma.List.create("name", user)
170
171 conn = delete(conn, "/api/v1/lists/#{list.id}")
172
173 assert %{} = json_response_and_validate_schema(conn, 200)
174 assert is_nil(Repo.get(Pleroma.List, list.id))
175 end
176 end