fix formatting
[akkoma] / test / tasks / user_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.UserTest do
6 alias Pleroma.Repo
7 alias Pleroma.User
8 alias Pleroma.Web.OAuth.Authorization
9 alias Pleroma.Web.OAuth.Token
10
11 use Pleroma.DataCase
12
13 import Pleroma.Factory
14 import ExUnit.CaptureIO
15
16 setup_all do
17 Mix.shell(Mix.Shell.Process)
18
19 on_exit(fn ->
20 Mix.shell(Mix.Shell.IO)
21 end)
22
23 :ok
24 end
25
26 describe "running new" do
27 test "user is created" do
28 # just get random data
29 unsaved = build(:user)
30
31 # prepare to answer yes
32 send(self(), {:mix_shell_input, :yes?, true})
33
34 Mix.Tasks.Pleroma.User.run([
35 "new",
36 unsaved.nickname,
37 unsaved.email,
38 "--name",
39 unsaved.name,
40 "--bio",
41 unsaved.bio,
42 "--password",
43 "test",
44 "--moderator",
45 "--admin"
46 ])
47
48 assert_received {:mix_shell, :info, [message]}
49 assert message =~ "user will be created"
50
51 assert_received {:mix_shell, :yes?, [message]}
52 assert message =~ "Continue"
53
54 assert_received {:mix_shell, :info, [message]}
55 assert message =~ "created"
56
57 user = User.get_cached_by_nickname(unsaved.nickname)
58 assert user.name == unsaved.name
59 assert user.email == unsaved.email
60 assert user.bio == unsaved.bio
61 assert user.info.is_moderator
62 assert user.info.is_admin
63 end
64
65 test "user is not created" do
66 unsaved = build(:user)
67
68 # prepare to answer no
69 send(self(), {:mix_shell_input, :yes?, false})
70
71 Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email])
72
73 assert_received {:mix_shell, :info, [message]}
74 assert message =~ "user will be created"
75
76 assert_received {:mix_shell, :yes?, [message]}
77 assert message =~ "Continue"
78
79 assert_received {:mix_shell, :info, [message]}
80 assert message =~ "will not be created"
81
82 refute User.get_cached_by_nickname(unsaved.nickname)
83 end
84 end
85
86 describe "running rm" do
87 test "user is deleted" do
88 user = insert(:user)
89
90 Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
91
92 assert_received {:mix_shell, :info, [message]}
93 assert message =~ " deleted"
94
95 refute User.get_by_nickname(user.nickname)
96 end
97
98 test "no user to delete" do
99 Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
100
101 assert_received {:mix_shell, :error, [message]}
102 assert message =~ "No local user"
103 end
104 end
105
106 describe "running toggle_activated" do
107 test "user is deactivated" do
108 user = insert(:user)
109
110 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
111
112 assert_received {:mix_shell, :info, [message]}
113 assert message =~ " deactivated"
114
115 user = User.get_cached_by_nickname(user.nickname)
116 assert user.info.deactivated
117 end
118
119 test "user is activated" do
120 user = insert(:user, info: %{deactivated: true})
121
122 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
123
124 assert_received {:mix_shell, :info, [message]}
125 assert message =~ " activated"
126
127 user = User.get_cached_by_nickname(user.nickname)
128 refute user.info.deactivated
129 end
130
131 test "no user to toggle" do
132 Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
133
134 assert_received {:mix_shell, :error, [message]}
135 assert message =~ "No user"
136 end
137 end
138
139 describe "running unsubscribe" do
140 test "user is unsubscribed" do
141 followed = insert(:user)
142 user = insert(:user, %{following: [User.ap_followers(followed)]})
143
144 Mix.Tasks.Pleroma.User.run(["unsubscribe", user.nickname])
145
146 assert_received {:mix_shell, :info, [message]}
147 assert message =~ "Deactivating"
148
149 assert_received {:mix_shell, :info, [message]}
150 assert message =~ "Unsubscribing"
151
152 # Note that the task has delay :timer.sleep(500)
153 assert_received {:mix_shell, :info, [message]}
154 assert message =~ "Successfully unsubscribed"
155
156 user = User.get_cached_by_nickname(user.nickname)
157 assert Enum.empty?(user.following)
158 assert user.info.deactivated
159 end
160
161 test "no user to unsubscribe" do
162 Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistent"])
163
164 assert_received {:mix_shell, :error, [message]}
165 assert message =~ "No user"
166 end
167 end
168
169 describe "running set" do
170 test "All statuses set" do
171 user = insert(:user)
172
173 Mix.Tasks.Pleroma.User.run(["set", user.nickname, "--moderator", "--admin", "--locked"])
174
175 assert_received {:mix_shell, :info, [message]}
176 assert message =~ ~r/Moderator status .* true/
177
178 assert_received {:mix_shell, :info, [message]}
179 assert message =~ ~r/Locked status .* true/
180
181 assert_received {:mix_shell, :info, [message]}
182 assert message =~ ~r/Admin status .* true/
183
184 user = User.get_cached_by_nickname(user.nickname)
185 assert user.info.is_moderator
186 assert user.info.locked
187 assert user.info.is_admin
188 end
189
190 test "All statuses unset" do
191 user = insert(:user, info: %{is_moderator: true, locked: true, is_admin: true})
192
193 Mix.Tasks.Pleroma.User.run([
194 "set",
195 user.nickname,
196 "--no-moderator",
197 "--no-admin",
198 "--no-locked"
199 ])
200
201 assert_received {:mix_shell, :info, [message]}
202 assert message =~ ~r/Moderator status .* false/
203
204 assert_received {:mix_shell, :info, [message]}
205 assert message =~ ~r/Locked status .* false/
206
207 assert_received {:mix_shell, :info, [message]}
208 assert message =~ ~r/Admin status .* false/
209
210 user = User.get_cached_by_nickname(user.nickname)
211 refute user.info.is_moderator
212 refute user.info.locked
213 refute user.info.is_admin
214 end
215
216 test "no user to set status" do
217 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
218
219 assert_received {:mix_shell, :error, [message]}
220 assert message =~ "No local user"
221 end
222 end
223
224 describe "running reset_password" do
225 test "password reset token is generated" do
226 user = insert(:user)
227
228 assert capture_io(fn ->
229 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
230 end) =~ "URL:"
231
232 assert_received {:mix_shell, :info, [message]}
233 assert message =~ "Generated"
234 end
235
236 test "no user to reset password" do
237 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
238
239 assert_received {:mix_shell, :error, [message]}
240 assert message =~ "No local user"
241 end
242 end
243
244 describe "running invite" do
245 test "invite token is generated" do
246 assert capture_io(fn ->
247 Mix.Tasks.Pleroma.User.run(["invite"])
248 end) =~ "http"
249
250 assert_received {:mix_shell, :info, [message]}
251 assert message =~ "Generated user invite token one time"
252 end
253
254 test "token is generated with expires_at" do
255 assert capture_io(fn ->
256 Mix.Tasks.Pleroma.User.run([
257 "invite",
258 "--expires-at",
259 Date.to_string(Date.utc_today())
260 ])
261 end)
262
263 assert_received {:mix_shell, :info, [message]}
264 assert message =~ "Generated user invite token date limited"
265 end
266
267 test "token is generated with max use" do
268 assert capture_io(fn ->
269 Mix.Tasks.Pleroma.User.run([
270 "invite",
271 "--max-use",
272 "5"
273 ])
274 end)
275
276 assert_received {:mix_shell, :info, [message]}
277 assert message =~ "Generated user invite token reusable"
278 end
279
280 test "token is generated with max use and expires date" do
281 assert capture_io(fn ->
282 Mix.Tasks.Pleroma.User.run([
283 "invite",
284 "--max-use",
285 "5",
286 "--expires-at",
287 Date.to_string(Date.utc_today())
288 ])
289 end)
290
291 assert_received {:mix_shell, :info, [message]}
292 assert message =~ "Generated user invite token reusable date limited"
293 end
294 end
295
296 describe "running invites" do
297 test "invites are listed" do
298 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
299
300 {:ok, invite2} =
301 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
302
303 # assert capture_io(fn ->
304 Mix.Tasks.Pleroma.User.run([
305 "invites"
306 ])
307
308 # end)
309
310 assert_received {:mix_shell, :info, [message]}
311 assert_received {:mix_shell, :info, [message2]}
312 assert_received {:mix_shell, :info, [message3]}
313 assert message =~ "Invites list:"
314 assert message2 =~ invite.invite_type
315 assert message3 =~ invite2.invite_type
316 end
317 end
318
319 describe "running revoke_invite" do
320 test "invite is revoked" do
321 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
322
323 assert capture_io(fn ->
324 Mix.Tasks.Pleroma.User.run([
325 "revoke_invite",
326 invite.token
327 ])
328 end)
329
330 assert_received {:mix_shell, :info, [message]}
331 assert message =~ "Invite for token #{invite.token} was revoked."
332 end
333
334 test "it prints an error message when invite is not exist" do
335 Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
336
337 assert_received {:mix_shell, :error, [message]}
338 assert message =~ "No invite found"
339 end
340 end
341
342 describe "running delete_activities" do
343 test "activities are deleted" do
344 %{nickname: nickname} = insert(:user)
345
346 assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
347 assert_received {:mix_shell, :info, [message]}
348 assert message == "User #{nickname} statuses deleted."
349 end
350
351 test "it prints an error message when user is not exist" do
352 Mix.Tasks.Pleroma.User.run(["delete_activities", "foo"])
353
354 assert_received {:mix_shell, :error, [message]}
355 assert message =~ "No local user"
356 end
357 end
358
359 describe "running toggle_confirmed" do
360 test "user is confirmed" do
361 %{id: id, nickname: nickname} = insert(:user, info: %{confirmation_pending: false})
362
363 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
364 assert_received {:mix_shell, :info, [message]}
365 assert message == "#{nickname} needs confirmation."
366
367 user = Repo.get(User, id)
368 assert user.info.confirmation_pending
369 assert user.info.confirmation_token
370 end
371
372 test "user is not confirmed" do
373 %{id: id, nickname: nickname} =
374 insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
375
376 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
377 assert_received {:mix_shell, :info, [message]}
378 assert message == "#{nickname} doesn't need confirmation."
379
380 user = Repo.get(User, id)
381 refute user.info.confirmation_pending
382 refute user.info.confirmation_token
383 end
384
385 test "it prints an error message when user is not exist" do
386 Mix.Tasks.Pleroma.User.run(["toggle_confirmed", "foo"])
387
388 assert_received {:mix_shell, :error, [message]}
389 assert message =~ "No local user"
390 end
391 end
392
393 describe "search" do
394 test "it returns users matching" do
395 user = insert(:user)
396 moon = insert(:user, nickname: "moon", name: "fediverse expert moon")
397 moot = insert(:user, nickname: "moot")
398 kawen = insert(:user, nickname: "kawen", name: "fediverse expert moon")
399
400 {:ok, user} = User.follow(user, kawen)
401
402 assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
403 res = User.search("moo") |> Enum.map(& &1.id)
404 assert moon.id in res
405 assert moot.id in res
406 assert kawen.id in res
407 assert [moon.id, kawen.id] == User.Search.search("moon fediverse") |> Enum.map(& &1.id)
408
409 assert [kawen.id, moon.id] ==
410 User.Search.search("moon fediverse", for_user: user) |> Enum.map(& &1.id)
411 end
412 end
413
414 describe "signing out" do
415 test "it deletes all user's tokens and authorizations" do
416 user = insert(:user)
417 insert(:oauth_token, user: user)
418 insert(:oauth_authorization, user: user)
419
420 assert Repo.get_by(Token, user_id: user.id)
421 assert Repo.get_by(Authorization, user_id: user.id)
422
423 :ok = Mix.Tasks.Pleroma.User.run(["sign_out", user.nickname])
424
425 refute Repo.get_by(Token, user_id: user.id)
426 refute Repo.get_by(Authorization, user_id: user.id)
427 end
428
429 test "it prints an error message when user is not exist" do
430 Mix.Tasks.Pleroma.User.run(["sign_out", "foo"])
431
432 assert_received {:mix_shell, :error, [message]}
433 assert message =~ "No local user"
434 end
435 end
436
437 describe "tagging" do
438 test "it add tags to a user" do
439 user = insert(:user)
440
441 :ok = Mix.Tasks.Pleroma.User.run(["tag", user.nickname, "pleroma"])
442
443 user = User.get_cached_by_nickname(user.nickname)
444 assert "pleroma" in user.tags
445 end
446
447 test "it prints an error message when user is not exist" do
448 Mix.Tasks.Pleroma.User.run(["tag", "foo"])
449
450 assert_received {:mix_shell, :error, [message]}
451 assert message =~ "Could not change user tags"
452 end
453 end
454
455 describe "untagging" do
456 test "it deletes tags from a user" do
457 user = insert(:user, tags: ["pleroma"])
458 assert "pleroma" in user.tags
459
460 :ok = Mix.Tasks.Pleroma.User.run(["untag", user.nickname, "pleroma"])
461
462 user = User.get_cached_by_nickname(user.nickname)
463 assert Enum.empty?(user.tags)
464 end
465
466 test "it prints an error message when user is not exist" do
467 Mix.Tasks.Pleroma.User.run(["untag", "foo"])
468
469 assert_received {:mix_shell, :error, [message]}
470 assert message =~ "Could not change user tags"
471 end
472 end
473 end