-
Notifications
You must be signed in to change notification settings - Fork 106
/
webhook_controller_spec.rb
85 lines (64 loc) · 2.56 KB
/
webhook_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'rails_helper'
require 'spec_helper'
describe StripeEvent::WebhookController do
def stub_event(identifier, status = 200)
stub_request(:get, "https://api.stripe.com/v1/events/#{identifier}").
to_return(status: status, body: File.read("spec/support/fixtures/#{identifier}.json"))
end
def webhook(params)
post :event, params.merge(use_route: :stripe_event)
end
it "succeeds with valid event data" do
count = 0
StripeEvent.subscribe('charge.succeeded') { |evt| count += 1 }
stub_event('evt_charge_succeeded')
webhook id: 'evt_charge_succeeded'
expect(response.code).to eq '200'
expect(count).to eq 1
end
it "succeeds when the event_retriever returns nil (simulating an ignored webhook event)" do
count = 0
StripeEvent.event_retriever = lambda { |params| return nil }
StripeEvent.subscribe('charge.succeeded') { |evt| count += 1 }
stub_event('evt_charge_succeeded')
webhook id: 'evt_charge_succeeded'
expect(response.code).to eq '200'
expect(count).to eq 0
end
it "denies access with invalid event data" do
count = 0
StripeEvent.subscribe('charge.succeeded') { |evt| count += 1 }
stub_event('evt_invalid_id', 404)
webhook id: 'evt_invalid_id'
expect(response.code).to eq '401'
expect(count).to eq 0
end
it "ensures user-generated Stripe exceptions pass through" do
StripeEvent.subscribe('charge.succeeded') { |evt| raise Stripe::StripeError, "testing" }
stub_event('evt_charge_succeeded')
expect { webhook id: 'evt_charge_succeeded' }.to raise_error(Stripe::StripeError, /testing/)
end
context "with an authentication secret" do
def webhook_with_secret(secret, params)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials('user', secret)
webhook params
end
before(:each) { StripeEvent.authentication_secret = "secret" }
after(:each) { StripeEvent.authentication_secret = nil }
it "rejects requests with no secret" do
stub_event('evt_charge_succeeded')
webhook id: 'evt_charge_succeeded'
expect(response.code).to eq '401'
end
it "rejects requests with incorrect secret" do
stub_event('evt_charge_succeeded')
webhook_with_secret 'incorrect', id: 'evt_charge_succeeded'
expect(response.code).to eq '401'
end
it "accepts requests with correct secret" do
stub_event('evt_charge_succeeded')
webhook_with_secret 'secret', id: 'evt_charge_succeeded'
expect(response.code).to eq '200'
end
end
end