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