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