From c535f206507317448b2bf7caf6d927025d72f564 Mon Sep 17 00:00:00 2001 From: Bruno Henrique - Garu Date: Thu, 15 Jan 2015 01:11:00 +0000 Subject: [PATCH 1/3] Bump rspec version to 3.1.0 --- Gemfile.lock | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d020306..9148e17 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,25 +7,25 @@ GEM remote: https://rubygems.org/ specs: diff-lcs (1.2.5) - json (1.8.1) - json (1.8.1-java) - rake (10.1.0) - rake-compiler (0.9.2) + json (1.8.2) + json (1.8.2-java) + rake (10.4.2) + rake-compiler (0.9.5) rake rdoc (3.12.2) json (~> 1.4) - rspec (3.0.0) - rspec-core (~> 3.0.0) - rspec-expectations (~> 3.0.0) - rspec-mocks (~> 3.0.0) - rspec-core (3.0.2) - rspec-support (~> 3.0.0) - rspec-expectations (3.0.2) + rspec (3.1.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) + rspec-expectations (3.1.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.0.0) - rspec-mocks (3.0.2) - rspec-support (~> 3.0.0) - rspec-support (3.0.2) + rspec-support (~> 3.1.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) + rspec-support (3.1.2) PLATFORMS java From 4196402e28ce17caeee3ddcf904c6fa92227cdf4 Mon Sep 17 00:00:00 2001 From: Bruno Henrique - Garu Date: Thu, 15 Jan 2015 01:23:18 +0000 Subject: [PATCH 2/3] Changes specs to use 'let' instead of 'before' hook For more informations: - http://betterspecs.org/#let - https://stackoverflow.com/questions/5359558/when-to-use-rspec-let/5359979#5359979 --- spec/bcrypt/engine_spec.rb | 22 ++++++++++------------ spec/bcrypt/password_spec.rb | 34 ++++++++++++++-------------------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/spec/bcrypt/engine_spec.rb b/spec/bcrypt/engine_spec.rb index 11ec907..cfe322b 100644 --- a/spec/bcrypt/engine_spec.rb +++ b/spec/bcrypt/engine_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) -describe "The BCrypt engine" do +RSpec.describe "The BCrypt engine" do specify "should calculate the optimal cost factor to fit in a specific time" do first = BCrypt::Engine.calibrate(100) second = BCrypt::Engine.calibrate(400) @@ -15,7 +15,7 @@ end specify "should produce random data" do - expect(BCrypt::Engine.generate_salt).to_not equal(BCrypt::Engine.generate_salt) + expect(BCrypt::Engine.generate_salt).not_to equal(BCrypt::Engine.generate_salt) end specify "should raise a InvalidCostError if the cost parameter isn't numeric" do @@ -43,27 +43,25 @@ class MyInvalidSecret undef to_s end - before :each do - @salt = BCrypt::Engine.generate_salt(4) - @password = "woo" - end + let(:salt) { BCrypt::Engine.generate_salt(4) } + let(:password) { "woo" } specify "should produce a string" do - expect(BCrypt::Engine.hash_secret(@password, @salt)).to be_an_instance_of(String) + expect(BCrypt::Engine.hash_secret(password, salt)).to be_an_instance_of(String) end specify "should raise an InvalidSalt error if the salt is invalid" do - expect { BCrypt::Engine.hash_secret(@password, 'nino') }.to raise_error(BCrypt::Errors::InvalidSalt) + expect { BCrypt::Engine.hash_secret(password, 'nino') }.to raise_error(BCrypt::Errors::InvalidSalt) end specify "should raise an InvalidSecret error if the secret is invalid" do - expect { BCrypt::Engine.hash_secret(MyInvalidSecret.new, @salt) }.to raise_error(BCrypt::Errors::InvalidSecret) - expect { BCrypt::Engine.hash_secret(nil, @salt) }.not_to raise_error - expect { BCrypt::Engine.hash_secret(false, @salt) }.not_to raise_error + expect { BCrypt::Engine.hash_secret(MyInvalidSecret.new, salt) }.to raise_error(BCrypt::Errors::InvalidSecret) + expect { BCrypt::Engine.hash_secret(nil, salt) }.not_to raise_error + expect { BCrypt::Engine.hash_secret(false, salt) }.not_to raise_error end specify "should call #to_s on the secret and use the return value as the actual secret data" do - expect(BCrypt::Engine.hash_secret(false, @salt)).to eq BCrypt::Engine.hash_secret("false", @salt) + expect(BCrypt::Engine.hash_secret(false, salt)).to eq BCrypt::Engine.hash_secret("false", salt) end specify "should be interoperable with other implementations" do diff --git a/spec/bcrypt/password_spec.rb b/spec/bcrypt/password_spec.rb index 648e614..d090115 100644 --- a/spec/bcrypt/password_spec.rb +++ b/spec/bcrypt/password_spec.rb @@ -1,18 +1,16 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) -describe "Creating a hashed password" do +RSpec.describe "Creating a hashed password" do - before :each do - @secret = "wheedle" - @password = BCrypt::Password.create(@secret, :cost => 4) - end + let(:secret) { "wheedle" } + let(:password) { BCrypt::Password.create(secret, :cost => 4) } specify "should return a BCrypt::Password" do - expect(@password).to be_an_instance_of(BCrypt::Password) + expect(password).to be_an_instance_of(BCrypt::Password) end specify "should return a valid bcrypt password" do - expect { BCrypt::Password.new(@password) }.not_to raise_error + expect { BCrypt::Password.new(password) }.not_to raise_error end specify "should behave normally if the secret is not a string" do @@ -29,10 +27,8 @@ end describe "Reading a hashed password" do - before :each do - @secret = "U*U" - @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW" - end + + let(:hash) { "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW" } specify "the cost is too damn high" do expect { @@ -73,7 +69,7 @@ end specify "should read the version, cost, salt, and hash" do - password = BCrypt::Password.new(@hash) + password = BCrypt::Password.new(hash) expect(password.version).to eql("2a") expect(password.version.class).to eq String expect(password.cost).to equal(5) @@ -81,7 +77,7 @@ expect(password.salt.class).to eq String expect(password.checksum).to eq("E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW") expect(password.checksum.class).to eq String - expect(password.to_s).to eql(@hash) + expect(password.to_s).to eql(hash) end specify "should raise an InvalidHashError when given an invalid hash" do @@ -90,18 +86,16 @@ end describe "Comparing a hashed password with a secret" do - before :each do - @secret = "U*U" - @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW" - @password = BCrypt::Password.create(@secret) - end + let(:secret) {"U*U"} + let(:hash) { "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW" } + let(:password) { BCrypt::Password.create(secret) } specify "should compare successfully to the original secret" do - expect((@password == @secret)).to be(true) + expect((password == secret)).to be(true) end specify "should compare unsuccessfully to anything besides original secret" do - expect((@password == "@secret")).to be(false) + expect((password == "secret")).to be(false) end end From 73aa29797b84f74b7f28be757318031673ec2f0e Mon Sep 17 00:00:00 2001 From: Bruno Henrique - Garu Date: Thu, 15 Jan 2015 01:48:41 +0000 Subject: [PATCH 3/3] Improves code consistency --- spec/bcrypt/engine_spec.rb | 12 ++++++------ spec/bcrypt/password_spec.rb | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/spec/bcrypt/engine_spec.rb b/spec/bcrypt/engine_spec.rb index cfe322b..c37fa30 100644 --- a/spec/bcrypt/engine_spec.rb +++ b/spec/bcrypt/engine_spec.rb @@ -15,7 +15,7 @@ end specify "should produce random data" do - expect(BCrypt::Engine.generate_salt).not_to equal(BCrypt::Engine.generate_salt) + expect(BCrypt::Engine.generate_salt).not_to eq(BCrypt::Engine.generate_salt) end specify "should raise a InvalidCostError if the cost parameter isn't numeric" do @@ -30,9 +30,9 @@ describe "Autodetecting of salt cost" do specify "should work" do - expect(BCrypt::Engine.autodetect_cost("$2a$08$hRx2IVeHNsTSYYtUWn61Ou")).to eq 8 - expect(BCrypt::Engine.autodetect_cost("$2a$05$XKd1bMnLgUnc87qvbAaCUu")).to eq 5 - expect(BCrypt::Engine.autodetect_cost("$2a$13$Lni.CZ6z5A7344POTFBBV.")).to eq 13 + expect(BCrypt::Engine.autodetect_cost("$2a$08$hRx2IVeHNsTSYYtUWn61Ou")).to eq(8) + expect(BCrypt::Engine.autodetect_cost("$2a$05$XKd1bMnLgUnc87qvbAaCUu")).to eq(5) + expect(BCrypt::Engine.autodetect_cost("$2a$13$Lni.CZ6z5A7344POTFBBV.")).to eq(13) end end @@ -61,7 +61,7 @@ class MyInvalidSecret end specify "should call #to_s on the secret and use the return value as the actual secret data" do - expect(BCrypt::Engine.hash_secret(false, salt)).to eq BCrypt::Engine.hash_secret("false", salt) + expect(BCrypt::Engine.hash_secret(false, salt)).to eq(BCrypt::Engine.hash_secret("false", salt)) end specify "should be interoperable with other implementations" do @@ -74,7 +74,7 @@ class MyInvalidSecret ["0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "$2a$05$abcdefghijklmnopqrstuu", "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui"] ] for secret, salt, test_vector in test_vectors - expect(BCrypt::Engine.hash_secret(secret, salt)).to eql(test_vector) + expect(BCrypt::Engine.hash_secret(secret, salt)).to eq(test_vector) end end end diff --git a/spec/bcrypt/password_spec.rb b/spec/bcrypt/password_spec.rb index d090115..373e924 100644 --- a/spec/bcrypt/password_spec.rb +++ b/spec/bcrypt/password_spec.rb @@ -37,20 +37,20 @@ end specify "the cost should be set to the default if nil" do - expect(BCrypt::Password.create("hello", :cost => nil).cost).to equal(BCrypt::Engine::DEFAULT_COST) + expect(BCrypt::Password.create("hello", :cost => nil).cost).to eq(BCrypt::Engine::DEFAULT_COST) end specify "the cost should be set to the default if empty hash" do - expect(BCrypt::Password.create("hello", {}).cost).to equal(BCrypt::Engine::DEFAULT_COST) + expect(BCrypt::Password.create("hello", {}).cost).to eq(BCrypt::Engine::DEFAULT_COST) end specify "the cost should be set to the passed value if provided" do - expect(BCrypt::Password.create("hello", :cost => 5).cost).to equal(5) + expect(BCrypt::Password.create("hello", :cost => 5).cost).to eq(5) end specify "the cost should be set to the global value if set" do BCrypt::Engine.cost = 5 - expect(BCrypt::Password.create("hello").cost).to equal(5) + expect(BCrypt::Password.create("hello").cost).to eq(5) # unset the global value to not affect other tests BCrypt::Engine.cost = nil end @@ -61,7 +61,7 @@ old_default_cost = BCrypt::Engine::DEFAULT_COST BCrypt::Engine::DEFAULT_COST = 5 - expect(BCrypt::Password.create("hello").cost).to equal(5) + expect(BCrypt::Password.create("hello").cost).to eq(5) # reset default to not affect other tests BCrypt::Engine::DEFAULT_COST = old_default_cost @@ -70,14 +70,14 @@ specify "should read the version, cost, salt, and hash" do password = BCrypt::Password.new(hash) - expect(password.version).to eql("2a") - expect(password.version.class).to eq String - expect(password.cost).to equal(5) - expect(password.salt).to eql("$2a$05$CCCCCCCCCCCCCCCCCCCCC.") - expect(password.salt.class).to eq String + expect(password.version).to eq("2a") + expect(password.version.class).to eq(String) + expect(password.cost).to eq(5) + expect(password.salt).to eq("$2a$05$CCCCCCCCCCCCCCCCCCCCC.") + expect(password.salt.class).to eq(String) expect(password.checksum).to eq("E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW") - expect(password.checksum.class).to eq String - expect(password.to_s).to eql(hash) + expect(password.checksum.class).to eq(String) + expect(password.to_s).to eq(hash) end specify "should raise an InvalidHashError when given an invalid hash" do