-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:philnash/pwned
- Loading branch information
Showing
9 changed files
with
145 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module DeepMerge | ||
refine Hash do | ||
def deep_merge(other) | ||
self.merge(other) do |key, this_val, other_val| | ||
if this_val.is_a?(Hash) && other_val.is_a?(Hash) | ||
this_val.deep_merge(other_val) | ||
else | ||
other_val | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,9 @@ def create_model(password) | |
end | ||
|
||
it "allows the user agent to be set" do | ||
# Default option should be overridden | ||
Pwned.default_request_options = { headers: { "User-Agent" => "Default user agent" } } | ||
|
||
Model.validates :password, not_pwned: { | ||
request_options: { headers: { "User-Agent" => "Super fun user agent" } } | ||
} | ||
|
@@ -44,7 +47,10 @@ def create_model(password) | |
to have_been_made.once | ||
end | ||
|
||
it "allows the proxy to be set" do | ||
it "allows the proxy to be set via options" do | ||
# Default option should be overridden | ||
Pwned.default_request_options = { proxy: "https://username:[email protected]:12345" } | ||
|
||
Model.validates :password, not_pwned: { | ||
request_options: { proxy: "https://username:[email protected]:12345" } | ||
} | ||
|
@@ -61,6 +67,23 @@ def create_model(password) | |
with(headers: { "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" })). | ||
to have_been_made.once | ||
end | ||
|
||
it "allows the proxy to be set via default options" do | ||
Pwned.default_request_options = { proxy: "https://username:[email protected]:12345" } | ||
Model.validates :password, not_pwned: true | ||
model = create_model("password") | ||
|
||
# Webmock doesn't support proxy assertions (https://github.com/bblimke/webmock/issues/753) | ||
# so we check that Net::HTTP receives the correct arguments. | ||
expect(Net::HTTP).to receive(:start). | ||
with("api.pwnedpasswords.com", 443, "default.com", 12345, "username", "password", anything). | ||
and_call_original | ||
|
||
expect(model).to_not be_valid | ||
expect(a_request(:get, "https://api.pwnedpasswords.com/range/5BAA6"). | ||
with(headers: { "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" })). | ||
to have_been_made.once | ||
end | ||
end | ||
|
||
describe "when not pwned", pwned_range: "37D5B" do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,8 @@ def verify_not_found_error(error) | |
to have_been_made.once | ||
end | ||
|
||
it "allows the user agent to be set" do | ||
it "allows the user agent to be set in constructor" do | ||
Pwned.default_request_options = { headers: { "User-Agent" => "Default user agent" } } | ||
password = Pwned::Password.new("password", headers: { "User-Agent" => "Super fun user agent" }) | ||
password.pwned? | ||
|
||
|
@@ -149,6 +150,26 @@ def verify_not_found_error(error) | |
to have_been_made.once | ||
end | ||
|
||
it "allows the user agent to be set with default settings" do | ||
Pwned.default_request_options = { headers: { "User-Agent" => "Default user agent" } } | ||
password = Pwned::Password.new("password") | ||
password.pwned? | ||
|
||
expect(a_request(:get, "https://api.pwnedpasswords.com/range/5BAA6"). | ||
with(headers: { "User-Agent" => "Default user agent" })). | ||
to have_been_made.once | ||
end | ||
|
||
it "allows headers to be set by default or in the constructor and merges them" do | ||
Pwned.default_request_options = { headers: { "User-Agent" => "Default user agent" } } | ||
password = Pwned::Password.new("password", headers: { "X-Test-Header" => "this-is-a-test" }) | ||
password.pwned? | ||
|
||
expect(a_request(:get, "https://api.pwnedpasswords.com/range/5BAA6"). | ||
with(headers: { "User-Agent" => "Default user agent", "X-Test-Header" => "this-is-a-test" })). | ||
to have_been_made.once | ||
end | ||
|
||
let(:subject) { Pwned::Password.new("password", request_options).pwned? } | ||
|
||
let(:request_options) { {} } | ||
|
@@ -274,6 +295,28 @@ def verify_not_found_error(error) | |
include_examples "doesn't use proxy from environment" | ||
end | ||
end | ||
|
||
context "proxy given in default request options" do | ||
before { Pwned.default_request_options = { proxy: "https://username:[email protected]:12345" } } | ||
|
||
it "uses proxy from the default require options" do | ||
expect(Net::HTTP).to receive(:start).and_wrap_original do |original_method, *args, &block| | ||
http = original_method.call(*args) | ||
expect(http.proxy_from_env?).to eq(false) | ||
expect(http.proxy_address).to eq("default.com") | ||
expect(http.proxy_user).to eq("username") | ||
expect(http.proxy_pass).to eq("password") | ||
expect(http.proxy_port).to eq(12_345) | ||
original_method.call(*args, &block) | ||
end | ||
|
||
subject | ||
|
||
expect(a_request(:get, "https://api.pwnedpasswords.com/range/5BAA6") | ||
.with(headers: { "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" })) | ||
.to have_been_made.once | ||
end | ||
end | ||
end | ||
|
||
describe "streaming", pwned_range: "A0F41" do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters