Merge branch 'develop' into 'remove-twitter-api'
[akkoma] / test / 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" 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 "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)
93
94 conn =
95 conn
96 |> assign(:user, user)
97 |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
98
99 assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)
100 assert id == to_string(other_user.id)
101 end
102
103 test "retrieving a list" do
104 %{user: user, conn: conn} = oauth_access(["read:lists"])
105 {:ok, list} = Pleroma.List.create("name", user)
106
107 conn =
108 conn
109 |> assign(:user, user)
110 |> get("/api/v1/lists/#{list.id}")
111
112 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
113 assert id == to_string(list.id)
114 end
115
116 test "renders 404 if list is not found" do
117 %{conn: conn} = oauth_access(["read:lists"])
118
119 conn = get(conn, "/api/v1/lists/666")
120
121 assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)
122 end
123
124 test "renaming a list" do
125 %{user: user, conn: conn} = oauth_access(["write:lists"])
126 {:ok, list} = Pleroma.List.create("name", user)
127
128 assert %{"title" => "newname"} =
129 conn
130 |> put_req_header("content-type", "application/json")
131 |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
132 |> json_response_and_validate_schema(:ok)
133 end
134
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)
138
139 conn =
140 conn
141 |> assign(:user, user)
142 |> put_req_header("content-type", "application/json")
143 |> put("/api/v1/lists/#{list.id}", %{"title" => " "})
144
145 assert %{"error" => "can't be blank"} ==
146 json_response_and_validate_schema(conn, :unprocessable_entity)
147 end
148
149 test "deleting a list" do
150 %{user: user, conn: conn} = oauth_access(["write:lists"])
151 {:ok, list} = Pleroma.List.create("name", user)
152
153 conn = delete(conn, "/api/v1/lists/#{list.id}")
154
155 assert %{} = json_response_and_validate_schema(conn, 200)
156 assert is_nil(Repo.get(Pleroma.List, list.id))
157 end
158 end