550 Commits

Author SHA1 Message Date
Brandur
b4e64969cc Don't persist idempotency_key option between API requests
Excludes `idempotency_key` from opts to persist between API requests.
Obviously the same idempotency key is not something that we ever want to
use again.

Fixes #598.
2017-10-16 13:00:32 -07:00
Brandur
e4af9ad820 Merge pull request #596 from stripe/ob-fix-554
Encode arrays as hashes when needed
2017-10-16 12:03:59 -07:00
Brandur
6a87b9ce68 Fix wording in test case name 2017-10-16 12:00:48 -07:00
Brandur
cfa6c2b8ae Merge pull request #595 from stripe/ob-fix-524
Call Object#method if method accessor is called with arguments
2017-10-16 12:01:07 -07:00
Olivier Bellone
7f866aab5c
Nested resource class methods 2017-10-15 18:49:13 +02:00
Olivier Bellone
4406f8e258
Call Object#method if method accessor is called with arguments 2017-10-14 23:00:40 +02:00
Olivier Bellone
de21302f4e
Encode arrays as hashes when needed 2017-10-14 17:48:20 +02:00
Olivier Bellone
c920a7c649
Maintain class in deep_copy 2017-10-14 01:05:06 +02:00
Brandur
91099f9ee5 Implement custom Marshal encoder/decoder for StripeObject
Backtracks a little bit #586 by bringing back custom `StripeObject`
encoding and decoding methods for Ruby's `Marshal`. These work by just
persisting values and some opts, and skipping everything else. It's
mostly the same as what we had before, but implemented a little more
cleanly so that we don't actually need to invoke `Marshal` anywhere
ourselves.

In #586 we still managed to remove all the uses of `Marshal` in our own
codebase to make the linter happy. Even though we wouldn't recommend the
use of `Marshal`, this code at least enables it for anyone using a Rails
cache or similar mechanism.

Addresses #90.
2017-10-13 09:31:08 -07:00
Olivier Bellone
a210c5cd76
Ensure that each thread has its own client 2017-10-12 18:20:13 +02:00
Olivier Bellone
92e216634c
detach method for detaching sources from customers 2017-10-11 11:15:38 +02:00
Brandur
8ea866616b Log query string as well as body on log debug
This patch modifies the debugging-level logging logic slightly so that
if it's a `GET` request that includes a query string, we log that string
just like we would've for a request body on a `POST` or like.

This especially comes in handy when looking when trying to resolve
something like a problem with the upcoming invoices endpoint like we saw
in #576, but will be useful in a number of situations.
2017-10-04 13:00:35 -07:00
Brandur
3f454495bf Merge pull request #586 from stripe/brandur-remove-marshal
Implement deep copy for StripeObject and remove marshal/unmarshal
2017-09-29 07:13:32 -07:00
Brandur
80d85a522c Implement deep copy for StripeObject and remove marshal/unmarshal
We were previously using a bit of a hack to get a free deep copy
implementation through Ruby's marshaling framework. Lint call this out
as a security problem though, and rightfully so: when combined with
unsanitized user input, unmarshaling can result in very serious security
breaches involving arbitrary code execution.

This patch removes all uses of marshal/unmarshal in favor of
implementing a deep copy method for `StripeObject`. I also reworked some
of the constants around what keys are available for `opts`. I'm still
not completely happy with the results, but I think it's going to need a
slightly larger refactor in order to get somewhere truly good.

There is what could be a breaking change for people doing non-standard
stuff with the library: the opts that we copy with an object are now
whitelisted, so if they were being used to pass around extraneous data,
that might not work as expected anymore. But because this is a contract
that we never committed to, I don't think I'd bump the major version for
change.
2017-09-28 11:02:20 -07:00
Brandur
cb198baaa3 Remove Rubocop TODO around guard clauses
Removes Rubocop TODO around guard clauses and fixes the outstanding
offenses.

This is starting to get into territory that feels of more dubious value
to me, but at least it did get me writing a couple more tests, so let's
see how it goes by keeping this on.
2017-09-28 09:32:44 -07:00
Brandur
7f85eea3ee Fix low hanging Rubocop TODOs
I wanted to see what fixing Rubocop TODOs was like, so I tried to
eliminate all the easy ones. Most of these were pretty easy, and the
changes required are relatively minimal.

Some of the stuff left is harder. Pretty much everything under
`Metrics/*` is going to be a pretty big yak shave. A few of the others
are just going to need a little more work (e.g. `Style/ClassVars` and
`Style/GuardClause`). Going to stop here for now.
2017-09-27 15:07:18 -07:00
Olivier Bellone
e02ff7f849
Start using RuboCop for linting 2017-09-27 21:28:25 +02:00
Brandur
ed6d7a7cf9 Upgrade stripe-mock to 0.2.0
This pulls us onto the new version of stripe-mock which should be
checking parameters more accurately now that it's on OpenAPI 3.0.
2017-09-22 12:01:30 -07:00
Tim Maxwell
f4412eec1b Work around idiosyncrasies of stripe-mock 2017-09-20 14:11:11 -07:00
Tim Maxwell
0b4ac62eae Remove recipient card tests 2017-09-20 14:03:00 -07:00
Tim Maxwell
d12fcdb862 Remove tests for legacy Bitcoin API 2017-09-20 13:48:05 -07:00
Brandur
1ca67cb954 Correct behavior for error logging
A few changes:

* Add a new `Util.log_error` method which will forward to the equivalent
  of `#error` on a logger.
* Move errors produced by `StripeClient` to use `Util.log_error`.
* Change standard stdout logging behavior to log to stderr in the case
  of `Util.log_error.
* Change `Stripe.log_level` values to be an enum in a similar fashion as
  the standard library's built in `Logger`.
2017-08-11 11:45:43 -07:00
Brandur
cb111a8e74 Add support for setting a logger
Adds support for setting `Stripe.logger` to a logger that's compatible
with `Logger` from Ruby's standard library. In set, the library will no
longer log to stdout, and instead emit straight to the logger and defer
decision on what log level to print to it.

Addresses a request in #566.
2017-08-11 11:22:14 -07:00
Brandur
eb3671b067 Log Stripe-Account values with STRIPE_LOG
Hopefully the last tweak in a while, but a discussion on [1] tipped me
off that this was missing. Here we add a `Stripe-Account` for a request
and response to logging. Follows #566 and #567.

[1] https://github.com/stripe/stripe-node/issues/364
2017-08-03 14:02:06 -07:00
Brandur
441331bd82 Log num_retries with STRIPE_LOG
This one is minor, but I realized after shipping #566 that it would be
nice if the number of retries was also logged for every request. This
patch follows up #566 by adding that in.

I also renamed `retry_count` to `num_retries` because I subjectively
think this name is a little better.
2017-08-03 13:50:00 -07:00
Brandur
ce69d749e1 Implement STRIPE_LOG for stripe-ruby
Adds logging support for stripe-ruby in a similar way that we did it for
stripe-python [1], with the idea that users you can optionally get some
additional low-cost-to-configure logging for operational visibility or
debugging.

I made a few tweaks from the Python implementation (which I'll try to
contribute back to there):

* Added an elapsed parameter to responses so you can tell how long they
  lasted.
* Mixed in idempotency_key to all lines that users have a way to
  aggregate logs related to a request from start to finish.
* Standardized naming between different log lines as much as possible.
* Detect a TTY and produce output that's colorized and formatted.

[1] https://github.com/stripe/stripe-python/pull/269
2017-08-03 13:39:15 -07:00
Jacqueline Xu
43b78055aa Add upcoming invoice subscription items test 2017-08-01 17:22:29 -07:00
Brandur
00180c5f35 Power test suite with stripe-mock
Moves away from Committee and towards stripe-mock, an external
self-contained executable API stub server based on OpenAPI [1]. The
motivation here is that instead of making stripe-ruby a special
snowflake, we can use a single well-tested and feature-rich mock
implementation to drive every API's test suite.

[1] https://github.com/stripe/stripe-mock
2017-07-31 13:25:48 -07:00
Brandur
1417cb5bd1 Allow empty strings in API invocation parameters
Currently, with a normal API resource, you can unset fields by
specifying a `nil` to that field's setter:

``` ruby
c = Charge.retrieve('ch_123')
c.customer = nil
c.save
```

This actually gets serialized as the form `customer=` (i.e. an empty
string), but we had to use the empty string to handle unsets because
form encoding has no concept of a `nil`/`null`.

To try and prevent usage errors, we actually prevent you from setting
fields with an empty string:

``` ruby
c = Charge.retrieve('ch_123')
c.customer = '' # error! use nil instead
```

When specifying parameters though, this doesn't work anywhere nearly as
well because usage patterns like this are very common in Ruby:

``` ruby
charge_opts = {
  params[:amount],
  params[:currency],
  params[:customer],
}
charge = Charge.create(charge_opts)
```

Each one of `params` above may or may not be `nil`, so we've
traditionally filtered those fields out during the invocation of
`Charge.create`.

Recently, I suggested to Slava that we may be able to change this
behavior, and we ended up putting in a patch as part of #557. Users
brought to my attention that this would be far too disruptive of a
change in #560 though, and having thought about it more carefully, I
agree. There's also an argument that filtered `nil` values are just a
better API, especially in Ruby where patterns like the one above are
frequently in effect.

So the best thing I can think of currently is to leave things as they
were before #557, and just require that users use an explicit empty
string when passes in parameter hashes:

``` ruby
Charge.update(customer: '') # will try to unset customer
```

Empty strings will continue to error for `StripeObject` fields like they
always have.

I don't think this is a perfect solution by any means (the different
between values on `StripeObject` versus using parameters is weird), but
it's the least disruptive thing that I can think of right now that gets
us the functionality that we need for endpoints like
`/v1/invoices/upcoming`.

Fixes #560.
2017-07-27 13:47:25 -07:00
Brandur
24a1704f05 Improve error handling safety in the event of unrecognized OAuth error
It was brought up in #562 that in case we receive an OAuth error that we
don't know about, `specific_oauth_error` will fall through with a `nil`,
then picked up by `specific_api_error` which will always try to handle
the error as if it were a `Hash` (even if we know it's not!) and thus
lead to typing problems at runtime.

This patch throws a generic `OAuthError` in cases where a code comes
back that we don't recognize. I'm still crazy about the fact that we
don't have a better way of recognizing an OAuth error in particular, but
it should do the trick.
2017-07-27 09:01:53 -07:00
Yossef Mendelssohn
83444b60cd test handling of invalid_client error code
An error in OAuth deauthorization could return the error code of
`invalid_client`, but that isn't handled by the code. That leads to a
`TypeError` instead of a clean, understandable error.
2017-07-26 13:14:12 -04:00
Brandur
5786abcb7a Merge pull request #557 from stripe/slava-1156
Allows removing coupon via passing nil
2017-07-12 13:59:23 -07:00
Slava Akhmechet
3094199fa8 styling 2017-07-12 12:58:20 -07:00
Slava Akhmechet
b4308aaa7e Moving tests closer to the core 2017-07-12 12:54:53 -07:00
Brandur
d90c2b8e74 Include IDs of resources set as properties
Tweaks the serialization behavior so that when a resource is explicitly
set to a resource's field and that resource is subsequently saved, then
if it looks like the set resource was persisted we extract its ID and
send it up to the API.

By slight extension we also throw an `ArgumentError` if it looks like
that set resource was _not_ persisted because if the user set it
explicitly then it was probably not their intention to have it silently
ignored by the library in the event of a problem.
2017-07-11 12:37:19 -07:00
Slava Akhmechet
d0c4450e7e Allows removing coupon via passing nil 2017-07-07 11:27:26 -07:00
Olivier Bellone
78cd1d4f3d Add parameters when calling pay on an invoice 2017-06-27 14:01:08 +02:00
Brandur
524526c9b7 Remove FIXTURE definitions for ephemeral keys
Redefining the constant like this produces a warning:

```
$ bundle exec rake
/Users/brandur/stripe/stripe-ruby/test/stripe/ephemeral_key_test.rb:75: warning: already initialized constant Stripe::EphemeralKeyTest::FIXTURE
/Users/brandur/stripe/stripe-ruby/test/stripe/ephemeral_key_test.rb:6: warning: previous definition of FIXTURE was here
Loaded suite /Users/brandur/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rake-11.1.2/lib/rake/rake_test_loader
Started
...
```

They also don't appear to be used, so it should be fine just to strip
them out of the test suite.
2017-06-21 15:39:41 -07:00
Marc Hesse
d69ccfc1dd Add support for ephemeral keys (#549) 2017-06-20 18:30:30 -07:00
Marc Hesse
3f01024974 Update OpenAPI files for ephemeral keys (#551) 2017-06-20 16:23:27 -07:00
Remi Jannel
f0b9ba7c6f Remove raw PANs and use magic tokens instead 2017-05-28 10:05:36 -04:00
Andrew Yang
e66eac41d1 Warn user if a known opt (such as idempotency_key) is in params 2017-05-26 13:29:36 -07:00
Remi Jannel
27dca775c7 Do not rely on fixtures for login_links as it's not always here. 2017-05-19 14:49:33 -04:00
Olivier Bellone
330763aa02 Adds support for login links 2017-05-19 17:22:54 +02:00
Brandur
08d24f9835 Modify a few tests to comply with new OpenAPI changes 2017-05-18 15:56:59 -07:00
Olivier Bellone
a645a78cd0 Add OAuth methods 2017-05-18 11:29:59 +02:00
Olivier Bellone
0376e242d9 Support for deserializing webhook events and verifying signatures 2017-04-28 14:25:40 +02:00
Brandur
24e12b1422 Add test structure for InvoiceLineItem
This doesn't come back directly from the API so the suite is empty, but
just for completeness add a test file for the newly created
`InvoiceLineItem` model.
2017-04-26 12:08:19 -07:00
Brandur
06992ef370 Add app info to User-Agent as well 2017-04-17 12:57:16 -07:00
Brandur
65c5c675ed Unset global .stripe_account after test 2017-04-14 14:40:19 -07:00