Skip to content

Commit

Permalink
fix specs and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed Nov 22, 2014
1 parent d474c3d commit 8e5e09c
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 67 deletions.
8 changes: 4 additions & 4 deletions lib/passwd/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ def initialize(options = {})
else
Salt.new(password: self)
end
self.plain = options[:plain]
self.update_plain(options[:plain])
end
end

def plain=(value)
def update_plain(value)
@plain = value
rehash
end

def hash=(value, salt_hash)
def update_hash(value, salt_hash)
@plain = nil
@hash = value
self.salt.hash = salt_hash
self.salt.update_hash(salt_hash)
end

def rehash
Expand Down
6 changes: 3 additions & 3 deletions lib/passwd/salt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def initialize(options = {})
end
end

def plain=(value)
def update_plain(value)
@plain = value
@hash = digest_without_stretching(@plain)
update_password!
end

def hash=(value)
def update_hash(value)
@plain = nil
@hash = value
update_password!
Expand All @@ -43,7 +43,7 @@ def default_options
end

def update_password!
@password.rehash if @password
@password.rehash if @password && @password.plain
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/passwd/active_record_ext_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require "spec_helper"

describe Passwd::ActiveRecordExt do
context ".#with_authenticate" do
it { expect(ActiveRecord::Base.respond_to? :with_authenticate).to be_truthy }
end
context ".with_authenticate" do
it { expect(ActiveRecord::Base).to respond_to(:with_authenticate) }

describe User do
it { is_expected.to respond_to(:passwd) }
it { is_expected.to respond_to(:authenticate) }
it { expect(User).to respond_to(:authenticate) }
it { is_expected.to respond_to(:set_password) }
it { is_expected.to respond_to(:update_password) }
context User do
it { is_expected.to respond_to(:passwd) }
it { is_expected.to respond_to(:authenticate) }
it { expect(User).to respond_to(:authenticate) }
it { is_expected.to respond_to(:set_password) }
it { is_expected.to respond_to(:update_password) }
end
end
end

44 changes: 20 additions & 24 deletions spec/passwd/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,58 @@
require "spec_helper"

describe Passwd::Base do
let(:included) { Class.new {include Passwd::Base}.new }
let(:extended) { Class.new {extend Passwd::Base} }
let(:plain) { "secret" }

context "#random" do
it { expect(included.respond_to?(:random)).to be_truthy }
it { expect(extended.respond_to?(:random)).to be_truthy }
it { expect(included.random.is_a?(String)).to be_truthy }
it { expect(included.random.size).to eq(Passwd::PwConfig.length) }
it { expect(("a".."z").to_a.include? Passwd.random(lower: false)).to be_falsey }
it { expect(("A".."Z").to_a.include? Passwd.random(upper: false)).to be_falsey }
it { expect(("0".."9").to_a.include? Passwd.random(number: false)).to be_falsey }
it { expect(Passwd).to respond_to(:random) }
it { expect(Passwd.random.is_a?(String)).to be_truthy }
it { expect(Passwd.random.size).to eq(Passwd::PwConfig.length) }
it { expect(Passwd.random(lower: false)).not_to include(*"a".."z") }
it { expect(Passwd.random(upper: false)).not_to include(*"A".."Z") }
it { expect(Passwd.random(number: false)).not_to include(*"0".."9") }

it {
length = Passwd::PwConfig.length + 1
expect(included.random(length: length).size).to eq(length)
expect(Passwd.random(length: length).size).to eq(length)
}

it {
lower = ["a"]
expect(
included.random(letters_lower: lower, upper: false, number: false)
Passwd.random(letters_lower: lower, upper: false, number: false)
.chars.uniq
).to eq(lower)
}
end

context "#digest" do
it { expect(included.respond_to?(:digest)).to be_truthy }
it { expect(extended.respond_to?(:digest)).to be_truthy }
it { expect(included.digest(plain).is_a?(String)).to be_truthy }
it { expect(included.digest(plain)).not_to eq(plain) }
it { expect(Passwd.respond_to?(:digest)).to be_truthy }
it { expect(Passwd.digest(plain).is_a?(String)).to be_truthy }
it { expect(Passwd.digest(plain)).not_to eq(plain) }

it {
hashed = included.send(:algorithm, Passwd::PwConfig.algorithm).hexdigest plain
hashed = Passwd.send(:algorithm, Passwd::PwConfig.algorithm).hexdigest plain
expect(Passwd.digest(plain)).to eq(hashed)
}

it {
not_default = :md5
hashed = included.send(:algorithm, not_default).hexdigest plain
hashed = Passwd.send(:algorithm, not_default).hexdigest plain
expect(Passwd.digest(plain, not_default)).to eq(hashed)
}
end

context "#algorithm" do
it { expect(included.send(:algorithm, :sha1)).to eq(Digest::SHA1) }
it { expect(included.send(:algorithm, :sha256)).to eq(Digest::SHA256) }
it { expect(included.send(:algorithm, :sha384)).to eq(Digest::SHA384) }
it { expect(included.send(:algorithm, :sha512)).to eq(Digest::SHA512) }
it { expect(included.send(:algorithm, :md5)).to eq(Digest::MD5) }
it { expect(included.send(:algorithm, :rmd160)).to eq(Digest::RMD160) }
it { expect(Passwd.send(:algorithm, :sha1)).to eq(Digest::SHA1) }
it { expect(Passwd.send(:algorithm, :sha256)).to eq(Digest::SHA256) }
it { expect(Passwd.send(:algorithm, :sha384)).to eq(Digest::SHA384) }
it { expect(Passwd.send(:algorithm, :sha512)).to eq(Digest::SHA512) }
it { expect(Passwd.send(:algorithm, :md5)).to eq(Digest::MD5) }
it { expect(Passwd.send(:algorithm, :rmd160)).to eq(Digest::RMD160) }

it {
expect {
included.send(:algorithm, :unknowAn)
Passwd.send(:algorithm, :unknowAn)
}.to raise_error
}
end
Expand Down
58 changes: 31 additions & 27 deletions spec/passwd/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,49 @@

describe Passwd::Configuration do
describe "#initialize" do
subject(:config) { Passwd::PwConfig }
subject { Passwd::PwConfig }
# defined options
it { expect(config.respond_to? :algorithm).to be_truthy }
it { expect(config.respond_to? :length).to be_truthy }
it { expect(config.respond_to? :policy).to be_truthy }
it { expect(config.respond_to? :stretching).to be_truthy }
it { expect(config.respond_to? :lower).to be_truthy }
it { expect(config.respond_to? :upper).to be_truthy }
it { expect(config.respond_to? :number).to be_truthy }
it { expect(config.respond_to? :letters_lower).to be_truthy }
it { expect(config.respond_to? :letters_upper).to be_truthy }
it { expect(config.respond_to? :letters_number).to be_truthy }
it { is_expected.to respond_to(:algorithm) }
it { is_expected.to respond_to(:length) }
it { is_expected.to respond_to(:policy) }
it { is_expected.to respond_to(:stretching) }
it { is_expected.to respond_to(:lower) }
it { is_expected.to respond_to(:upper) }
it { is_expected.to respond_to(:number) }
it { is_expected.to respond_to(:letters_lower) }
it { is_expected.to respond_to(:letters_upper) }
it { is_expected.to respond_to(:letters_number) }

# default settings
it { expect(config.algorithm).to eq(:sha512) }
it { expect(config.length).to eq(8) }
it { expect(config.policy.is_a?(Passwd::Policy)).to be_truthy }
it { expect(config.stretching).to eq(nil) }
it { expect(config.lower).to be_truthy }
it { expect(config.upper).to be_truthy }
it { expect(config.number).to be_truthy }
it { expect(config.letters_lower).to eq(("a".."z").to_a) }
it { expect(config.letters_upper).to eq(("A".."Z").to_a) }
it { expect(config.letters_number).to eq(("0".."9").to_a) }
it { is_expected.to have_attributes(algorithm: :sha512) }
it { is_expected.to have_attributes(length: 8) }
it { is_expected.to satisfy {|v| v.policy.is_a?(Passwd::Policy) } }
it { is_expected.to have_attributes(stretching: nil) }
it { is_expected.to have_attributes(lower: true) }
it { is_expected.to have_attributes(upper: true) }
it { is_expected.to have_attributes(number: true) }
it { is_expected.to have_attributes(letters_lower: [*"a".."z"]) }
it { is_expected.to have_attributes(letters_upper: [*"A".."Z"]) }
it { is_expected.to have_attributes(letters_number: [*"0".."9"]) }
end

describe "Writable" do
subject { Passwd }

it {
passwd = Class.new {extend Passwd::Configuration::Writable}
expect(defined?(passwd::PwConfig)).to be_truthy
klass = Class.new { extend Passwd::Configuration::Writable }
expect(defined?(klass::PwConfig)).to be_truthy
}

it { expect(Passwd.respond_to?(:configure)).to be_truthy }
it { expect(Passwd.respond_to?(:policy_configure)).to be_truthy }
it { is_expected.to respond_to(:configure) }
it { is_expected.to respond_to(:policy_configure) }
end

describe "Accessible" do
let(:passwd) { Class.new {include Passwd::Configuration::Accessible} }
it { expect(passwd::PwConfig.is_a?(Passwd::Configuration)).to be_truthy }
it {
klass = Class.new { include Passwd::Configuration::Accessible }
expect(klass::PwConfig.is_a?(Passwd::Configuration)).to be_truthy
}
end
end

74 changes: 74 additions & 0 deletions spec/passwd/password_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require "spec_helper"

describe Passwd::Password do
let!(:pswd) { Passwd::Password.new }

describe "#initialize" do
context "without argument" do
subject { Passwd::Password.new }

it { is_expected.not_to have_attributes(plain: nil) }
it { is_expected.not_to have_attributes(hash: nil) }
it { is_expected.not_to have_attributes(salt: nil) }
it { is_expected.to satisfy {|v| v.salt.is_a?(Passwd::Salt) } }
end

context "with plain" do
subject { Passwd::Password.new(plain: pswd.plain) }

it { is_expected.to have_attributes(plain: pswd.plain) }
it { is_expected.not_to have_attributes(hash: nil) }
it { is_expected.not_to have_attributes(salt: nil) }
it { is_expected.to satisfy {|v| v.salt.is_a?(Passwd::Salt) } }
end

context "with plain and salt_plain" do
subject { Passwd::Password.new(plain: pswd.plain, salt_plain: pswd.salt.plain) }

it { is_expected.to have_attributes(plain: pswd.plain) }
it { is_expected.to have_attributes(hash: pswd.hash) }
it { is_expected.to satisfy {|v| v.salt.plain == pswd.salt.plain } }
it { is_expected.to satisfy {|v| v.salt.hash == pswd.salt.hash } }
end

context "with plain and salt_hash" do
subject { Passwd::Password.new(plain: pswd.plain, salt_hash: pswd.salt.hash) }

it { is_expected.to have_attributes(plain: pswd.plain) }
it { is_expected.to have_attributes(hash: pswd.hash) }
it { is_expected.to satisfy {|v| v.salt.plain.nil? } }
it { is_expected.to satisfy {|v| v.salt.hash == pswd.salt.hash } }
end

context "with hash and salt_hash" do
subject { Passwd::Password.new(hash: pswd.hash, salt_hash: pswd.salt.hash) }

it { is_expected.to have_attributes(plain: nil) }
it { is_expected.to have_attributes(hash: pswd.hash) }
it { is_expected.to satisfy {|v| v.salt.plain.nil? } }
it { is_expected.to satisfy {|v| v.salt.hash == pswd.salt.hash } }
end

it {
expect {
Passwd::Password.new(hash: pswd.hash)
}.to raise_error(ArgumentError)
}
end

describe "#update_plain" do
it {
expect(pswd).to receive(:rehash)
pswd.update_plain("secret")
expect(pswd).to have_attributes(plain: "secret")
}
end

describe "#update_hash" do
it {
expect {
pswd.update_hash("hashed", "salt")
}.to change { pswd.plain }
}
end
end

0 comments on commit 8e5e09c

Please sign in to comment.