logo

pleroma

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

mfa_test.exs (1562B)


  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. defmodule Pleroma.MFATest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.MFA
  8. describe "mfa_settings" do
  9. test "returns settings user's" do
  10. user =
  11. insert(:user,
  12. multi_factor_authentication_settings: %MFA.Settings{
  13. enabled: true,
  14. totp: %MFA.Settings.TOTP{secret: "xx", confirmed: true}
  15. }
  16. )
  17. settings = MFA.mfa_settings(user)
  18. assert match?(^settings, %{enabled: true, totp: true})
  19. end
  20. end
  21. describe "generate backup codes" do
  22. test "returns backup codes" do
  23. user = insert(:user)
  24. {:ok, [code1, code2]} = MFA.generate_backup_codes(user)
  25. updated_user = refresh_record(user)
  26. [hash1, hash2] = updated_user.multi_factor_authentication_settings.backup_codes
  27. assert Pbkdf2.verify_pass(code1, hash1)
  28. assert Pbkdf2.verify_pass(code2, hash2)
  29. end
  30. end
  31. describe "invalidate_backup_code" do
  32. test "invalid used code" do
  33. user = insert(:user)
  34. {:ok, _} = MFA.generate_backup_codes(user)
  35. user = refresh_record(user)
  36. assert length(user.multi_factor_authentication_settings.backup_codes) == 2
  37. [hash_code | _] = user.multi_factor_authentication_settings.backup_codes
  38. {:ok, user} = MFA.invalidate_backup_code(user, hash_code)
  39. assert length(user.multi_factor_authentication_settings.backup_codes) == 1
  40. end
  41. end
  42. end