Add timeline visibility options
[akkoma] / test / pleroma / web / mastodon_api / controllers / announcement_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AnnouncementControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Announcement
11 alias Pleroma.AnnouncementReadRelationship
12
13 describe "GET /api/v1/announcements" do
14 setup do
15 %{conn: conn} = oauth_access([])
16 {:ok, conn: conn}
17 end
18
19 test "it does not allow guests", %{conn: conn} do
20 _response =
21 conn
22 |> assign(:token, nil)
23 |> get("/api/v1/announcements")
24 |> json_response_and_validate_schema(:forbidden)
25 end
26
27 test "it allows users with scopes" do
28 %{conn: conn} = oauth_access(["read:accounts"])
29
30 _response =
31 conn
32 |> get("/api/v1/announcements")
33 |> json_response_and_validate_schema(:ok)
34 end
35
36 test "it lists all announcements", %{conn: conn} do
37 %{id: id} = insert(:announcement)
38
39 response =
40 conn
41 |> get("/api/v1/announcements")
42 |> json_response_and_validate_schema(:ok)
43
44 assert [%{"id" => ^id}] = response
45 end
46
47 test "it returns time with utc timezone", %{conn: conn} do
48 start_time =
49 NaiveDateTime.utc_now()
50 |> NaiveDateTime.add(-999_999, :second)
51 |> NaiveDateTime.truncate(:second)
52
53 end_time =
54 NaiveDateTime.utc_now()
55 |> NaiveDateTime.add(999_999, :second)
56 |> NaiveDateTime.truncate(:second)
57
58 %{id: id} = insert(:announcement, %{starts_at: start_time, ends_at: end_time})
59
60 response =
61 conn
62 |> get("/api/v1/announcements")
63 |> json_response_and_validate_schema(:ok)
64
65 assert [%{"id" => ^id}] = [announcement] = response
66
67 assert String.ends_with?(announcement["starts_at"], "Z")
68 assert String.ends_with?(announcement["ends_at"], "Z")
69 end
70
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)
74
75 response =
76 conn
77 |> get("/api/v1/announcements")
78 |> json_response_and_validate_schema(:ok)
79
80 assert [] = response
81 end
82
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)
86
87 response =
88 conn
89 |> get("/api/v1/announcements")
90 |> json_response_and_validate_schema(:ok)
91
92 assert [] = response
93 end
94
95 test "when authenticated, also expose read property", %{conn: conn} do
96 %{id: id} = insert(:announcement)
97
98 response =
99 conn
100 |> get("/api/v1/announcements")
101 |> json_response_and_validate_schema(:ok)
102
103 assert [%{"id" => ^id, "read" => false}] = response
104 end
105
106 test "when authenticated and announcement is read by user" do
107 %{id: id} = announcement = insert(:announcement)
108 user = insert(:user)
109
110 AnnouncementReadRelationship.mark_read(user, announcement)
111
112 %{conn: conn} = oauth_access(["read:accounts"], user: user)
113
114 response =
115 conn
116 |> get("/api/v1/announcements")
117 |> json_response_and_validate_schema(:ok)
118
119 assert [%{"id" => ^id, "read" => true}] = response
120 end
121 end
122
123 describe "POST /api/v1/announcements/:id/dismiss" do
124 setup do: oauth_access(["write:accounts"])
125
126 test "it requires auth", %{conn: conn} do
127 %{id: id} = insert(:announcement)
128
129 _response =
130 conn
131 |> assign(:token, nil)
132 |> post("/api/v1/announcements/#{id}/dismiss")
133 |> json_response_and_validate_schema(:forbidden)
134 end
135
136 test "it requires write:accounts oauth scope" do
137 %{id: id} = insert(:announcement)
138
139 %{conn: conn} = oauth_access(["read:accounts"])
140
141 _response =
142 conn
143 |> post("/api/v1/announcements/#{id}/dismiss")
144 |> json_response_and_validate_schema(:forbidden)
145 end
146
147 test "it gives 404 for non-existent announcements", %{conn: conn} do
148 %{id: id} = insert(:announcement)
149
150 _response =
151 conn
152 |> post("/api/v1/announcements/#{id}xxx/dismiss")
153 |> json_response_and_validate_schema(:not_found)
154 end
155
156 test "it marks announcement as read", %{user: user, conn: conn} do
157 %{id: id} = announcement = insert(:announcement)
158
159 refute Announcement.read_by?(announcement, user)
160
161 _response =
162 conn
163 |> post("/api/v1/announcements/#{id}/dismiss")
164 |> json_response_and_validate_schema(:ok)
165
166 assert Announcement.read_by?(announcement, user)
167 end
168 end
169 end