Merge branch 'open-api' into 'develop'
[akkoma] / test / web / twitter_api / twitter_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
6 use Pleroma.DataCase
7 alias Pleroma.Repo
8 alias Pleroma.Tests.ObanHelpers
9 alias Pleroma.User
10 alias Pleroma.UserInviteToken
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.TwitterAPI.TwitterAPI
13
14 setup_all do
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
16 :ok
17 end
18
19 test "it registers a new user and returns the user." do
20 data = %{
21 "nickname" => "lain",
22 "email" => "lain@wired.jp",
23 "fullname" => "lain iwakura",
24 "password" => "bear",
25 "confirm" => "bear"
26 }
27
28 {:ok, user} = TwitterAPI.register_user(data)
29
30 fetched_user = User.get_cached_by_nickname("lain")
31
32 assert AccountView.render("show.json", %{user: user}) ==
33 AccountView.render("show.json", %{user: fetched_user})
34 end
35
36 test "it registers a new user with empty string in bio and returns the user." do
37 data = %{
38 "nickname" => "lain",
39 "email" => "lain@wired.jp",
40 "fullname" => "lain iwakura",
41 "bio" => "",
42 "password" => "bear",
43 "confirm" => "bear"
44 }
45
46 {:ok, user} = TwitterAPI.register_user(data)
47
48 fetched_user = User.get_cached_by_nickname("lain")
49
50 assert AccountView.render("show.json", %{user: user}) ==
51 AccountView.render("show.json", %{user: fetched_user})
52 end
53
54 test "it sends confirmation email if :account_activation_required is specified in instance config" do
55 setting = Pleroma.Config.get([:instance, :account_activation_required])
56
57 unless setting do
58 Pleroma.Config.put([:instance, :account_activation_required], true)
59 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
60 end
61
62 data = %{
63 "nickname" => "lain",
64 "email" => "lain@wired.jp",
65 "fullname" => "lain iwakura",
66 "bio" => "",
67 "password" => "bear",
68 "confirm" => "bear"
69 }
70
71 {:ok, user} = TwitterAPI.register_user(data)
72 ObanHelpers.perform_all()
73
74 assert user.confirmation_pending
75
76 email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
77
78 notify_email = Pleroma.Config.get([:instance, :notify_email])
79 instance_name = Pleroma.Config.get([:instance, :name])
80
81 Swoosh.TestAssertions.assert_email_sent(
82 from: {instance_name, notify_email},
83 to: {user.name, user.email},
84 html_body: email.html_body
85 )
86 end
87
88 test "it registers a new user and parses mentions in the bio" do
89 data1 = %{
90 "nickname" => "john",
91 "email" => "john@gmail.com",
92 "fullname" => "John Doe",
93 "bio" => "test",
94 "password" => "bear",
95 "confirm" => "bear"
96 }
97
98 {:ok, user1} = TwitterAPI.register_user(data1)
99
100 data2 = %{
101 "nickname" => "lain",
102 "email" => "lain@wired.jp",
103 "fullname" => "lain iwakura",
104 "bio" => "@john test",
105 "password" => "bear",
106 "confirm" => "bear"
107 }
108
109 {:ok, user2} = TwitterAPI.register_user(data2)
110
111 expected_text =
112 ~s(<span class="h-card"><a class="u-url mention" data-user="#{user1.id}" href="#{
113 user1.ap_id
114 }" rel="ugc">@<span>john</span></a></span> test)
115
116 assert user2.bio == expected_text
117 end
118
119 describe "register with one time token" do
120 setup do: clear_config([:instance, :registrations_open], false)
121
122 test "returns user on success" do
123 {:ok, invite} = UserInviteToken.create_invite()
124
125 data = %{
126 "nickname" => "vinny",
127 "email" => "pasta@pizza.vs",
128 "fullname" => "Vinny Vinesauce",
129 "bio" => "streamer",
130 "password" => "hiptofbees",
131 "confirm" => "hiptofbees",
132 "token" => invite.token
133 }
134
135 {:ok, user} = TwitterAPI.register_user(data)
136
137 fetched_user = User.get_cached_by_nickname("vinny")
138 invite = Repo.get_by(UserInviteToken, token: invite.token)
139
140 assert invite.used == true
141
142 assert AccountView.render("show.json", %{user: user}) ==
143 AccountView.render("show.json", %{user: fetched_user})
144 end
145
146 test "returns error on invalid token" do
147 data = %{
148 "nickname" => "GrimReaper",
149 "email" => "death@reapers.afterlife",
150 "fullname" => "Reaper Grim",
151 "bio" => "Your time has come",
152 "password" => "scythe",
153 "confirm" => "scythe",
154 "token" => "DudeLetMeInImAFairy"
155 }
156
157 {:error, msg} = TwitterAPI.register_user(data)
158
159 assert msg == "Invalid token"
160 refute User.get_cached_by_nickname("GrimReaper")
161 end
162
163 test "returns error on expired token" do
164 {:ok, invite} = UserInviteToken.create_invite()
165 UserInviteToken.update_invite!(invite, used: true)
166
167 data = %{
168 "nickname" => "GrimReaper",
169 "email" => "death@reapers.afterlife",
170 "fullname" => "Reaper Grim",
171 "bio" => "Your time has come",
172 "password" => "scythe",
173 "confirm" => "scythe",
174 "token" => invite.token
175 }
176
177 {:error, msg} = TwitterAPI.register_user(data)
178
179 assert msg == "Expired token"
180 refute User.get_cached_by_nickname("GrimReaper")
181 end
182 end
183
184 describe "registers with date limited token" do
185 setup do: clear_config([:instance, :registrations_open], false)
186
187 setup do
188 data = %{
189 "nickname" => "vinny",
190 "email" => "pasta@pizza.vs",
191 "fullname" => "Vinny Vinesauce",
192 "bio" => "streamer",
193 "password" => "hiptofbees",
194 "confirm" => "hiptofbees"
195 }
196
197 check_fn = fn invite ->
198 data = Map.put(data, "token", invite.token)
199 {:ok, user} = TwitterAPI.register_user(data)
200 fetched_user = User.get_cached_by_nickname("vinny")
201
202 assert AccountView.render("show.json", %{user: user}) ==
203 AccountView.render("show.json", %{user: fetched_user})
204 end
205
206 {:ok, data: data, check_fn: check_fn}
207 end
208
209 test "returns user on success", %{check_fn: check_fn} do
210 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
211
212 check_fn.(invite)
213
214 invite = Repo.get_by(UserInviteToken, token: invite.token)
215
216 refute invite.used
217 end
218
219 test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
220 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
221
222 check_fn.(invite)
223
224 invite = Repo.get_by(UserInviteToken, token: invite.token)
225
226 refute invite.used
227 end
228
229 test "returns an error on overdue date", %{data: data} do
230 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
231
232 data = Map.put(data, "token", invite.token)
233
234 {:error, msg} = TwitterAPI.register_user(data)
235
236 assert msg == "Expired token"
237 refute User.get_cached_by_nickname("vinny")
238 invite = Repo.get_by(UserInviteToken, token: invite.token)
239
240 refute invite.used
241 end
242 end
243
244 describe "registers with reusable token" do
245 setup do: clear_config([:instance, :registrations_open], false)
246
247 test "returns user on success, after him registration fails" do
248 {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
249
250 UserInviteToken.update_invite!(invite, uses: 99)
251
252 data = %{
253 "nickname" => "vinny",
254 "email" => "pasta@pizza.vs",
255 "fullname" => "Vinny Vinesauce",
256 "bio" => "streamer",
257 "password" => "hiptofbees",
258 "confirm" => "hiptofbees",
259 "token" => invite.token
260 }
261
262 {:ok, user} = TwitterAPI.register_user(data)
263 fetched_user = User.get_cached_by_nickname("vinny")
264 invite = Repo.get_by(UserInviteToken, token: invite.token)
265
266 assert invite.used == true
267
268 assert AccountView.render("show.json", %{user: user}) ==
269 AccountView.render("show.json", %{user: fetched_user})
270
271 data = %{
272 "nickname" => "GrimReaper",
273 "email" => "death@reapers.afterlife",
274 "fullname" => "Reaper Grim",
275 "bio" => "Your time has come",
276 "password" => "scythe",
277 "confirm" => "scythe",
278 "token" => invite.token
279 }
280
281 {:error, msg} = TwitterAPI.register_user(data)
282
283 assert msg == "Expired token"
284 refute User.get_cached_by_nickname("GrimReaper")
285 end
286 end
287
288 describe "registers with reusable date limited token" do
289 setup do: clear_config([:instance, :registrations_open], false)
290
291 test "returns user on success" do
292 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
293
294 data = %{
295 "nickname" => "vinny",
296 "email" => "pasta@pizza.vs",
297 "fullname" => "Vinny Vinesauce",
298 "bio" => "streamer",
299 "password" => "hiptofbees",
300 "confirm" => "hiptofbees",
301 "token" => invite.token
302 }
303
304 {:ok, user} = TwitterAPI.register_user(data)
305 fetched_user = User.get_cached_by_nickname("vinny")
306 invite = Repo.get_by(UserInviteToken, token: invite.token)
307
308 refute invite.used
309
310 assert AccountView.render("show.json", %{user: user}) ==
311 AccountView.render("show.json", %{user: fetched_user})
312 end
313
314 test "error after max uses" do
315 {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
316
317 UserInviteToken.update_invite!(invite, uses: 99)
318
319 data = %{
320 "nickname" => "vinny",
321 "email" => "pasta@pizza.vs",
322 "fullname" => "Vinny Vinesauce",
323 "bio" => "streamer",
324 "password" => "hiptofbees",
325 "confirm" => "hiptofbees",
326 "token" => invite.token
327 }
328
329 {:ok, user} = TwitterAPI.register_user(data)
330 fetched_user = User.get_cached_by_nickname("vinny")
331 invite = Repo.get_by(UserInviteToken, token: invite.token)
332 assert invite.used == true
333
334 assert AccountView.render("show.json", %{user: user}) ==
335 AccountView.render("show.json", %{user: fetched_user})
336
337 data = %{
338 "nickname" => "GrimReaper",
339 "email" => "death@reapers.afterlife",
340 "fullname" => "Reaper Grim",
341 "bio" => "Your time has come",
342 "password" => "scythe",
343 "confirm" => "scythe",
344 "token" => invite.token
345 }
346
347 {:error, msg} = TwitterAPI.register_user(data)
348
349 assert msg == "Expired token"
350 refute User.get_cached_by_nickname("GrimReaper")
351 end
352
353 test "returns error on overdue date" do
354 {:ok, invite} =
355 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
356
357 data = %{
358 "nickname" => "GrimReaper",
359 "email" => "death@reapers.afterlife",
360 "fullname" => "Reaper Grim",
361 "bio" => "Your time has come",
362 "password" => "scythe",
363 "confirm" => "scythe",
364 "token" => invite.token
365 }
366
367 {:error, msg} = TwitterAPI.register_user(data)
368
369 assert msg == "Expired token"
370 refute User.get_cached_by_nickname("GrimReaper")
371 end
372
373 test "returns error on with overdue date and after max" do
374 {:ok, invite} =
375 UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
376
377 UserInviteToken.update_invite!(invite, uses: 100)
378
379 data = %{
380 "nickname" => "GrimReaper",
381 "email" => "death@reapers.afterlife",
382 "fullname" => "Reaper Grim",
383 "bio" => "Your time has come",
384 "password" => "scythe",
385 "confirm" => "scythe",
386 "token" => invite.token
387 }
388
389 {:error, msg} = TwitterAPI.register_user(data)
390
391 assert msg == "Expired token"
392 refute User.get_cached_by_nickname("GrimReaper")
393 end
394 end
395
396 test "it returns the error on registration problems" do
397 data = %{
398 "nickname" => "lain",
399 "email" => "lain@wired.jp",
400 "fullname" => "lain iwakura",
401 "bio" => "close the world.",
402 "password" => "bear"
403 }
404
405 {:error, error_object} = TwitterAPI.register_user(data)
406
407 assert is_binary(error_object[:error])
408 refute User.get_cached_by_nickname("lain")
409 end
410
411 setup do
412 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
413 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
414 :ok
415 end
416 end