1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest do
6 use Pleroma.Web.ConnCase, async: false
11 clear_config([:instance, :admin_privileges], [:announcements_manage_announcements])
12 admin = insert(:user, is_admin: true)
13 token = insert(:oauth_admin_token, user: admin)
17 |> assign(:user, admin)
18 |> assign(:token, token)
20 {:ok, %{admin: admin, token: token, conn: conn}}
23 describe "GET /api/v1/pleroma/admin/announcements" do
24 test "it lists all announcements", %{conn: conn} do
25 %{id: id} = insert(:announcement)
29 |> get("/api/v1/pleroma/admin/announcements")
30 |> json_response_and_validate_schema(:ok)
32 assert [%{"id" => ^id}] = response
35 test "it requires privileged role :announcements_manage_announcements", %{conn: conn} do
37 |> get("/api/v1/pleroma/admin/announcements")
38 |> json_response_and_validate_schema(:ok)
40 clear_config([:instance, :admin_privileges], [])
43 |> get("/api/v1/pleroma/admin/announcements")
44 |> json_response(:forbidden)
47 test "it paginates announcements", %{conn: conn} do
48 _announcements = Enum.map(0..20, fn _ -> insert(:announcement) end)
52 |> get("/api/v1/pleroma/admin/announcements")
53 |> json_response_and_validate_schema(:ok)
55 assert length(response) == 20
58 test "it paginates announcements with custom params", %{conn: conn} do
59 announcements = Enum.map(0..20, fn _ -> insert(:announcement) end)
63 |> get("/api/v1/pleroma/admin/announcements", limit: 5, offset: 7)
64 |> json_response_and_validate_schema(:ok)
66 assert length(response) == 5
67 assert Enum.at(response, 0)["id"] == Enum.at(announcements, 7).id
70 test "it returns empty list with out-of-bounds offset", %{conn: conn} do
71 _announcements = Enum.map(0..20, fn _ -> insert(:announcement) end)
75 |> get("/api/v1/pleroma/admin/announcements", offset: 21)
76 |> json_response_and_validate_schema(:ok)
81 test "it rejects invalid pagination params", %{conn: conn} do
83 |> get("/api/v1/pleroma/admin/announcements", limit: 0)
84 |> json_response_and_validate_schema(400)
87 |> get("/api/v1/pleroma/admin/announcements", limit: -1)
88 |> json_response_and_validate_schema(400)
91 |> get("/api/v1/pleroma/admin/announcements", offset: -1)
92 |> json_response_and_validate_schema(400)
96 describe "GET /api/v1/pleroma/admin/announcements/:id" do
97 test "it displays one announcement", %{conn: conn} do
98 %{id: id} = insert(:announcement)
102 |> get("/api/v1/pleroma/admin/announcements/#{id}")
103 |> json_response_and_validate_schema(:ok)
105 assert %{"id" => ^id} = response
108 test "it requires privileged role :announcements_manage_announcements", %{conn: conn} do
109 %{id: id} = insert(:announcement)
112 |> get("/api/v1/pleroma/admin/announcements/#{id}")
113 |> json_response_and_validate_schema(:ok)
115 clear_config([:instance, :admin_privileges], [])
118 |> get("/api/v1/pleroma/admin/announcements/#{id}")
119 |> json_response(:forbidden)
122 test "it returns not found for non-existent id", %{conn: conn} do
123 %{id: id} = insert(:announcement)
127 |> get("/api/v1/pleroma/admin/announcements/#{id}xxx")
128 |> json_response_and_validate_schema(:not_found)
132 describe "DELETE /api/v1/pleroma/admin/announcements/:id" do
133 test "it deletes specified announcement", %{conn: conn} do
134 %{id: id} = insert(:announcement)
138 |> delete("/api/v1/pleroma/admin/announcements/#{id}")
139 |> json_response_and_validate_schema(:ok)
142 test "it requires privileged role :announcements_manage_announcements", %{conn: conn} do
143 %{id: id} = insert(:announcement)
146 |> delete("/api/v1/pleroma/admin/announcements/#{id}")
147 |> json_response_and_validate_schema(:ok)
149 clear_config([:instance, :admin_privileges], [])
152 |> delete("/api/v1/pleroma/admin/announcements/#{id}")
153 |> json_response(:forbidden)
156 test "it returns not found for non-existent id", %{conn: conn} do
157 %{id: id} = insert(:announcement)
161 |> delete("/api/v1/pleroma/admin/announcements/#{id}xxx")
162 |> json_response_and_validate_schema(:not_found)
164 assert %{id: ^id} = Pleroma.Announcement.get_by_id(id)
168 describe "PATCH /api/v1/pleroma/admin/announcements/:id" do
169 test "it returns not found for non-existent id", %{conn: conn} do
170 %{id: id} = insert(:announcement)
174 |> put_req_header("content-type", "application/json")
175 |> patch("/api/v1/pleroma/admin/announcements/#{id}xxx", %{})
176 |> json_response_and_validate_schema(:not_found)
178 assert %{id: ^id} = Pleroma.Announcement.get_by_id(id)
181 test "it updates a field", %{conn: conn} do
182 %{id: id} = insert(:announcement)
184 now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
185 starts_at = NaiveDateTime.add(now, -10, :second)
189 |> put_req_header("content-type", "application/json")
190 |> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
191 starts_at: NaiveDateTime.to_iso8601(starts_at)
193 |> json_response_and_validate_schema(:ok)
195 new = Pleroma.Announcement.get_by_id(id)
197 assert NaiveDateTime.compare(new.starts_at, starts_at) == :eq
200 test "it requires privileged role :announcements_manage_announcements", %{conn: conn} do
201 %{id: id} = insert(:announcement)
203 now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
204 starts_at = NaiveDateTime.add(now, -10, :second)
207 |> put_req_header("content-type", "application/json")
208 |> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
209 starts_at: NaiveDateTime.to_iso8601(starts_at)
211 |> json_response_and_validate_schema(:ok)
213 clear_config([:instance, :admin_privileges], [])
216 |> put_req_header("content-type", "application/json")
217 |> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
218 starts_at: NaiveDateTime.to_iso8601(starts_at)
220 |> json_response(:forbidden)
223 test "it updates with time with utc timezone", %{conn: conn} do
224 %{id: id} = insert(:announcement)
226 now = DateTime.now("Etc/UTC") |> elem(1) |> DateTime.truncate(:second)
227 starts_at = DateTime.add(now, -10, :second)
231 |> put_req_header("content-type", "application/json")
232 |> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
233 starts_at: DateTime.to_iso8601(starts_at)
235 |> json_response_and_validate_schema(:ok)
237 new = Pleroma.Announcement.get_by_id(id)
239 assert DateTime.compare(new.starts_at, starts_at) == :eq
242 test "it updates a data field", %{conn: conn} do
243 %{id: id} = announcement = insert(:announcement, data: %{"all_day" => true})
245 assert announcement.data["all_day"] == true
247 new_content = "new content"
251 |> put_req_header("content-type", "application/json")
252 |> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
255 |> json_response_and_validate_schema(:ok)
257 assert response["content"] == new_content
258 assert response["all_day"] == true
260 new = Pleroma.Announcement.get_by_id(id)
262 assert new.data["content"] == new_content
263 assert new.data["all_day"] == true
266 test "it nullifies a nullable field", %{conn: conn} do
267 now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
268 starts_at = NaiveDateTime.add(now, -10, :second)
270 %{id: id} = insert(:announcement, starts_at: starts_at)
274 |> put_req_header("content-type", "application/json")
275 |> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
278 |> json_response_and_validate_schema(:ok)
280 assert response["starts_at"] == nil
282 new = Pleroma.Announcement.get_by_id(id)
284 assert new.starts_at == nil
288 describe "POST /api/v1/pleroma/admin/announcements" do
289 test "it creates an announcement", %{conn: conn} do
290 content = "test post announcement api"
292 now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
293 starts_at = NaiveDateTime.add(now, -10, :second)
294 ends_at = NaiveDateTime.add(now, 10, :second)
298 |> put_req_header("content-type", "application/json")
299 |> post("/api/v1/pleroma/admin/announcements", %{
300 "content" => content,
301 "starts_at" => NaiveDateTime.to_iso8601(starts_at),
302 "ends_at" => NaiveDateTime.to_iso8601(ends_at),
305 |> json_response_and_validate_schema(:ok)
307 assert %{"content" => ^content, "all_day" => true} = response
309 announcement = Pleroma.Announcement.get_by_id(response["id"])
311 assert not is_nil(announcement)
313 assert NaiveDateTime.compare(announcement.starts_at, starts_at) == :eq
314 assert NaiveDateTime.compare(announcement.ends_at, ends_at) == :eq
317 test "it requires privileged role :announcements_manage_announcements", %{conn: conn} do
318 content = "test post announcement api"
320 now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
321 starts_at = NaiveDateTime.add(now, -10, :second)
322 ends_at = NaiveDateTime.add(now, 10, :second)
325 |> put_req_header("content-type", "application/json")
326 |> post("/api/v1/pleroma/admin/announcements", %{
327 "content" => content,
328 "starts_at" => NaiveDateTime.to_iso8601(starts_at),
329 "ends_at" => NaiveDateTime.to_iso8601(ends_at),
332 |> json_response_and_validate_schema(:ok)
334 clear_config([:instance, :admin_privileges], [])
337 |> put_req_header("content-type", "application/json")
338 |> post("/api/v1/pleroma/admin/announcements", %{
339 "content" => content,
340 "starts_at" => NaiveDateTime.to_iso8601(starts_at),
341 "ends_at" => NaiveDateTime.to_iso8601(ends_at),
344 |> json_response(:forbidden)
347 test "creating with time with utc timezones", %{conn: conn} do
348 content = "test post announcement api"
350 now = DateTime.now("Etc/UTC") |> elem(1) |> DateTime.truncate(:second)
351 starts_at = DateTime.add(now, -10, :second)
352 ends_at = DateTime.add(now, 10, :second)
356 |> put_req_header("content-type", "application/json")
357 |> post("/api/v1/pleroma/admin/announcements", %{
358 "content" => content,
359 "starts_at" => DateTime.to_iso8601(starts_at),
360 "ends_at" => DateTime.to_iso8601(ends_at),
363 |> json_response_and_validate_schema(:ok)
365 assert %{"content" => ^content, "all_day" => true} = response
367 announcement = Pleroma.Announcement.get_by_id(response["id"])
369 assert not is_nil(announcement)
371 assert DateTime.compare(announcement.starts_at, starts_at) == :eq
372 assert DateTime.compare(announcement.ends_at, ends_at) == :eq