1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 {:ok, list} = Pleroma.List.create("name", user)
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)
66 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
67 assert following == [other_user.follower_address]
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)
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)
84 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
85 assert following == [third_user.follower_address]
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)
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)
102 %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
103 assert following == [third_user.follower_address]
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)
114 |> assign(:user, user)
115 |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
117 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
118 assert id == to_string(other_user.id)
121 test "retrieving a list" do
122 %{user: user, conn: conn} = oauth_access(["read:lists"])
123 {:ok, list} = Pleroma.List.create("name", user)
127 |> assign(:user, user)
128 |> get("/api/v1/lists/#{list.id}")
130 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
131 assert id == to_string(list.id)
134 test "renders 404 if list is not found" do
135 %{conn: conn} = oauth_access(["read:lists"])
137 conn = get(conn, "/api/v1/lists/666")
139 assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
142 test "renaming a list" do
143 %{user: user, conn: conn} = oauth_access(["write:lists"])
144 {:ok, list} = Pleroma.List.create("name", user)
146 assert %{"title" => "newname"} =
148 |> put_req_header("content-type", "application/json")
149 |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
150 |> json_response_and_validate_schema(:ok)
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)
159 |> assign(:user, user)
160 |> put_req_header("content-type", "application/json")
161 |> put("/api/v1/lists/#{list.id}", %{"title" => " "})
163 assert %{"error" => "can't be blank"} ==
164 json_response_and_validate_schema(conn, :unprocessable_entity)
167 test "deleting a list" do
168 %{user: user, conn: conn} = oauth_access(["write:lists"])
169 {:ok, list} = Pleroma.List.create("name", user)
171 conn = delete(conn, "/api/v1/lists/#{list.id}")
173 assert %{} = json_response_and_validate_schema(conn, 200)
174 assert is_nil(Repo.get(Pleroma.List, list.id))