1 defmodule Pleroma.PasswordTest do
2 use Pleroma.DataCase, async: true
4 import ExUnit.CaptureLog
8 describe "hash_pwd_salt/1" do
9 test "returns a hash" do
10 assert "$argon2id" <> _ = Password.hash_pwd_salt("test")
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
19 {:ok, user} = Password.maybe_update_password(user, "123")
20 assert "$argon2" <> _ = user.password_hash
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
27 {:ok, user} = Password.maybe_update_password(user, "123")
28 assert "$argon2" <> _ = user.password_hash
32 describe "checkpw/2" do
33 test "check pbkdf2 hash" do
35 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
37 assert Password.checkpw("test-password", hash)
38 refute Password.checkpw("test-password1", hash)
41 test "check bcrypt hash" do
42 hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
44 assert Password.checkpw("password", hash)
45 refute Password.checkpw("password1", hash)
48 test "check argon2 hash" do
50 "$argon2id$v=19$m=65536,t=8,p=2$zEMMsTuK5KkL5AFWbX7jyQ$VyaQD7PF6e9btz0oH1YiAkWwIGZ7WNDZP8l+a/O171g"
52 assert Password.checkpw("password", hash)
53 refute Password.checkpw("password1", hash)
56 test "it returns false when hash invalid" do
58 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
60 assert capture_log(fn ->
61 refute Password.checkpw("password", hash)
62 end) =~ "[error] Password hash not recognized"