Add PUT /api/pleroma/notification_settings endpoint
[akkoma] / test / web / twitter_api / util_controller_test.exs
1 defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
2 use Pleroma.Web.ConnCase
3
4 alias Pleroma.Notification
5 alias Pleroma.Repo
6 alias Pleroma.User
7 alias Pleroma.Web.CommonAPI
8 import Pleroma.Factory
9
10 describe "POST /api/pleroma/follow_import" do
11 test "it returns HTTP 200", %{conn: conn} do
12 user1 = insert(:user)
13 user2 = insert(:user)
14
15 response =
16 conn
17 |> assign(:user, user1)
18 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
19 |> json_response(:ok)
20
21 assert response == "job started"
22 end
23
24 test "requires 'follow' permission", %{conn: conn} do
25 token1 = insert(:oauth_token, scopes: ["read", "write"])
26 token2 = insert(:oauth_token, scopes: ["follow"])
27 another_user = insert(:user)
28
29 for token <- [token1, token2] do
30 conn =
31 conn
32 |> put_req_header("authorization", "Bearer #{token.token}")
33 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
34
35 if token == token1 do
36 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
37 else
38 assert json_response(conn, 200)
39 end
40 end
41 end
42 end
43
44 describe "POST /api/pleroma/blocks_import" do
45 test "it returns HTTP 200", %{conn: conn} do
46 user1 = insert(:user)
47 user2 = insert(:user)
48
49 response =
50 conn
51 |> assign(:user, user1)
52 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
53 |> json_response(:ok)
54
55 assert response == "job started"
56 end
57 end
58
59 describe "POST /api/pleroma/notifications/read" do
60 test "it marks a single notification as read", %{conn: conn} do
61 user1 = insert(:user)
62 user2 = insert(:user)
63 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
64 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
65 {:ok, [notification1]} = Notification.create_notifications(activity1)
66 {:ok, [notification2]} = Notification.create_notifications(activity2)
67
68 conn
69 |> assign(:user, user1)
70 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
71 |> json_response(:ok)
72
73 assert Repo.get(Notification, notification1.id).seen
74 refute Repo.get(Notification, notification2.id).seen
75 end
76 end
77
78 describe "PUT /api/pleroma/notification_settings" do
79 test "it updates notification settings", %{conn: conn} do
80 user = insert(:user)
81
82 conn
83 |> assign(:user, user)
84 |> put("/api/pleroma/notification_settings", %{
85 "remote" => false,
86 "followers" => false,
87 "bar" => 1
88 })
89 |> json_response(:ok)
90
91 user = Repo.get(User, user.id)
92
93 assert %{"remote" => false, "local" => true, "followers" => false, "follows" => true} ==
94 user.info.notification_settings
95 end
96 end
97
98 describe "GET /api/statusnet/config.json" do
99 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
100 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
101 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
102
103 response =
104 conn
105 |> get("/api/statusnet/config.json")
106 |> json_response(:ok)
107
108 assert response["site"]["safeDMMentionsEnabled"] == "1"
109
110 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
111
112 response =
113 conn
114 |> get("/api/statusnet/config.json")
115 |> json_response(:ok)
116
117 assert response["site"]["safeDMMentionsEnabled"] == "0"
118
119 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
120 end
121
122 test "it returns the managed config", %{conn: conn} do
123 Pleroma.Config.put([:instance, :managed_config], false)
124 Pleroma.Config.put([:fe], theme: "rei-ayanami-towel")
125
126 response =
127 conn
128 |> get("/api/statusnet/config.json")
129 |> json_response(:ok)
130
131 refute response["site"]["pleromafe"]
132
133 Pleroma.Config.put([:instance, :managed_config], true)
134
135 response =
136 conn
137 |> get("/api/statusnet/config.json")
138 |> json_response(:ok)
139
140 assert response["site"]["pleromafe"]
141 end
142
143 test "if :pleroma, :fe is false, it returns the new style config settings", %{conn: conn} do
144 Pleroma.Config.put([:instance, :managed_config], true)
145 Pleroma.Config.put([:fe, :theme], "rei-ayanami-towel")
146 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
147
148 response =
149 conn
150 |> get("/api/statusnet/config.json")
151 |> json_response(:ok)
152
153 assert response["site"]["pleromafe"]["theme"] == "rei-ayanami-towel"
154
155 Pleroma.Config.put([:fe], false)
156
157 response =
158 conn
159 |> get("/api/statusnet/config.json")
160 |> json_response(:ok)
161
162 assert response["site"]["pleromafe"]["theme"] == "asuka-hospital"
163 end
164 end
165
166 describe "GET /api/pleroma/frontend_configurations" do
167 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
168 config = [
169 frontend_a: %{
170 x: 1,
171 y: 2
172 },
173 frontend_b: %{
174 z: 3
175 }
176 ]
177
178 Pleroma.Config.put(:frontend_configurations, config)
179
180 response =
181 conn
182 |> get("/api/pleroma/frontend_configurations")
183 |> json_response(:ok)
184
185 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
186 end
187 end
188 end