Skip to content

Commit

Permalink
Require non-empty ID when fetching a resource (#159)
Browse files Browse the repository at this point in the history
Calling `#get` with nil or an empty string returned an empty resource
object which could lead to unexpected results when accessing nested
resources.

For example, if `id` was nil inadvertently, `Mollie::Payment.get(nil).chargebacks`
returned *all* chargebacks instead of those scoped to a specific payment.

Co-authored-by: Rein de Lange <[email protected]>
  • Loading branch information
moiristo authored Dec 1, 2022
1 parent 7a53715 commit 2f88990
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/mollie/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def all(options = {})
end

def get(id, options = {})
raise Mollie::Exception, "Invalid resource ID: #{id.inspect}" if id.nil? || id.strip.empty?

request('GET', id, {}, options) do |response|
new(response)
end
Expand Down
7 changes: 7 additions & 0 deletions test/mollie/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def test_nested_get
assert_equal 'my-id', resource.id
assert_equal 'object-id', resource.testobject_id
end
def test_get_with_invalid_identifiers
assert_raises(Mollie::Exception) { TestObject.get(nil) }
assert_raises(Mollie::Exception) { TestObject.get(" ") }
assert_raises(Mollie::Exception) { TestObject.get(" ") }
assert_raises(Mollie::Exception) { TestObject.get("\t") }
assert_raises(Mollie::Exception) { TestObject.get("\n") }
end

def test_create
stub_request(:post, 'https://api.mollie.com/v2/testobjects')
Expand Down

0 comments on commit 2f88990

Please sign in to comment.