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.MastodonAPI.AnnouncementControllerTest do
6 use Pleroma.Web.ConnCase
10 alias Pleroma.Announcement
11 alias Pleroma.AnnouncementReadRelationship
13 describe "GET /api/v1/announcements" do
15 %{conn: conn} = oauth_access([])
19 test "it does not allow guests", %{conn: conn} do
22 |> assign(:token, nil)
23 |> get("/api/v1/announcements")
24 |> json_response_and_validate_schema(:forbidden)
27 test "it allows users with scopes" do
28 %{conn: conn} = oauth_access(["read:accounts"])
32 |> get("/api/v1/announcements")
33 |> json_response_and_validate_schema(:ok)
36 test "it lists all announcements", %{conn: conn} do
37 %{id: id} = insert(:announcement)
41 |> get("/api/v1/announcements")
42 |> json_response_and_validate_schema(:ok)
44 assert [%{"id" => ^id}] = response
47 test "it returns time with utc timezone", %{conn: conn} do
49 NaiveDateTime.utc_now()
50 |> NaiveDateTime.add(-999_999, :second)
51 |> NaiveDateTime.truncate(:second)
54 NaiveDateTime.utc_now()
55 |> NaiveDateTime.add(999_999, :second)
56 |> NaiveDateTime.truncate(:second)
58 %{id: id} = insert(:announcement, %{starts_at: start_time, ends_at: end_time})
62 |> get("/api/v1/announcements")
63 |> json_response_and_validate_schema(:ok)
65 assert [%{"id" => ^id}] = [announcement] = response
67 assert String.ends_with?(announcement["starts_at"], "Z")
68 assert String.ends_with?(announcement["ends_at"], "Z")
71 test "it does not list announcements starting after current time", %{conn: conn} do
72 time = NaiveDateTime.utc_now() |> NaiveDateTime.add(999_999, :second)
73 insert(:announcement, starts_at: time)
77 |> get("/api/v1/announcements")
78 |> json_response_and_validate_schema(:ok)
83 test "it does not list announcements ending before current time", %{conn: conn} do
84 time = NaiveDateTime.utc_now() |> NaiveDateTime.add(-999_999, :second)
85 insert(:announcement, ends_at: time)
89 |> get("/api/v1/announcements")
90 |> json_response_and_validate_schema(:ok)
95 test "when authenticated, also expose read property", %{conn: conn} do
96 %{id: id} = insert(:announcement)
100 |> get("/api/v1/announcements")
101 |> json_response_and_validate_schema(:ok)
103 assert [%{"id" => ^id, "read" => false}] = response
106 test "when authenticated and announcement is read by user" do
107 %{id: id} = announcement = insert(:announcement)
110 AnnouncementReadRelationship.mark_read(user, announcement)
112 %{conn: conn} = oauth_access(["read:accounts"], user: user)
116 |> get("/api/v1/announcements")
117 |> json_response_and_validate_schema(:ok)
119 assert [%{"id" => ^id, "read" => true}] = response
123 describe "POST /api/v1/announcements/:id/dismiss" do
124 setup do: oauth_access(["write:accounts"])
126 test "it requires auth", %{conn: conn} do
127 %{id: id} = insert(:announcement)
131 |> assign(:token, nil)
132 |> post("/api/v1/announcements/#{id}/dismiss")
133 |> json_response_and_validate_schema(:forbidden)
136 test "it requires write:accounts oauth scope" do
137 %{id: id} = insert(:announcement)
139 %{conn: conn} = oauth_access(["read:accounts"])
143 |> post("/api/v1/announcements/#{id}/dismiss")
144 |> json_response_and_validate_schema(:forbidden)
147 test "it gives 404 for non-existent announcements", %{conn: conn} do
148 %{id: id} = insert(:announcement)
152 |> post("/api/v1/announcements/#{id}xxx/dismiss")
153 |> json_response_and_validate_schema(:not_found)
156 test "it marks announcement as read", %{user: user, conn: conn} do
157 %{id: id} = announcement = insert(:announcement)
159 refute Announcement.read_by?(announcement, user)
163 |> post("/api/v1/announcements/#{id}/dismiss")
164 |> json_response_and_validate_schema(:ok)
166 assert Announcement.read_by?(announcement, user)