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