stripe_tester 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06f6394aa9d07f63c05ab4ec77775015f244f57e
4
- data.tar.gz: 5f8becdc1a3db570a0113a31fea1d331ffce73a4
3
+ metadata.gz: dc24a66f567268e0680c750da9e2946053fb0bc5
4
+ data.tar.gz: 5a96c275404f6cb1c60b0ac41900e4cadb953c27
5
5
  SHA512:
6
- metadata.gz: 6a5fd56e31a7bdc6ab7cb1a13922636dbe994d6dffe0edff5e891920ea2cb3ff9dc464e8fd317cd5dfd9ff5fd44b1219e9df5edab0fedfa4340cce75b5f8f906
7
- data.tar.gz: f83c7aa752beb8d06ebb2ad1b49696814807942cf38448a69d56405ef1dcd87139d447e9cc8917a33735670efa847afdd59a1e0871a93179be176fc1e014a096
6
+ metadata.gz: 271bfe5c2a9aa3a02dc15e697abe1c3c6beacf45dac985e4f148aa7e8e6f68028ead6ad26646b6df4509fcebcfa433a511e35099a00ea834e2ffdc9dc4ad0938
7
+ data.tar.gz: 1f0924a40305edbc408ff38a77949f771d875871ef4e6abfca013d498a1b2596d6e58690a65447a403aeafe38d620f5b3b9947ca005d5c0fe039d71f27c087c9
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015 ButterCloud LLC.
1
+ Copyright (c) 2016 ButterCloud LLC.
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -10,7 +10,7 @@ StripeTester allows you to submit webhooks to your application without hitting S
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
  ```ruby
13
- gem 'stripe_tester', "~> 0.4.0"
13
+ gem 'stripe_tester', "~> 0.5.0"
14
14
  ```
15
15
  And then execute:
16
16
  ```bash
@@ -32,52 +32,67 @@ RSpec
32
32
  In your test:
33
33
 
34
34
  1. Set the URL where the webhooks are handled:
35
- ```ruby
36
- # Normal HTTP URL
37
- StripeTester.webhook_url = "http://www.example.com/my_post_url"
35
+ ```ruby
36
+ # Normal HTTP URL
37
+ StripeTester.webhook_url = "http://www.example.com/my_post_url"
38
38
 
39
- # HTTPS URL
40
- StripeTester.webhook_url = "https://www.secure-example.com/my_post_url"
41
- ```
39
+ # HTTPS URL
40
+ StripeTester.webhook_url = "https://www.secure-example.com/my_post_url"
41
+ ```
42
42
 
43
43
  2. If your URL is secured with a self-signed SSL certificate, disable SSL verification:
44
- ```ruby
45
- StripeTester.verify_ssl = false
46
- ```
44
+ ```ruby
45
+ StripeTester.verify_ssl = false
46
+ ```
47
47
 
48
48
  3. If you want to specify which Stripe webhook API version you would like to use (the default will be the latest [supported version](https://github.com/buttercloud/stripe_tester#supported-stripe-webhook-api-versions)):
49
- ```ruby
50
- StripeTester.stripe_version = "2015-10-16"
51
- ```
52
-
53
- 4. Send the webhook. This will send a POST request to the URL with the event data as JSON:
54
- ```ruby
55
- # as a symbol
56
- StripeTester.create_event(:invoice_created)
57
-
58
- # or as a string
59
- StripeTester.create_event("invoice_created")
60
- ```
49
+ ```ruby
50
+ StripeTester.stripe_version = "2015-10-16"
51
+ ```
52
+
53
+ 4. If you are using username and password in your Stripe webhook event, you can provide it in two ways:
54
+
55
+ This will set basic auth with the default username, `stripe`, and the password provided:
56
+ ```ruby
57
+ StripeTester.webhook_password = "<password>"
58
+ ```
59
+ Or you can set the username and password in the URL:
60
+ ```ruby
61
+ # Normal HTTP URL
62
+ StripeTester.webhook_url = "http://stripe:password@www.example.com/my_post_url"
63
+
64
+ # HTTPS URL
65
+ StripeTester.webhook_url = "https://stripe:password@www.secure-example.com/my_post_url"
66
+ ```
67
+
68
+ 5. Send the webhook. This will send a POST request to the URL with the event data as JSON:
69
+ ```ruby
70
+ # as a symbol
71
+ StripeTester.create_event(:invoice_created)
72
+
73
+ # or as a string
74
+ StripeTester.create_event("invoice_created")
75
+ ```
61
76
 
62
77
  Or if you want to overwrite certain attributes globally:
63
- ```ruby
64
- StripeTester.create_event(:invoice_created, {"amount" => 100, "currency" => 'gbp'})
65
- ```
78
+ ```ruby
79
+ StripeTester.create_event(:invoice_created, {"amount" => 100, "currency" => 'gbp'})
80
+ ```
66
81
 
67
82
  Or you can explicitly overwrite certain attributes using deep object merging:
68
- ```ruby
69
- StripeTester.create_event(:customer_subscription_created, {"data"=>{"object"=>{"plan"=>{"id"=>"gold-v1"}}}}, :method=>:merge)
70
- ```
83
+ ```ruby
84
+ StripeTester.create_event(:customer_subscription_created, {"data"=>{"object"=>{"plan"=>{"id"=>"gold-v1"}}}}, :method=>:merge)
85
+ ```
71
86
 
72
87
  If you want to load the JSON only:
73
- ```ruby
74
- json = StripeTester.load_template(:invoice_payment_failed)
75
- ```
88
+ ```ruby
89
+ json = StripeTester.load_template(:invoice_payment_failed)
90
+ ```
76
91
 
77
92
  You can also overwrite certain attributes in the JSON:
78
- ```ruby
79
- json = StripeTester.load_template(:invoice_payment_failed, {"data"=>{"object"=>{"customer"=>"cus_MYCUSTOMERID"}}}, :method=>:merge)
80
- ```
93
+ ```ruby
94
+ json = StripeTester.load_template(:invoice_payment_failed, {"data"=>{"object"=>{"customer"=>"cus_MYCUSTOMERID"}}}, :method=>:merge)
95
+ ```
81
96
 
82
97
  ## Supported Stripe Webhook API Versions
83
98
 
@@ -106,7 +121,7 @@ json = StripeTester.load_template(:invoice_payment_failed, {"data"=>{"object"=>{
106
121
 
107
122
  ## License
108
123
 
109
- Copyright (c) 2015 ButterCloud LLC.
124
+ Copyright (c) 2016 ButterCloud LLC.
110
125
 
111
126
  MIT License
112
127
 
@@ -21,7 +21,7 @@ module StripeTester
21
21
  def self.overwrite_attributes(original_data, attributes={})
22
22
  data = original_data.clone
23
23
  if attributes
24
- attributes.each do |k,v|
24
+ indifferent(attributes).each do |k,v|
25
25
  replace_value(data, k, v)
26
26
  end
27
27
  end
@@ -45,7 +45,7 @@ module StripeTester
45
45
  def self.merge_attributes(original_attributes, new_attributes={})
46
46
  original_attributes = original_attributes.clone
47
47
  if new_attributes
48
- new_attributes.each do |key, value|
48
+ indifferent(new_attributes).each do |key, value|
49
49
  if value.is_a?(Hash) && original_attributes[key].is_a?(Hash)
50
50
  original_attributes[key] = self.merge_attributes original_attributes[key], value
51
51
  else
@@ -64,7 +64,7 @@ module StripeTester
64
64
  req = Net::HTTP::Post.new(post_url.path)
65
65
  req.content_type = 'application/json'
66
66
  req.body = data.to_json
67
-
67
+ req.basic_auth 'stripe', self.basic_authentication_password if !self.basic_authentication_password.nil?
68
68
  http_object = Net::HTTP.new(post_url.hostname, post_url.port)
69
69
  http_object.use_ssl = true if post_url.scheme == 'https'
70
70
  http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE if (!verify_ssl? && http_object.use_ssl?)
@@ -93,8 +93,8 @@ module StripeTester
93
93
 
94
94
  path = gem_root + "/stripe_webhooks/#{version}/#{callback_type}.yml"
95
95
  if File.exists?(path)
96
- template = Psych.load_file(path)
97
-
96
+ template = indifferent(Psych.load_file(path))
97
+
98
98
  unless attributes.empty?
99
99
  if options[:method] == :merge
100
100
  template = merge_attributes(template, attributes)
@@ -125,6 +125,18 @@ module StripeTester
125
125
  @url = nil
126
126
  end
127
127
 
128
+ def self.webhook_password=(webhook_password)
129
+ @webhook_password = webhook_password
130
+ end
131
+
132
+ def self.webhook_password
133
+ @webhook_password
134
+ end
135
+
136
+ def self.basic_authentication_password
137
+ @webhook_password || self.webhook_url.password
138
+ end
139
+
128
140
  def self.stripe_version=(version)
129
141
  @version = version
130
142
  end
@@ -140,5 +152,15 @@ module StripeTester
140
152
  def self.verify_ssl?
141
153
  !@verify_ssl.nil? ? @verify_ssl : true
142
154
  end
155
+
156
+ private
157
+
158
+ def self.indifferent(obj)
159
+ if obj.is_a?(Hash) && obj.respond_to?(:with_indifferent_access)
160
+ obj.with_indifferent_access
161
+ else
162
+ obj
163
+ end
164
+ end
143
165
 
144
166
  end
@@ -1,3 +1,3 @@
1
1
  module StripeTester
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -13,29 +13,47 @@ describe StripeTester do
13
13
  after(:each) do
14
14
  StripeTester.stripe_version = nil
15
15
  StripeTester.verify_ssl = nil
16
+ StripeTester.webhook_password = nil
16
17
  end
17
18
 
18
- it "#load_template should return hash" do
19
- result = StripeTester.load_template(:invoice_created)
20
-
21
- expect(result).to be_a_kind_of(Hash)
22
- end
23
-
24
- it "#load_template should return correct callback type" do
25
- type = "invoice_created"
26
-
27
- returned_hash = StripeTester.load_template(type)
28
- returned_type = returned_hash["type"]
29
-
30
- returned_type.sub!('.', '_')
31
-
32
- expect(returned_type).to eq(type)
33
- end
34
-
35
- it "#load_template should raise an exception when invalid event is given" do
36
- type = "incorrect_type"
37
-
38
- expect { StripeTester.load_template(type) }.to raise_error
19
+ describe "#load_template" do
20
+ it "should return hash" do
21
+ result = StripeTester.load_template(:invoice_created)
22
+
23
+ expect(result).to be_a_kind_of(Hash)
24
+ end
25
+
26
+ context "Hash with_indifferent_access supported" do
27
+ it "should call with_indifferent_access on Hash" do
28
+ allow_any_instance_of(Hash).to receive(:with_indifferent_access)
29
+ expect_any_instance_of(Hash).to receive(:with_indifferent_access)
30
+ StripeTester.load_template(:invoice_created)
31
+ end
32
+ end
33
+
34
+ context "Hash with_indifferent_access not supported" do
35
+ it "should not call with_indifferent_access" do
36
+ allow_any_instance_of(Hash).to receive(:respond_to?).with(:with_indifferent_access).and_return(false)
37
+ expect_any_instance_of(Hash).not_to receive(:with_indifferent_access)
38
+ StripeTester.load_template(:invoice_created)
39
+ end
40
+ end
41
+
42
+ it "should return correct callback type" do
43
+ type = "invoice_created"
44
+
45
+ returned_hash = StripeTester.load_template(type)
46
+ returned_type = returned_hash["type"]
47
+
48
+ returned_type.sub!('.', '_')
49
+
50
+ expect(returned_type).to eq(type)
51
+ end
52
+
53
+ it "should raise an exception when invalid event is given" do
54
+ type = "incorrect_type"
55
+ expect { StripeTester.load_template(type) }.to raise_error("Webhook not found. Please use a correct webhook type or correct Stripe version")
56
+ end
39
57
  end
40
58
 
41
59
  it "#stripe_version should set the correct stripe version" do
@@ -57,6 +75,23 @@ describe StripeTester do
57
75
  expect(result_url.to_s).to eq(url)
58
76
  end
59
77
 
78
+ it "#webhook_url should set the correct url if authentication is provided in url itself" do
79
+ url = 'http://abc:def@www.google.com'
80
+ StripeTester.webhook_url = url
81
+
82
+ result_url = StripeTester.webhook_url
83
+ expect(result_url.to_s).to eq(url)
84
+ end
85
+
86
+ it "#webhook_url should have correct url when password is provided through webhook_password" do
87
+ url = 'http://www.google.com'
88
+ StripeTester.webhook_url = url
89
+ StripeTester.webhook_password = 'password'
90
+
91
+ result_url = StripeTester.webhook_url
92
+ expect(result_url.to_s).to eq(url)
93
+ end
94
+
60
95
  it "#verify_ssl should default to true" do
61
96
  result_verify = StripeTester.verify_ssl?
62
97
  expect(result_verify).to eq(true)
@@ -97,6 +132,38 @@ describe StripeTester do
97
132
  expect(response).to be_truthy
98
133
  end
99
134
 
135
+ it "#post_to_url should return true when authentication is provided" do
136
+ data = StripeTester.load_template(:invoice_created)
137
+ url = "http://localhost:3000/transactions"
138
+ fake_web_url = "http://stripe:password@localhost:3000/transactions"
139
+ StripeTester.webhook_url = url
140
+ StripeTester.webhook_password='password'
141
+
142
+ FakeWeb.register_uri(:post,
143
+ fake_web_url,
144
+ body: data.to_json,
145
+ content_type: 'application/json')
146
+
147
+ response = StripeTester.post_to_url(data)
148
+
149
+ expect(response).to be_truthy
150
+ end
151
+
152
+ it "#post_to_url should return true when authentication is provided through url" do
153
+ data = StripeTester.load_template(:invoice_created)
154
+ url = "http://stripe:password@localhost:3000/transactions"
155
+ StripeTester.webhook_url = url
156
+
157
+ FakeWeb.register_uri(:post,
158
+ url,
159
+ body: data.to_json,
160
+ content_type: 'application/json')
161
+
162
+ response = StripeTester.post_to_url(data)
163
+
164
+ expect(response).to be_truthy
165
+ end
166
+
100
167
  it "#post_to_url should raise an error when request fails" do
101
168
  data = StripeTester.load_template(:invoice_created)
102
169
  url = "http://localhost:3000/"
@@ -112,7 +179,7 @@ describe StripeTester do
112
179
  end
113
180
 
114
181
  it "#post_to_url should raise an error if webhook URL is not set" do
115
- expect { StripeTester.post_to_url() }.to raise_error
182
+ expect { StripeTester.post_to_url() }.to raise_error("Could not post to URL. Please set URL.")
116
183
  end
117
184
 
118
185
  it "#post_to_url should support HTTPS requests" do
@@ -147,24 +214,42 @@ describe StripeTester do
147
214
  expect(response).to be_truthy
148
215
  end
149
216
 
150
- it "#overwrite_attributes should overwrite attributes in default data to custom data" do
151
- original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
152
- overwrite_data = {name: 'smith john', age: 99}
153
-
154
- new_data = StripeTester.overwrite_attributes(original_data, overwrite_data)
155
-
156
- expect(new_data[:name]).to eq(overwrite_data[:name])
157
- expect(new_data[:info][:age]).to eq(overwrite_data[:age])
158
- end
159
-
160
- it "#overwrite_attributes should return an unmodified hash when options don't exist" do
161
- original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
162
-
163
- new_data = StripeTester.overwrite_attributes(original_data)
164
-
165
- expect(new_data).to eq(original_data)
217
+ describe "#overwrite_attributes" do
218
+ it "should overwrite attributes in default data to custom data" do
219
+ original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
220
+ overwrite_data = {name: 'smith john', age: 99}
221
+
222
+ new_data = StripeTester.overwrite_attributes(original_data, overwrite_data)
223
+
224
+ expect(new_data[:name]).to eq(overwrite_data[:name])
225
+ expect(new_data[:info][:age]).to eq(overwrite_data[:age])
226
+ end
227
+
228
+ it "should return an unmodified hash when options don't exist" do
229
+ original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
230
+
231
+ new_data = StripeTester.overwrite_attributes(original_data)
232
+
233
+ expect(new_data).to eq(original_data)
234
+ end
235
+
236
+ context "Hash with_indifferent_access supported" do
237
+ it "should call with_indifferent_access on Hash" do
238
+ allow_any_instance_of(Hash).to receive(:with_indifferent_access).and_return({})
239
+ expect_any_instance_of(Hash).to receive(:with_indifferent_access)
240
+ StripeTester.overwrite_attributes({}, {})
241
+ end
242
+ end
243
+
244
+ context "Hash with_indifferent_access not supported" do
245
+ it "should not call with_indifferent_access" do
246
+ allow_any_instance_of(Hash).to receive(:respond_to?).with(:with_indifferent_access).and_return(false)
247
+ expect_any_instance_of(Hash).not_to receive(:with_indifferent_access)
248
+ StripeTester.overwrite_attributes({}, {})
249
+ end
250
+ end
166
251
  end
167
-
252
+
168
253
  it "#replace_value should replace a value of a given key in the hash" do
169
254
  original_data = {name: 'john smith', info: {age: 45, gender: 'male'}}
170
255
 
@@ -173,22 +258,41 @@ describe StripeTester do
173
258
  expect(new_data[:info][:age]).to eq(99)
174
259
  end
175
260
 
176
- it "#merge_attributes should do a deep merge" do
177
- original_data = {name: 'john smith',
178
- info: {age: 45,
179
- gender: 'male',
180
- occupation: {title: 'Software Developer',
181
- employer: 'ACME, Inc'},
182
- address: {street: '123 Fake St',
183
- city: 'Somewhere',
184
- state: 'NC',
185
- zip: '12345'}}}
186
- new_data = StripeTester.merge_attributes(original_data, {name: 'jane smith', info: {gender: 'female', address: {city: 'Springfield'}}})
187
- expect(new_data[:name]).to eq('jane smith')
188
- expect(new_data[:info][:gender]).to eq('female')
189
- expect(new_data[:info][:age]).to eq(45)
190
- expect(new_data[:info][:address][:city]).to eq('Springfield')
191
- expect(new_data[:info][:address][:state]).to eq('NC')
261
+ describe "#merge_attributes" do
262
+ it "should do a deep merge" do
263
+ original_data = {name: 'john smith',
264
+ info: {age: 45,
265
+ gender: 'male',
266
+ occupation: {title: 'Software Developer',
267
+ employer: 'ACME, Inc'},
268
+ address: {street: '123 Fake St',
269
+ city: 'Somewhere',
270
+ state: 'NC',
271
+ zip: '12345'}}}
272
+ new_data = StripeTester.merge_attributes(original_data, {name: 'jane smith', info: {gender: 'female', address: {city: 'Springfield'}}})
273
+ expect(new_data[:name]).to eq('jane smith')
274
+ expect(new_data[:info][:gender]).to eq('female')
275
+ expect(new_data[:info][:age]).to eq(45)
276
+ expect(new_data[:info][:address][:city]).to eq('Springfield')
277
+ expect(new_data[:info][:address][:state]).to eq('NC')
278
+ end
279
+
280
+ context "Hash with_indifferent_access supported" do
281
+ it "should call with_indifferent_access on Hash" do
282
+ allow_any_instance_of(Hash).to receive(:with_indifferent_access).and_return({})
283
+ expect_any_instance_of(Hash).to receive(:with_indifferent_access)
284
+ StripeTester.merge_attributes({}, {})
285
+ end
286
+ end
287
+
288
+ context "Hash with_indifferent_access not supported" do
289
+ it "should not call with_indifferent_access" do
290
+ allow_any_instance_of(Hash).to receive(:respond_to?).with(:with_indifferent_access).and_return(false)
291
+ expect_any_instance_of(Hash).not_to receive(:with_indifferent_access)
292
+ StripeTester.merge_attributes({}, {})
293
+ end
294
+ end
192
295
  end
296
+
193
297
  end
194
298
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe_tester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Buttercloud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-10 00:00:00.000000000 Z
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler