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