Skip to content

Commit

Permalink
fix specs (RSpec 2 => 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed Sep 1, 2014
1 parent 69fb858 commit bfec7d5
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "rspec/core/rake_task"

desc "Run all specs"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = ["-c", "-fs"]
t.rspec_opts = %w(--color --format documentation)
end

task :default => :spec
67 changes: 32 additions & 35 deletions spec/passwd/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class User

describe ".#included" do
it "define singleton methods" do
expect(User.respond_to? :define_column).to be_true
expect(User.respond_to? :define_column).to be_truthy
end
end

Expand All @@ -21,19 +21,19 @@ class User
let(:user) {User.new}

it "define singleton methods" do
expect(User.respond_to? :authenticate).to be_true
expect(User.respond_to? :authenticate).to be_truthy
end

it "define authenticate method" do
expect(user.respond_to? :authenticate).to be_true
expect(user.respond_to? :authenticate).to be_truthy
end

it "define set_password method" do
expect(user.respond_to? :set_password).to be_true
expect(user.respond_to? :set_password).to be_truthy
end

it "define update_password" do
expect(user.respond_to? :update_password).to be_true
expect(user.respond_to? :update_password).to be_truthy
end
end
end
Expand All @@ -42,98 +42,96 @@ class User
describe ".#authenticate" do
let!(:record) {
record = double("record mock")
record.stub(:salt).and_return(salt)
record.stub(:password).and_return(password_hash)
allow(record).to receive_messages(salt: salt, password: password_hash)
response = [record]
User.stub(:where).and_return(response)
allow(User).to receive(:where) {response}
record
}

it "user should be returned if authentication is successful" do
User.should_receive(:where)
expect(User).to receive(:where)
expect(User.authenticate("valid_id", password_text)).to eq(record)
end

it "should return nil if authentication failed" do
User.should_receive(:where)
expect(User).to receive(:where)
expect(User.authenticate("valid_id", "invalid_secret")).to be_nil
end

it "should return nil if user not found" do
User.should_receive(:where).with(:email => "invalid_id").and_return([])
expect(User).to receive(:where).with(email: "invalid_id") {[]}
expect(User.authenticate("invalid_id", password_text)).to be_nil
end
end

describe "#authenticate" do
let!(:user) {
user = User.new
user.stub(:salt).and_return(salt)
user.stub(:password).and_return(password_hash)
allow(user).to receive_messages(salt: salt, password: password_hash)
user
}

it "should return true if authentication is successful" do
expect(user.authenticate(password_text)).to be_true
expect(user.authenticate(password_text)).to be_truthy
end

it "should return false if authentication failed" do
expect(user.authenticate("invalid_pass")).to be_false
expect(user.authenticate("invalid_pass")).to be_falsey
end
end

describe "#set_password" do
let!(:user) {
user = User.new
user.stub(:salt).and_return(salt)
allow(user).to receive(:salt) {salt}
user
}

it "should return set password" do
user.should_receive(:salt=).with(salt)
user.should_receive(:password=).with(Passwd.hashing("#{salt}#{password_text}"))
expect(user).to receive(:salt=).with(salt)
expect(user).to receive(:password=).with(Passwd.hashing("#{salt}#{password_text}"))
expect(user.set_password(password_text)).to eq(password_text)
end

it "should set random password if not specified" do
user.should_receive(:salt=).with(salt)
expect(user).to receive(:salt=).with(salt)
random_password = Passwd.create
Passwd.should_receive(:create).and_return(random_password)
user.should_receive(:password=).with(Passwd.hashing("#{salt}#{random_password}"))
expect(Passwd).to receive(:create) {random_password}
expect(user).to receive(:password=).with(Passwd.hashing("#{salt}#{random_password}"))
user.set_password
end

it "should set salt if salt is nil" do
mail_addr = "foo@example.com"
time_now = Time.now
salt2 = Passwd.hashing("#{mail_addr}#{time_now.to_s}")
Time.stub(:now).and_return(time_now)
user.stub(:email).and_return(mail_addr)
user.should_receive(:salt).and_return(nil)
user.should_receive(:salt=).with(salt2)
user.should_receive(:password=).with(Passwd.hashing("#{salt2}#{password_text}"))
allow(Time).to receive(:now) {time_now}
allow(user).to receive(:email) {mail_addr}
expect(user).to receive(:salt) {nil}
expect(user).to receive(:salt=).with(salt2)
expect(user).to receive(:password=).with(Passwd.hashing("#{salt2}#{password_text}"))
user.set_password(password_text)
end
end

describe "#update_password" do
let!(:user) {
user = User.new
user.stub(:salt).and_return(salt)
user.stub(:password).and_return(password_hash)
allow(user).to receive(:salt) {salt}
allow(user).to receive(:password) {password_hash}
user
}

context "without policy check" do
it "should return update password" do
pass = "new_password"
user.should_receive(:set_password).with(pass).and_return(pass)
expect(user).to receive(:set_password).with(pass) {pass}
expect(user.update_password(password_text, pass)).to eq(pass)
end

it "should generate exception if authentication failed" do
Passwd.should_receive(:auth).and_return(false)
user.should_not_receive(:set_password)
expect(Passwd).to receive(:auth) {false}
expect(user).not_to receive(:set_password)
expect {
user.update_password("invalid_password", "new_password")
}.to raise_error(Passwd::AuthError)
Expand All @@ -143,14 +141,14 @@ class User
context "with policy check" do
it "should return update password" do
pass = "new_password"
Passwd.should_receive(:policy_check).and_return(true)
user.should_receive(:set_password).with(pass).and_return(pass)
expect(Passwd).to receive(:policy_check) {true}
expect(user).to receive(:set_password).with(pass) {pass}
expect(user.update_password(password_text, pass, true)).to eq(pass)
end

it "should generate exception if policy not match" do
pass = "new_password"
Passwd.should_receive(:policy_check).and_return(false)
expect(Passwd).to receive(:policy_check) {false}
expect {
user.update_password(password_text, pass, true)
}.to raise_error(Passwd::PolicyNotMatch)
Expand All @@ -159,4 +157,3 @@ class User
end
end
end

49 changes: 24 additions & 25 deletions spec/passwd/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
let(:password) {Passwd.create}

it "TmpConfig should not be generated" do
Passwd::TmpConfig.should_not_receive(:new)
expect(Passwd::TmpConfig).not_to receive(:new)
expect{password}.not_to raise_error
end

it "created password should be String object" do
expect(password.is_a? String).to be_true
expect(password.is_a? String).to be_truthy
end

it "created password length should be default length" do
Expand All @@ -23,7 +23,7 @@
context "with arguments" do
it "TmpConfig should be generated" do
tmp_config = double("tmp_config mock", length: 8, letters: ["a", "b"])
Passwd::TmpConfig.should_receive(:new).and_return(tmp_config)
expect(Passwd::TmpConfig).to receive(:new) {tmp_config}
expect{Passwd.create(length: 10)}.not_to raise_error
end

Expand All @@ -32,15 +32,15 @@
end

it "password create without lower case" do
expect(("a".."z").to_a.include? Passwd.create(lower: false)).to be_false
expect(("a".."z").to_a.include? Passwd.create(lower: false)).to be_falsey
end

it "password create without upper case" do
expect(("A".."Z").to_a.include? Passwd.create(upper: false)).to be_false
expect(("A".."Z").to_a.include? Passwd.create(upper: false)).to be_falsey
end

it "password create without number" do
expect(("0".."9").to_a.include? Passwd.create(number: false)).to be_false
expect(("0".."9").to_a.include? Passwd.create(number: false)).to be_falsey
end
end
end
Expand All @@ -54,11 +54,11 @@
end

it "return true with valid password" do
expect(Passwd.auth(password[:text], password[:salt], password[:hash])).to be_true
expect(Passwd.auth(password[:text], password[:salt], password[:hash])).to be_truthy
end

it "return false with invalid password" do
expect(Passwd.auth("invalid", password[:salt], password[:hash])).to be_false
expect(Passwd.auth("invalid", password[:salt], password[:hash])).to be_falsey
end

it "should create exception if not specified arguments" do
Expand All @@ -68,7 +68,7 @@

describe "#hashing" do
it "should call SHA512.#hexdigest" do
Digest::SHA512.should_receive(:hexdigest)
expect(Digest::SHA512).to receive(:hexdigest)
Passwd.hashing("secret")
end

Expand Down Expand Up @@ -96,24 +96,24 @@
end

it "return true if password matches" do
expect(Passwd.confirm_check("secret", "secret")).to be_true
expect(Passwd.confirm_check("secret", "secret")).to be_truthy
end
end

context "with policy check" do
it "return false if invalid password by policy" do
expect(Passwd.confirm_check("secret", "secret", true)).to be_false
expect(Passwd.confirm_check("secret", "secret", true)).to be_falsey
end

it "return true if valid password by policy" do
expect(Passwd.confirm_check("secretpass", "secretpass", true)).to be_false
expect(Passwd.confirm_check("secretpass", "secretpass", true)).to be_falsey
end
end
end

describe "#configure" do
it "return configuration object" do
expect(Passwd.configure.is_a? Passwd::Config).to be_true
expect(Passwd.configure.is_a? Passwd::Config).to be_truthy
end

it "set config value from block" do
Expand All @@ -137,7 +137,7 @@

describe "#policy_configure" do
it "return policy object" do
expect(Passwd.policy_configure.is_a? Passwd::Policy).to be_true
expect(Passwd.policy_configure.is_a? Passwd::Policy).to be_truthy
end

it "set policy value from block" do
Expand All @@ -151,14 +151,14 @@

describe "#policy_check" do
it "Policy#valid? should be called" do
Passwd::Policy.instance.should_receive(:valid?).with("secret1234" ,Passwd::Config.instance)
expect(Passwd.policy_check("secret1234")).not_to raise_error
expect(Passwd::Policy.instance).to receive(:valid?).with("secret1234" ,Passwd::Config.instance)
Passwd.policy_check("secret1234")
end
end

describe "#reset_policy" do
let(:policy) {Passwd::Policy.instance}

before {
policy.configure do |c|
c.min_length = 20
Expand All @@ -174,21 +174,21 @@
end

it "require_lower should be a default" do
expect(policy.require_lower).to be_true
expect(policy.require_lower).to be_truthy
end

it "upper should be a default" do
expect(policy.require_upper).to be_false
expect(policy.require_upper).to be_falsey
end

it "number should be a default" do
expect(policy.require_number).to be_true
expect(policy.require_number).to be_truthy
end
end

describe "#reset_config" do
let(:config) {Passwd::Config.instance}

before {
config.configure do |c|
c.length = 20
Expand All @@ -207,15 +207,15 @@
end

it "lower should be a default" do
expect(config.lower).to be_true
expect(config.lower).to be_truthy
end

it "upper should be a default" do
expect(config.upper).to be_true
expect(config.upper).to be_truthy
end

it "number should be a default" do
expect(config.number).to be_true
expect(config.number).to be_truthy
end

it "letters_lower should be a default" do
Expand All @@ -232,4 +232,3 @@
end
end
end

0 comments on commit bfec7d5

Please sign in to comment.