Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXTREMELY hard to test with rspec #25

Closed
noctivityinc opened this issue Nov 15, 2013 · 6 comments
Closed

EXTREMELY hard to test with rspec #25

noctivityinc opened this issue Nov 15, 2013 · 6 comments

Comments

@noctivityinc
Copy link

I feel like I MUST be missing something. This should be MUCH easier but Ive spent hours on this.

Using the dashboard (not test webhooks as the README states) I created a bunch of actions. I used RequestBin to capture the raw response and saved them locally to fixture files.

I then created a spec to post the data to the server to test the hooks. It simply doesnt work. I either get 500 errors due to Hash Indifference or the events are processed as strings.

Here is are some examples:

 require 'spec_helper'

  describe 'StripeEvents receiving web hooks from Stripe' do
    it "should not allow creation of an application" do
      json = JSON.parse(File.read("spec/fixtures/stripe/customer.subscription.deleted.json"))

      @pro = create(:professional, active: true, stripe_customer_id: '123123123')
      json["data"]["object"]["customer"] = '123123123'

      post( '/stripe', json, :content_type => 'application/json')

      response.status.should eq(200)
      expect(@pro.active).to eq false
    end
  end

which returns an Unauthorized. I've also tried doing different variations of these:

 if Rails.env.development? || Rails.env.test?
  # StripeEvent.event_retriever = lambda { |params| params }
  # StripeEvent.event_retriever = lambda {|params|   Stripe::Event.construct_from(params) }
end

I simply need a way to create specs for every possible hook I have wired up for testing. Ideally I dont need to actually perform every one of those actions on Stripe to get the response.

How can I stub and test the various event types without going crazy?

@invisiblefunnel
Copy link
Contributor

Take a look at this example application: https://github.com/invisiblefunnel/test-hooks. If you find it helpful I'll add it to the README.

The main thing is to stub out the HTTP request which retrieves the event from Stripe. Then you can return the JSON collected from RequestBin as though it was returned directly from Stripe.

@noctivityinc
Copy link
Author

So if I am reading this correctly, you are taking advantage of the security check that is already in Stripe Event and basically telling the spec that when it makes the request to api.stripe.com, intercept them and supply the JSON in the fixture, is that correct?

Thanks!

Joshua Lippiner
.:t 704.323.5661
.:e jlippiner@noctivity.com


Is this one of 20 or more emails in your inbox?

You need a doorman for your inbox!! (Which I created :))

Check out Taper by Clicking Here (http://www.kickstarter.com/projects/1369431496/taper-the-end-of-email-overload) Now. Thanks!

On Friday, November 15, 2013 at 3:51 PM, Danny Whalen wrote:

Take a look at this example application: https://github.com/invisiblefunnel/test-hooks. If you find it helpful I'll add it to the README.
The main thing is to stub out the HTTP request which retrieves the event from Stripe. Then you can return the JSON collected from RequestBin as though it was returned directly from Stripe.


Reply to this email directly or view it on GitHub (#25 (comment)).

@invisiblefunnel
Copy link
Contributor

Exactly. This is how I recommend testing Stripe webhooks. Thanks for your question; I've wanted to build an example app for a while.

@noctivityinc
Copy link
Author

Did you just create that app? I spent ALL DAY looking for something like that. Im going to test it right now but THANK YOU.

Joshua Lippiner
.:t 704.323.5661
.:e jlippiner@noctivity.com


Is this one of 20 or more emails in your inbox?

You need a doorman for your inbox!! (Which I created :))

Check out Taper by Clicking Here (http://www.kickstarter.com/projects/1369431496/taper-the-end-of-email-overload) Now. Thanks!

On Friday, November 15, 2013 at 4:35 PM, Danny Whalen wrote:

Exactly. This is how I recommend testing Stripe webhooks. Thanks for your question; I've wanted to build an example app for a while.


Reply to this email directly or view it on GitHub (#25 (comment)).

@invisiblefunnel
Copy link
Contributor

You're welcome. If you have any feedback on the example app please let me know and I'll incorporate it when I add a testing section to the StripeEvent readme.

@noctivityinc
Copy link
Author

YES!! Ok - I FINALLY got the tests to work. It was fine but here were the gotchas that I found:

  • you need to have webmock in the Gemfile
  • you need to have require 'webmock/rspec' in spec_helper
  • your event id AND the filename NEED to match! StripeEvent calls out to https://api.stripe.com/v1/events/EVENT_ID based on what you pass it and when using this example as is you must name the fixture the same as the event id.

I might recommend changing the method to something like this as it did cause some confusion for me:

def stub_event(fixture_id, json_file = nil)
  json_file ||= fixture_id
  stub_request(:get, "https://api.stripe.com/v1/events/#{fixture_id}").
    to_return(:status => 200, :body => File.read(File.expand_path("../../fixtures/stripe/#{fixture_id}.json",   __FILE__)))
end 

but other than that it was GREAT!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants