forked from integrallis/stripe_event
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_controller_spec.rb
127 lines (93 loc) · 3.65 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
end
routes { StripeEvent::Engine.routes }
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
context "with a signing secret" do
def webhook_with_signature(signature, params)
request.env['HTTP_STRIPE_SIGNATURE'] = signature
webhook params
end
def generate_signature(secret)
payload = 'id=evt_charge_succeeded'
timestamp = Time.now.to_i
signature = Stripe::Webhook::Signature.send(:compute_signature, "#{timestamp}.#{payload}", secret)
"t=#{timestamp},v1=#{signature}"
end
let(:shared_secret) { 'secret' }
before(:each) { StripeEvent.signing_secret = shared_secret }
after(:each) { StripeEvent.signing_secret = nil }
it "rejects missing signature" do
webhook id: 'evt_charge_succeeded'
expect(response.code).to eq '400'
end
it "rejects invalid signature" do
webhook_with_signature "invalid signature", id: 'evt_charge_succeeded'
expect(response.code).to eq '400'
end
it "accepts valid signature" do
stub_event 'evt_charge_succeeded'
webhook_with_signature generate_signature(shared_secret), id: 'evt_charge_succeeded'
expect(response.code).to eq '200'
end
end
end