integrallis / stripe_event

Stripe webhook integration for Rails applications.
https://rubygems.org/gems/stripe_event
MIT License
844 stars 104 forks source link

EXTREMELY hard to test with rspec #25

Closed noctivityinc closed 10 years ago

noctivityinc commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 (https://github.com/integrallis/stripe_event/issues/25#issuecomment-28602328).

invisiblefunnel commented 10 years ago

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 commented 10 years ago

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 (https://github.com/integrallis/stripe_event/issues/25#issuecomment-28605373).

invisiblefunnel commented 10 years ago

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 commented 10 years ago

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

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!