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
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" 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 "listing users in a list" do
89 %{user: user, conn: conn} = oauth_access(["read:lists"])
90 other_user = insert(:user)
91 {:ok, list} = Pleroma.List.create("name", user)
92 {:ok, list} = Pleroma.List.follow(list, other_user)
96 |> assign(:user, user)
97 |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
99 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
100 assert id == to_string(other_user.id)
103 test "retrieving a list" do
104 %{user: user, conn: conn} = oauth_access(["read:lists"])
105 {:ok, list} = Pleroma.List.create("name", user)
109 |> assign(:user, user)
110 |> get("/api/v1/lists/#{list.id}")
112 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
113 assert id == to_string(list.id)
116 test "renders 404 if list is not found" do
117 %{conn: conn} = oauth_access(["read:lists"])
119 conn = get(conn, "/api/v1/lists/666")
121 assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
124 test "renaming a list" do
125 %{user: user, conn: conn} = oauth_access(["write:lists"])
126 {:ok, list} = Pleroma.List.create("name", user)
128 assert %{"title" => "newname"} =
130 |> put_req_header("content-type", "application/json")
131 |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
132 |> json_response_and_validate_schema(:ok)
135 test "validates title when renaming a list" do
136 %{user: user, conn: conn} = oauth_access(["write:lists"])
137 {:ok, list} = Pleroma.List.create("name", user)
141 |> assign(:user, user)
142 |> put_req_header("content-type", "application/json")
143 |> put("/api/v1/lists/#{list.id}", %{"title" => " "})
145 assert %{"error" => "can't be blank"} ==
146 json_response_and_validate_schema(conn, :unprocessable_entity)
149 test "deleting a list" do
150 %{user: user, conn: conn} = oauth_access(["write:lists"])
151 {:ok, list} = Pleroma.List.create("name", user)
153 conn = delete(conn, "/api/v1/lists/#{list.id}")
155 assert %{} = json_response_and_validate_schema(conn, 200)
156 assert is_nil(Repo.get(Pleroma.List, list.id))