Skip to content

Commit

Permalink
Using test_helper from Comeonin v5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
riverrun committed Feb 27, 2019
1 parent 7687d07 commit 94e942b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 65 deletions.
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule BcryptElixir.Mixfile do
use Mix.Project

@version "2.0.0"
@version "2.0.1"

@description """
Bcrypt password hashing algorithm for Elixir
Expand Down Expand Up @@ -29,7 +29,7 @@ defmodule BcryptElixir.Mixfile do

defp deps do
[
{:comeonin, "~> 5.0"},
{:comeonin, "~> 5.1"},
{:elixir_make, "~> 0.4", runtime: false},
{:ex_doc, "~> 0.19", only: :dev, runtime: false}
]
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"comeonin": {:hex, :comeonin, "5.0.0", "e87716d3b1c31e56312f6a1545a5548cdc80376cff5025fe3b12be2046934837", [:mix], [], "hexpm"},
"comeonin": {:hex, :comeonin, "5.1.0", "dc7dc04cc2fd12ffee16ddf3f91e876ee1a686447be403baf6f89da38e215365", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], [], "hexpm"},
"elixir_make": {:hex, :elixir_make, "0.4.2", "332c649d08c18bc1ecc73b1befc68c647136de4f340b548844efc796405743bf", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
46 changes: 17 additions & 29 deletions test/bcrypt_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,30 @@ defmodule BcryptTest do
use ExUnit.Case
doctest Bcrypt

import BcryptTestHelper
import Comeonin.BehaviourTestHelper

test "hashing and checking passwords" do
wrong_list = ["aged2h$ru", "2dau$ehgr", "rg$deh2au", "2edrah$gu", "$agedhur2", ""]
password_hash_check("hard2guess", wrong_list)
test "implementation of Comeonin.PasswordHash behaviour" do
password = Enum.random(ascii_passwords())
assert correct_password_true(Bcrypt, password)
assert wrong_password_false(Bcrypt, password)
end

test "hashing and checking passwords with characters from the extended ascii set" do
wrong_list = ["eáé åöêô ëaäo", "aäôáö eéoêë å", " aöêôée oåäëá", "åaêöéäëeoô á ", ""]
password_hash_check("aáåä eéê ëoôö", wrong_list)
test "Comeonin.PasswordHash behaviour with non-ascii characters" do
password = Enum.random(non_ascii_passwords())
assert correct_password_true(Bcrypt, password)
assert wrong_password_false(Bcrypt, password)
end

test "hashing and checking passwords with non-ascii characters" do
wrong_list = [
"и Скл;лекьоток к олсомзь",
"кеокок зС омлслтььлок;и",
"е о оиькльлтСо;осккклзм",
""
]

password_hash_check("Сколько лет; сколько зим", wrong_list)
end

test "hashing and checking passwords with mixed characters" do
wrong_list = ["Я☕t☔s❤ùo", "o❤ Я☔ùrtês☕", " ùt❤o☕☔srêЯ", "ù☕os êt❤☔rЯ", ""]
password_hash_check("Я❤três☕ où☔", wrong_list)
end

test "check password using check_pass, which uses the user map as input" do
wrong_list = ["บดสคสััีวร", "สดรบัีสัคว", "สวดัรคบัสี", "ดรสสีวคบัั", "วรคดสัสีับ", ""]
check_pass_check("สวัสดีครับ", wrong_list)
test "add_hash function" do
password = Enum.random(ascii_passwords())
assert add_hash_creates_map(Bcrypt, password)
end

test "add hash to map and set password to nil" do
wrong_list = ["êäöéaoeôáåë", "åáoêëäéôeaö", "aäáeåëéöêôo", ""]
add_hash_check("aáåäeéêëoôö", wrong_list)
test "check_pass function" do
password = Enum.random(ascii_passwords())
assert check_pass_returns_user(Bcrypt, password)
assert check_pass_returns_error(Bcrypt, password)
assert check_pass_nil_user(Bcrypt)
end

test "hash_pwd_salt legacy prefix" do
Expand Down
33 changes: 0 additions & 33 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,34 +1 @@
ExUnit.start()

defmodule BcryptTestHelper do
use ExUnit.Case

def password_hash_check(password, wrong_list) do
hash = Bcrypt.hash_pwd_salt(password)
assert Bcrypt.verify_pass(password, hash)

for wrong <- wrong_list do
refute Bcrypt.verify_pass(wrong, hash)
end
end

def add_hash_check(password, wrong_list) do
%{password_hash: hash, password: nil} = Bcrypt.add_hash(password)
assert Bcrypt.verify_pass(password, hash)

for wrong <- wrong_list do
refute Bcrypt.verify_pass(wrong, hash)
end
end

def check_pass_check(password, wrong_list) do
hash = Bcrypt.hash_pwd_salt(password)
user = %{id: 2, name: "fred", password_hash: hash}
assert Bcrypt.check_pass(user, password) == {:ok, user}
assert Bcrypt.check_pass(nil, password) == {:error, "invalid user-identifier"}

for wrong <- wrong_list do
assert Bcrypt.check_pass(user, wrong) == {:error, "invalid password"}
end
end
end

0 comments on commit 94e942b

Please sign in to comment.