1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.ListControllerTest do
6 use Pleroma.Web.ConnCase, async: true
10 import Pleroma.Factory
12 test "creating a list" do
13 %{conn: conn} = oauth_access(["write:lists"])
15 assert %{"title" => "cuties"} =
17 |> put_req_header("content-type", "application/json")
18 |> post("/api/v1/lists", %{"title" => "cuties"})
19 |> json_response_and_validate_schema(:ok)
22 test "renders error for invalid params" do
23 %{conn: conn} = oauth_access(["write:lists"])
27 |> put_req_header("content-type", "application/json")
28 |> post("/api/v1/lists", %{"title" => nil})
30 assert %{"error" => "title - null value where string expected."} =
31 json_response_and_validate_schema(conn, 400)
34 test "listing a user's lists" do
35 %{conn: conn} = oauth_access(["read:lists", "write:lists"])
38 |> put_req_header("content-type", "application/json")
39 |> post("/api/v1/lists", %{"title" => "cuties"})
40 |> json_response_and_validate_schema(:ok)
43 |> put_req_header("content-type", "application/json")
44 |> post("/api/v1/lists", %{"title" => "cofe"})
45 |> json_response_and_validate_schema(:ok)
47 conn = get(conn, "/api/v1/lists")
50 %{"id" => _, "title" => "cofe"},
51 %{"id" => _, "title" => "cuties"}
52 ] = json_response_and_validate_schema(conn, :ok)
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)
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]
67 |> json_response_and_validate_schema(:ok)
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
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)
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]
91 |> json_response_and_validate_schema(:ok)
93 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
94 assert following == [third_user.follower_address]
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)
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)
111 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
112 assert following == [third_user.follower_address]
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)
123 |> assign(:user, user)
124 |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
126 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
127 assert id == to_string(other_user.id)
130 test "retrieving a list" do
131 %{user: user, conn: conn} = oauth_access(["read:lists"])
132 {:ok, list} = Pleroma.List.create("name", user)
136 |> assign(:user, user)
137 |> get("/api/v1/lists/#{list.id}")
139 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
140 assert id == to_string(list.id)
143 test "renders 404 if list is not found" do
144 %{conn: conn} = oauth_access(["read:lists"])
146 conn = get(conn, "/api/v1/lists/666")
148 assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
151 test "renaming a list" do
152 %{user: user, conn: conn} = oauth_access(["write:lists"])
153 {:ok, list} = Pleroma.List.create("name", user)
155 assert %{"title" => "newname"} =
157 |> put_req_header("content-type", "application/json")
158 |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
159 |> json_response_and_validate_schema(:ok)
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)
168 |> assign(:user, user)
169 |> put_req_header("content-type", "application/json")
170 |> put("/api/v1/lists/#{list.id}", %{"title" => " "})
172 assert %{"error" => "can't be blank"} ==
173 json_response_and_validate_schema(conn, :unprocessable_entity)
176 test "deleting a list" do
177 %{user: user, conn: conn} = oauth_access(["write:lists"])
178 {:ok, list} = Pleroma.List.create("name", user)
180 conn = delete(conn, "/api/v1/lists/#{list.id}")
182 assert %{} = json_response_and_validate_schema(conn, 200)
183 assert is_nil(Repo.get(Pleroma.List, list.id))