Feature/896 toggling confirmation
[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.User
7 use Pleroma.DataCase
8
9 import Pleroma.Factory
10 import ExUnit.CaptureIO
11
12 setup_all do
13 Mix.shell(Mix.Shell.Process)
14
15 on_exit(fn ->
16 Mix.shell(Mix.Shell.IO)
17 end)
18
19 :ok
20 end
21
22 describe "running new" do
23 test "user is created" do
24 # just get random data
25 unsaved = build(:user)
26
27 # prepare to answer yes
28 send(self(), {:mix_shell_input, :yes?, true})
29
30 Mix.Tasks.Pleroma.User.run([
31 "new",
32 unsaved.nickname,
33 unsaved.email,
34 "--name",
35 unsaved.name,
36 "--bio",
37 unsaved.bio,
38 "--password",
39 "test",
40 "--moderator",
41 "--admin"
42 ])
43
44 assert_received {:mix_shell, :info, [message]}
45 assert message =~ "user will be created"
46
47 assert_received {:mix_shell, :yes?, [message]}
48 assert message =~ "Continue"
49
50 assert_received {:mix_shell, :info, [message]}
51 assert message =~ "created"
52
53 user = User.get_cached_by_nickname(unsaved.nickname)
54 assert user.name == unsaved.name
55 assert user.email == unsaved.email
56 assert user.bio == unsaved.bio
57 assert user.info.is_moderator
58 assert user.info.is_admin
59 end
60
61 test "user is not created" do
62 unsaved = build(:user)
63
64 # prepare to answer no
65 send(self(), {:mix_shell_input, :yes?, false})
66
67 Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email])
68
69 assert_received {:mix_shell, :info, [message]}
70 assert message =~ "user will be created"
71
72 assert_received {:mix_shell, :yes?, [message]}
73 assert message =~ "Continue"
74
75 assert_received {:mix_shell, :info, [message]}
76 assert message =~ "will not be created"
77
78 refute User.get_cached_by_nickname(unsaved.nickname)
79 end
80 end
81
82 describe "running rm" do
83 test "user is deleted" do
84 user = insert(:user)
85
86 Mix.Tasks.Pleroma.User.run(["rm", user.nickname])
87
88 assert_received {:mix_shell, :info, [message]}
89 assert message =~ " deleted"
90
91 user = User.get_cached_by_nickname(user.nickname)
92 assert user.info.deactivated
93 end
94
95 test "no user to delete" do
96 Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"])
97
98 assert_received {:mix_shell, :error, [message]}
99 assert message =~ "No local user"
100 end
101 end
102
103 describe "running toggle_activated" do
104 test "user is deactivated" do
105 user = insert(:user)
106
107 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
108
109 assert_received {:mix_shell, :info, [message]}
110 assert message =~ " deactivated"
111
112 user = User.get_cached_by_nickname(user.nickname)
113 assert user.info.deactivated
114 end
115
116 test "user is activated" do
117 user = insert(:user, info: %{deactivated: true})
118
119 Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname])
120
121 assert_received {:mix_shell, :info, [message]}
122 assert message =~ " activated"
123
124 user = User.get_cached_by_nickname(user.nickname)
125 refute user.info.deactivated
126 end
127
128 test "no user to toggle" do
129 Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"])
130
131 assert_received {:mix_shell, :error, [message]}
132 assert message =~ "No user"
133 end
134 end
135
136 describe "running unsubscribe" do
137 test "user is unsubscribed" do
138 followed = insert(:user)
139 user = insert(:user, %{following: [User.ap_followers(followed)]})
140
141 Mix.Tasks.Pleroma.User.run(["unsubscribe", user.nickname])
142
143 assert_received {:mix_shell, :info, [message]}
144 assert message =~ "Deactivating"
145
146 assert_received {:mix_shell, :info, [message]}
147 assert message =~ "Unsubscribing"
148
149 # Note that the task has delay :timer.sleep(500)
150 assert_received {:mix_shell, :info, [message]}
151 assert message =~ "Successfully unsubscribed"
152
153 user = User.get_cached_by_nickname(user.nickname)
154 assert Enum.empty?(user.following)
155 assert user.info.deactivated
156 end
157
158 test "no user to unsubscribe" do
159 Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistent"])
160
161 assert_received {:mix_shell, :error, [message]}
162 assert message =~ "No user"
163 end
164 end
165
166 describe "running set" do
167 test "All statuses set" do
168 user = insert(:user)
169
170 Mix.Tasks.Pleroma.User.run(["set", user.nickname, "--moderator", "--admin", "--locked"])
171
172 assert_received {:mix_shell, :info, [message]}
173 assert message =~ ~r/Moderator status .* true/
174
175 assert_received {:mix_shell, :info, [message]}
176 assert message =~ ~r/Locked status .* true/
177
178 assert_received {:mix_shell, :info, [message]}
179 assert message =~ ~r/Admin status .* true/
180
181 user = User.get_cached_by_nickname(user.nickname)
182 assert user.info.is_moderator
183 assert user.info.locked
184 assert user.info.is_admin
185 end
186
187 test "All statuses unset" do
188 user = insert(:user, info: %{is_moderator: true, locked: true, is_admin: true})
189
190 Mix.Tasks.Pleroma.User.run([
191 "set",
192 user.nickname,
193 "--no-moderator",
194 "--no-admin",
195 "--no-locked"
196 ])
197
198 assert_received {:mix_shell, :info, [message]}
199 assert message =~ ~r/Moderator status .* false/
200
201 assert_received {:mix_shell, :info, [message]}
202 assert message =~ ~r/Locked status .* false/
203
204 assert_received {:mix_shell, :info, [message]}
205 assert message =~ ~r/Admin status .* false/
206
207 user = User.get_cached_by_nickname(user.nickname)
208 refute user.info.is_moderator
209 refute user.info.locked
210 refute user.info.is_admin
211 end
212
213 test "no user to set status" do
214 Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"])
215
216 assert_received {:mix_shell, :error, [message]}
217 assert message =~ "No local user"
218 end
219 end
220
221 describe "running reset_password" do
222 test "password reset token is generated" do
223 user = insert(:user)
224
225 assert capture_io(fn ->
226 Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname])
227 end) =~ "URL:"
228
229 assert_received {:mix_shell, :info, [message]}
230 assert message =~ "Generated"
231 end
232
233 test "no user to reset password" do
234 Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"])
235
236 assert_received {:mix_shell, :error, [message]}
237 assert message =~ "No local user"
238 end
239 end
240
241 describe "running invite" do
242 test "invite token is generated" do
243 assert capture_io(fn ->
244 Mix.Tasks.Pleroma.User.run(["invite"])
245 end) =~ "http"
246
247 assert_received {:mix_shell, :info, [message]}
248 assert message =~ "Generated user invite token one time"
249 end
250
251 test "token is generated with expires_at" do
252 assert capture_io(fn ->
253 Mix.Tasks.Pleroma.User.run([
254 "invite",
255 "--expires-at",
256 Date.to_string(Date.utc_today())
257 ])
258 end)
259
260 assert_received {:mix_shell, :info, [message]}
261 assert message =~ "Generated user invite token date limited"
262 end
263
264 test "token is generated with max use" do
265 assert capture_io(fn ->
266 Mix.Tasks.Pleroma.User.run([
267 "invite",
268 "--max-use",
269 "5"
270 ])
271 end)
272
273 assert_received {:mix_shell, :info, [message]}
274 assert message =~ "Generated user invite token reusable"
275 end
276
277 test "token is generated with max use and expires date" do
278 assert capture_io(fn ->
279 Mix.Tasks.Pleroma.User.run([
280 "invite",
281 "--max-use",
282 "5",
283 "--expires-at",
284 Date.to_string(Date.utc_today())
285 ])
286 end)
287
288 assert_received {:mix_shell, :info, [message]}
289 assert message =~ "Generated user invite token reusable date limited"
290 end
291 end
292
293 describe "running invites" do
294 test "invites are listed" do
295 {:ok, invite} = Pleroma.UserInviteToken.create_invite()
296
297 {:ok, invite2} =
298 Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
299
300 # assert capture_io(fn ->
301 Mix.Tasks.Pleroma.User.run([
302 "invites"
303 ])
304
305 # end)
306
307 assert_received {:mix_shell, :info, [message]}
308 assert_received {:mix_shell, :info, [message2]}
309 assert_received {:mix_shell, :info, [message3]}
310 assert message =~ "Invites list:"
311 assert message2 =~ invite.invite_type
312 assert message3 =~ invite2.invite_type
313 end
314 end
315
316 describe "running revoke_invite" do
317 test "invite is revoked" do
318 {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
319
320 assert capture_io(fn ->
321 Mix.Tasks.Pleroma.User.run([
322 "revoke_invite",
323 invite.token
324 ])
325 end)
326
327 assert_received {:mix_shell, :info, [message]}
328 assert message =~ "Invite for token #{invite.token} was revoked."
329 end
330 end
331
332 describe "running delete_activities" do
333 test "activities are deleted" do
334 %{nickname: nickname} = insert(:user)
335
336 assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
337 assert_received {:mix_shell, :info, [message]}
338 assert message == "User #{nickname} statuses deleted."
339 end
340 end
341
342 describe "running toggle_confirmed" do
343 test "user is confirmed" do
344 %{id: id, nickname: nickname} = insert(:user, info: %{confirmation_pending: false})
345
346 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
347 assert_received {:mix_shell, :info, [message]}
348 assert message == "#{nickname} needs confirmation."
349
350 user = Repo.get(User, id)
351 assert user.info.confirmation_pending
352 assert user.info.confirmation_token
353 end
354
355 test "user is not confirmed" do
356 %{id: id, nickname: nickname} =
357 insert(:user, info: %{confirmation_pending: true, confirmation_token: "some token"})
358
359 assert :ok = Mix.Tasks.Pleroma.User.run(["toggle_confirmed", nickname])
360 assert_received {:mix_shell, :info, [message]}
361 assert message == "#{nickname} doesn't need confirmation."
362
363 user = Repo.get(User, id)
364 refute user.info.confirmation_pending
365 refute user.info.confirmation_token
366 end
367 end
368 end