Skip to content

Commit 3575d9d

Browse files
committed
Adds some tests for hashed passwords, upcases hashes on initialization
1 parent e5ab8ea commit 3575d9d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/pwned/hashed_password.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class HashedPassword
2626
# @since 1.1.0
2727
def initialize(hashed_password, request_options={})
2828
raise TypeError, "hashed_password must be of type String" unless hashed_password.is_a? String
29-
@hashed_password = hashed_password
29+
@hashed_password = hashed_password.upcase
3030
@request_options = Hash(request_options).dup
3131
@request_headers = Hash(request_options.delete(:headers))
3232
@request_headers = DEFAULT_REQUEST_HEADERS.merge(@request_headers)

spec/pwned/hashed_password_spec.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
RSpec.describe Pwned::HashedPassword do
2-
subject(:hashed_password) { Pwned::HashedPassword.new(password_hash) }
3-
2+
let(:hashed_password) { Pwned::HashedPassword.new(password_hash) }
43
let(:password) { "password" }
54
let(:password_hash) { Pwned.hash_password(password) }
65

@@ -31,4 +30,30 @@
3130
expect { hashed_password }.to raise_error(TypeError)
3231
end
3332
end
33+
34+
describe "when pwned", pwned_range: "5BAA6" do
35+
it "reports it is pwned" do
36+
expect(hashed_password.pwned?).to be true
37+
expect(@stub).to have_been_requested
38+
end
39+
40+
it "reports it has been pwned many times" do
41+
expect(hashed_password.pwned_count).to eq(3303003)
42+
expect(@stub).to have_been_requested
43+
end
44+
45+
describe "when given a lower case hash" do
46+
let(:hashed_password) { Pwned::HashedPassword.new(password_hash) }
47+
let(:password_hash) { Pwned.hash_password(password).downcase }
48+
49+
it "upcases the hashed password" do
50+
expect(hashed_password.hashed_password).to eq("5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8")
51+
end
52+
53+
it "reports it is pwned" do
54+
expect(hashed_password.pwned?).to be true
55+
expect(@stub).to have_been_requested
56+
end
57+
end
58+
end
3459
end

0 commit comments

Comments
 (0)