logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

user_test.exs (18228B)


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