forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request stripe-ruby-mock#897 from stripe-ruby-mock/init-v4
First bunch of changes for v4
- Loading branch information
Showing
25 changed files
with
680 additions
and
43 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
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
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
67 changes: 67 additions & 0 deletions
67
lib/stripe_mock/request_handlers/helpers/search_helpers.rb
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,67 @@ | ||
module StripeMock | ||
module RequestHandlers | ||
module Helpers | ||
# Only supports exact matches on a single field, e.g. | ||
# - 'amount:100' | ||
# - 'email:"[email protected]"' | ||
# - 'name:"Foo Bar"' | ||
# - 'metadata["foo"]:"bar"' | ||
QUERYSTRING_PATTERN = /\A(?<field>[\w\.]+)(\[['"](?<metadata_key>[^'"]*)['"]\])?:['"]?(?<value>[^'"]*)['"]?\z/ | ||
def search_results(all_values, querystring, fields: [], resource_name:) | ||
values = all_values.dup | ||
query_match = QUERYSTRING_PATTERN.match(querystring) | ||
raise Stripe::InvalidRequestError.new( | ||
'We were unable to parse your search query.' \ | ||
' Try using the format `metadata["key"]:"value"` to query for metadata or key:"value" to query for other fields.', | ||
nil, | ||
http_status: 400, | ||
) unless query_match | ||
|
||
case query_match[:field] | ||
when *fields | ||
values = values.select { |resource| | ||
exact_match?(actual: field_value(resource, field: query_match[:field]), expected: query_match[:value]) | ||
} | ||
when "metadata" | ||
values = values.select { |resource| | ||
resource[:metadata] && | ||
exact_match?(actual: resource[:metadata][query_match[:metadata_key].to_sym], expected: query_match[:value]) | ||
} | ||
else | ||
raise Stripe::InvalidRequestError.new( | ||
"Field `#{query_match[:field]}` is an unsupported search field for resource `#{resource_name}`." \ | ||
" See http://stripe.com/docs/search#query-fields-for-#{resource_name.gsub('_', '-')} for a list of supported fields.", | ||
nil, | ||
http_status: 400, | ||
) | ||
end | ||
|
||
values | ||
end | ||
|
||
def exact_match?(actual:, expected:) | ||
# allow comparisons of integers | ||
if actual.respond_to?(:to_i) && actual.to_i == actual | ||
expected = expected.to_i | ||
end | ||
# allow comparisons of boolean | ||
case expected | ||
when "true" | ||
expected = true | ||
when "false" | ||
expected = false | ||
end | ||
|
||
actual == expected | ||
end | ||
|
||
def field_value(resource, field:) | ||
value = resource | ||
field.split('.').each do |segment| | ||
value = value[segment.to_sym] | ||
end | ||
value | ||
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
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 |
---|---|---|
|
@@ -28,20 +28,19 @@ | |
expect(bank_token).to match /^test_btok/ | ||
end | ||
|
||
it "assigns the generated bank account to a new recipient" do | ||
it "assigns the generated bank account to a new customer" do | ||
bank_token = StripeMock.generate_bank_token( | ||
:bank_name => "Bank Token Mocking", | ||
:last4 => "7777" | ||
) | ||
|
||
recipient = Stripe::Recipient.create({ | ||
customer = Stripe::Customer.create({ | ||
name: "Fred Flinstone", | ||
type: "individual", | ||
email: '[email protected]', | ||
bank_account: bank_token | ||
source: bank_token | ||
}) | ||
expect(recipient.active_account.last4).to eq("7777") | ||
expect(recipient.active_account.bank_name).to eq("Bank Token Mocking") | ||
expect(customer.sources.first.last4).to eq("7777") | ||
expect(customer.sources.first.bank_name).to eq("Bank Token Mocking") | ||
end | ||
|
||
it "retrieves a created token" do | ||
|
@@ -55,5 +54,4 @@ | |
expect(token.bank_account.last4).to eq("3939") | ||
expect(token.bank_account.bank_name).to eq("Cha-ching Banking") | ||
end | ||
|
||
end |
Oops, something went wrong.