add inbound language test
[akkoma] / test / pleroma / password_test.exs
1 defmodule Pleroma.PasswordTest do
2 use Pleroma.DataCase, async: true
3 import Pleroma.Factory
4 import ExUnit.CaptureLog
5
6 alias Pleroma.Password
7
8 describe "hash_pwd_salt/1" do
9 test "returns a hash" do
10 assert "$argon2id" <> _ = Password.hash_pwd_salt("test")
11 end
12 end
13
14 describe "maybe_update_password/2" do
15 test "with a bcrypt hash, it updates to an argon2 hash" do
16 user = insert(:user, password_hash: Bcrypt.hash_pwd_salt("123"))
17 assert "$2" <> _ = user.password_hash
18
19 {:ok, user} = Password.maybe_update_password(user, "123")
20 assert "$argon2" <> _ = user.password_hash
21 end
22
23 test "with a pbkdf2 hash, it updates to an argon2 hash" do
24 user = insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("123"))
25 assert "$pbkdf2" <> _ = user.password_hash
26
27 {:ok, user} = Password.maybe_update_password(user, "123")
28 assert "$argon2" <> _ = user.password_hash
29 end
30 end
31
32 describe "checkpw/2" do
33 test "check pbkdf2 hash" do
34 hash =
35 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
36
37 assert Password.checkpw("test-password", hash)
38 refute Password.checkpw("test-password1", hash)
39 end
40
41 test "check bcrypt hash" do
42 hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
43
44 assert Password.checkpw("password", hash)
45 refute Password.checkpw("password1", hash)
46 end
47
48 test "check argon2 hash" do
49 hash =
50 "$argon2id$v=19$m=65536,t=8,p=2$zEMMsTuK5KkL5AFWbX7jyQ$VyaQD7PF6e9btz0oH1YiAkWwIGZ7WNDZP8l+a/O171g"
51
52 assert Password.checkpw("password", hash)
53 refute Password.checkpw("password1", hash)
54 end
55
56 test "it returns false when hash invalid" do
57 hash =
58 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
59
60 assert capture_log(fn ->
61 refute Password.checkpw("password", hash)
62 end) =~ "[error] Password hash not recognized"
63 end
64 end
65 end