stripe_webhooks 0.1 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -11
- data/Rakefile +1 -5
- data/app/controllers/stripe_webhooks/events_controller.rb +6 -6
- data/app/jobs/stripe_webhooks/processor_job.rb +1 -3
- data/app/models/stripe_webhooks/callback.rb +9 -5
- data/app/models/stripe_webhooks/event.rb +10 -11
- data/app/models/stripe_webhooks/performed_callback.rb +1 -1
- data/lib/generators/stripe_webhooks/callback_generator.rb +7 -9
- data/lib/generators/stripe_webhooks/templates/callback_spec.rb.erb +10 -0
- data/lib/stripe_webhooks/callbacks.rb +2 -4
- data/lib/stripe_webhooks/engine.rb +2 -2
- data/lib/stripe_webhooks/version.rb +1 -1
- data/lib/stripe_webhooks.rb +2 -2
- data/spec/controllers/stripe_webhooks/events_controller_spec.rb +14 -17
- data/spec/dummy/config/application.rb +2 -2
- data/spec/dummy/config/database.yml +3 -2
- data/spec/dummy/config/environments/test.rb +2 -0
- data/spec/dummy/config/routes.rb +1 -1
- data/spec/factories/stripe_webhooks_events.rb +4 -4
- data/spec/factories/stripe_webhooks_performed_callbacks.rb +3 -4
- data/spec/helpers/stripe_webhooks/events_helper_spec.rb +0 -1
- data/spec/jobs/stripe_webhooks/processor_job_spec.rb +2 -5
- data/spec/lib/stripe_webhooks/callbacks_spec.rb +11 -8
- data/spec/models/stripe_webhooks/callback_spec.rb +17 -6
- data/spec/models/stripe_webhooks/event_spec.rb +12 -11
- data/spec/models/stripe_webhooks/performed_callback_spec.rb +0 -1
- data/spec/rails_helper.rb +5 -4
- data/spec/spec_helper.rb +42 -44
- metadata +45 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0592b945f0b8f6dcc22b465338e266edb8e1da4
|
4
|
+
data.tar.gz: 7f35b9a9f565afb154351f6486319f79ba398e41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31e2ee5f45feb89438cfc0d866b2022fa115c6bb9ccfefcde9a038c334fb63f601fc2cabba118547d1fa9809ee09907123f65e47d976ad71d10865f45f445853
|
7
|
+
data.tar.gz: aca513f030fac3f13cc4d800cd65fa8c6df60920d35307002d6fd3ee6f65fa2084291f601400a321a952d8d0d61187aba55ab20df1b447d5fd69d928fe3e461b
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# StripeWebhooks
|
2
2
|
|
3
|
+
[![Code Climate](https://codeclimate.com/github/westlakedesign/stripe_webhooks/badges/gpa.svg)](https://codeclimate.com/github/westlakedesign/stripe_webhooks)
|
4
|
+
[![Test Coverage](https://codeclimate.com/github/westlakedesign/stripe_webhooks/badges/coverage.svg)](https://codeclimate.com/github/westlakedesign/stripe_webhooks/coverage)
|
5
|
+
[![Build Status](https://semaphoreci.com/api/v1/moser-it/stripe_webhooks/branches/master/shields_badge.svg)](https://semaphoreci.com/moser-it/stripe_webhooks)
|
6
|
+
|
3
7
|
StripeWebhooks is a Rails engine for dealing with data posted from [Stripe via Webhooks](https://stripe.com/docs/webhooks).
|
4
8
|
|
5
9
|
## Goals
|
@@ -50,12 +54,7 @@ Create a callback object using the generator, which accepts a name argument foll
|
|
50
54
|
|
51
55
|
*See the [official documentation](https://stripe.com/docs/api/ruby#event_types) for a list of possible events.*
|
52
56
|
|
53
|
-
This will
|
54
|
-
|
55
|
-
1. Appends an entry to `config/initializers/stripe_webhook_callbacks.rb`, and creates this file if it does not exist.
|
56
|
-
2. Creates a new callback object at `app/callbacks/NAME_callback.rb`
|
57
|
-
|
58
|
-
A callback is a simple ruby object with a `#run` method.
|
57
|
+
This will creates a new callback object at `app/callbacks/NAME_callback.rb`. A callback is a simple ruby object with a `handles_events` declaration and a `#run` method.
|
59
58
|
|
60
59
|
class CustomerCallback < ApplicationCallback
|
61
60
|
handles_events 'customer.created', 'customer.updated', 'customer.deleted'
|
@@ -65,8 +64,3 @@ A callback is a simple ruby object with a `#run` method.
|
|
65
64
|
end
|
66
65
|
|
67
66
|
end
|
68
|
-
|
69
|
-
Callbacks must be registered in order to be recognized. If you ran the generator, this will have been done for you in an initializer.
|
70
|
-
|
71
|
-
StripeWebhooks.register_callback('customer')
|
72
|
-
|
data/Rakefile
CHANGED
@@ -14,14 +14,11 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
14
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
15
|
end
|
16
16
|
|
17
|
-
APP_RAKEFILE = File.expand_path(
|
17
|
+
APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
|
18
18
|
load 'rails/tasks/engine.rake'
|
19
19
|
|
20
|
-
|
21
20
|
load 'rails/tasks/statistics.rake'
|
22
21
|
|
23
|
-
|
24
|
-
|
25
22
|
Bundler::GemHelper.install_tasks
|
26
23
|
|
27
24
|
require 'rake/testtask'
|
@@ -33,5 +30,4 @@ Rake::TestTask.new(:test) do |t|
|
|
33
30
|
t.verbose = false
|
34
31
|
end
|
35
32
|
|
36
|
-
|
37
33
|
task default: :test
|
@@ -1,24 +1,24 @@
|
|
1
|
-
require_dependency
|
1
|
+
require_dependency 'stripe_webhooks/application_controller'
|
2
2
|
|
3
3
|
module StripeWebhooks
|
4
4
|
class EventsController < ApplicationController
|
5
|
-
protect_from_forgery :
|
5
|
+
protect_from_forgery except: :create
|
6
6
|
|
7
7
|
def create
|
8
8
|
begin
|
9
9
|
raw_data = JSON.parse(request.body.read)
|
10
10
|
rescue JSON::ParserError => e
|
11
|
-
render :
|
11
|
+
render text: e.message, status: 400
|
12
12
|
return
|
13
13
|
end
|
14
14
|
|
15
|
-
@event = StripeWebhooks::Event.find_or_initialize_by(:
|
15
|
+
@event = StripeWebhooks::Event.find_or_initialize_by(stripe_event_id: raw_data['id'])
|
16
16
|
|
17
17
|
if @event.save()
|
18
18
|
StripeWebhooks::ProcessorJob.perform_later(@event)
|
19
|
-
render :
|
19
|
+
render nothing: true, status: 200
|
20
20
|
else
|
21
|
-
render :
|
21
|
+
render text: @event.errors.full_messages.first, status: 422
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -14,9 +14,7 @@ module StripeWebhooks
|
|
14
14
|
StripeWebhooks.callbacks.each do |label|
|
15
15
|
class_name = "#{label.classify}Callback"
|
16
16
|
callback = class_name.constantize.new
|
17
|
-
if callback.handles?(event_type)
|
18
|
-
callback.run_once(event)
|
19
|
-
end
|
17
|
+
callback.run_once(event) if callback.handles?(event_type)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
@@ -34,6 +32,12 @@ module StripeWebhooks
|
|
34
32
|
def handles_events(*event_types)
|
35
33
|
@event_types = event_types
|
36
34
|
end
|
35
|
+
|
36
|
+
# Automatically register subclasses
|
37
|
+
#
|
38
|
+
def inherited(subclass)
|
39
|
+
StripeWebhooks.register_callback(subclass.new.label)
|
40
|
+
end
|
37
41
|
end
|
38
42
|
extend ClassMethods
|
39
43
|
|
@@ -50,9 +54,9 @@ module StripeWebhooks
|
|
50
54
|
# Run the callback only if we have not run it once before
|
51
55
|
#
|
52
56
|
def run_once(event)
|
53
|
-
|
57
|
+
unless StripeWebhooks::PerformedCallback.exists?(stripe_event_id: event.id, label: label)
|
54
58
|
run(event)
|
55
|
-
StripeWebhooks::PerformedCallback.create(:
|
59
|
+
StripeWebhooks::PerformedCallback.create(stripe_event_id: event.id, label: label)
|
56
60
|
end
|
57
61
|
end
|
58
62
|
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module StripeWebhooks
|
2
2
|
class Event < ActiveRecord::Base
|
3
|
-
validates :stripe_event_id, :
|
4
|
-
has_many :performed_callbacks, :
|
3
|
+
validates :stripe_event_id, presence: true, uniqueness: true
|
4
|
+
has_many :performed_callbacks, primary_key: :stripe_event_id, foreign_key: :stripe_event_id
|
5
5
|
|
6
6
|
def stripe_event
|
7
7
|
if created_at < 30.days.ago
|
8
|
-
Rails.logger.warn('The event you requested was created over 30 days ago,
|
8
|
+
Rails.logger.warn('The event you requested was created over 30 days ago,
|
9
|
+
which means it may no longer be available via the Stripe Events API.')
|
9
10
|
end
|
10
11
|
@_stripe_event ||= Stripe::Event.retrieve(stripe_event_id)
|
11
12
|
return @_stripe_event
|
@@ -15,21 +16,19 @@ module StripeWebhooks
|
|
15
16
|
return true if is_authentic
|
16
17
|
begin
|
17
18
|
event = stripe_event()
|
18
|
-
update_attributes(
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
:stripe_created_at => Time.at(event.created).to_datetime
|
23
|
-
})
|
19
|
+
update_attributes(is_processed: true,
|
20
|
+
is_authentic: true,
|
21
|
+
stripe_event_type: event.type,
|
22
|
+
stripe_created_at: Time.zone.at(event.created).to_datetime)
|
24
23
|
return true
|
25
24
|
rescue Stripe::InvalidRequestError
|
26
|
-
update_attributes(:
|
25
|
+
update_attributes(is_processed: true, is_authentic: false)
|
27
26
|
return false
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
31
30
|
def run_callbacks!
|
32
|
-
StripeWebhooks::Callback.run_callbacks_for(
|
31
|
+
StripeWebhooks::Callback.run_callbacks_for(stripe_event_type, stripe_event)
|
33
32
|
return true
|
34
33
|
end
|
35
34
|
|
@@ -3,24 +3,22 @@ require 'generators/stripe_webhooks'
|
|
3
3
|
module StripeWebhooks
|
4
4
|
module Generators
|
5
5
|
class CallbackGenerator < Base
|
6
|
-
desc
|
7
|
-
argument :event_types, :
|
6
|
+
desc 'Creates a stripe webhook callback object'
|
7
|
+
argument :event_types, type: :array, default: [], banner: 'event.type.a event.type.b ...'
|
8
8
|
|
9
9
|
source_root File.expand_path('../templates', __FILE__)
|
10
10
|
|
11
11
|
def create_callback
|
12
12
|
application_callback = 'app/callbacks/application_callback.rb'
|
13
|
-
|
13
|
+
unless File.exist?(application_callback)
|
14
14
|
template 'application_callback.rb.erb', application_callback
|
15
15
|
end
|
16
|
-
|
17
16
|
template 'callback.rb.erb', "app/callbacks/#{name.underscore}_callback.rb"
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
|
18
|
+
if defined?(RSpec)
|
19
|
+
template 'callback_spec.rb.erb',
|
20
|
+
"#{RSpec.configuration.default_path}/callbacks/#{name.underscore}_callback_spec.rb"
|
22
21
|
end
|
23
|
-
append_to_file initializer, "StripeWebhooks.register_callback('#{name.underscore}')\n"
|
24
22
|
end
|
25
23
|
|
26
24
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module StripeWebhooks
|
2
2
|
module Callbacks
|
3
|
-
|
4
3
|
def self.included(base)
|
5
4
|
base.instance_variable_set(:@callbacks, [])
|
6
5
|
base.extend ClassMethods
|
@@ -14,11 +13,10 @@ module StripeWebhooks
|
|
14
13
|
# === Example
|
15
14
|
#
|
16
15
|
# StripeWebhooks.register_callback('my_callback')
|
17
|
-
#
|
16
|
+
#
|
18
17
|
def register_callback(label)
|
19
|
-
@callbacks << label
|
18
|
+
@callbacks << label unless @callbacks.include?(label) || label == 'application'
|
20
19
|
end
|
21
20
|
end
|
22
|
-
|
23
21
|
end
|
24
22
|
end
|
@@ -4,8 +4,8 @@ module StripeWebhooks
|
|
4
4
|
config.autoload_paths << "#{root}/lib"
|
5
5
|
|
6
6
|
config.generators do |g|
|
7
|
-
g.test_framework :rspec, :
|
8
|
-
g.fixture_replacement :factory_girl, :
|
7
|
+
g.test_framework :rspec, fixture: false
|
8
|
+
g.fixture_replacement :factory_girl, dir: 'spec/factories'
|
9
9
|
g.assets false
|
10
10
|
g.helper true
|
11
11
|
end
|
data/lib/stripe_webhooks.rb
CHANGED
@@ -2,47 +2,44 @@ require 'rails_helper'
|
|
2
2
|
|
3
3
|
module StripeWebhooks
|
4
4
|
RSpec.describe EventsController, type: :controller do
|
5
|
-
|
6
5
|
routes { StripeWebhooks::Engine.routes }
|
7
6
|
|
8
|
-
let(:customer_created)
|
7
|
+
let(:customer_created) do
|
9
8
|
StripeMock.mock_webhook_payload('customer.created').to_json
|
10
|
-
|
11
|
-
|
12
|
-
describe "POST #create" do
|
9
|
+
end
|
13
10
|
|
14
|
-
|
15
|
-
|
11
|
+
describe 'POST #create' do
|
12
|
+
it 'should create the event' do
|
13
|
+
expect do
|
16
14
|
post :create, customer_created
|
17
|
-
|
15
|
+
end.to change(StripeWebhooks::Event, :count).by(1)
|
18
16
|
expect(response).to have_http_status(:success)
|
19
17
|
end
|
20
18
|
|
21
|
-
it
|
19
|
+
it 'should not create a duplicate event' do
|
22
20
|
post :create, customer_created
|
23
|
-
expect
|
21
|
+
expect do
|
24
22
|
post :create, customer_created
|
25
|
-
|
23
|
+
end.to_not change(StripeWebhooks::Event, :count)
|
26
24
|
expect(response).to have_http_status(:success)
|
27
25
|
end
|
28
26
|
|
29
|
-
it
|
30
|
-
expect
|
27
|
+
it 'should queue an active job' do
|
28
|
+
expect do
|
31
29
|
post :create, customer_created
|
32
|
-
|
30
|
+
end.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :length).by(1)
|
33
31
|
expect(ActiveJob::Base.queue_adapter.enqueued_jobs.last[:job]).to eq(StripeWebhooks::ProcessorJob)
|
34
32
|
end
|
35
33
|
|
36
|
-
it
|
34
|
+
it 'should return an 400 error when the JSON is poorly formed' do
|
37
35
|
post :create, File.read(StripeWebhooks::Engine.root.join('spec', 'data', 'parse_error.json'))
|
38
36
|
expect(response).to have_http_status(400)
|
39
37
|
end
|
40
38
|
|
41
|
-
it
|
39
|
+
it 'should return a 422 error when the provided data is not valid' do
|
42
40
|
post :create, File.read(StripeWebhooks::Engine.root.join('spec', 'data', 'invalid_data.json'))
|
43
41
|
expect(response).to have_http_status(422)
|
44
42
|
end
|
45
43
|
end
|
46
|
-
|
47
44
|
end
|
48
45
|
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
require File.expand_path('../boot', __FILE__)
|
2
2
|
|
3
3
|
require 'rails/all'
|
4
|
+
require 'stripe'
|
4
5
|
|
5
6
|
Bundler.require(*Rails.groups)
|
6
|
-
require
|
7
|
+
require 'stripe_webhooks'
|
7
8
|
|
8
9
|
module Dummy
|
9
10
|
class Application < Rails::Application
|
@@ -23,4 +24,3 @@ module Dummy
|
|
23
24
|
config.active_record.raise_in_transactional_callbacks = true
|
24
25
|
end
|
25
26
|
end
|
26
|
-
|
@@ -6,8 +6,9 @@
|
|
6
6
|
#
|
7
7
|
default: &default
|
8
8
|
adapter: mysql2
|
9
|
-
username: root
|
9
|
+
username: <%= ENV['DATABASE_USERNAME'] || 'root' %>
|
10
10
|
|
11
11
|
test:
|
12
12
|
<<: *default
|
13
|
-
database: stripe_webhooks_test
|
13
|
+
database: <%= ENV['DATABASE_NAME_TEST'] || 'stripe_webhooks_test' %>
|
14
|
+
password: <%= ENV['DATABASE_PASSWORD'] %>
|
data/spec/dummy/config/routes.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
FactoryGirl.define do
|
2
|
-
factory :stripe_webhooks_event, :
|
3
|
-
stripe_event_id
|
4
|
-
stripe_event_type
|
5
|
-
stripe_created_at DateTime.
|
2
|
+
factory :stripe_webhooks_event, class: 'StripeWebhooks::Event' do
|
3
|
+
stripe_event_id 'evt_00000000000000'
|
4
|
+
stripe_event_type 'charge.succeeded'
|
5
|
+
stripe_created_at DateTime.current
|
6
6
|
is_processed true
|
7
7
|
is_authentic true
|
8
8
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
FactoryGirl.define do
|
2
|
-
factory :stripe_webhooks_performed_callback, :
|
3
|
-
stripe_event_id
|
4
|
-
label
|
2
|
+
factory :stripe_webhooks_performed_callback, class: 'StripeWebhooks::PerformedCallback' do
|
3
|
+
stripe_event_id 'MyString'
|
4
|
+
label 'MyString'
|
5
5
|
end
|
6
|
-
|
7
6
|
end
|
@@ -2,22 +2,19 @@ require 'rails_helper'
|
|
2
2
|
|
3
3
|
module StripeWebhooks
|
4
4
|
RSpec.describe ProcessorJob, type: :job do
|
5
|
-
|
6
5
|
describe '#perform' do
|
7
6
|
it 'should validate the event and run callbacks' do
|
8
7
|
mock_event = StripeMock.mock_webhook_event('customer.created')
|
9
|
-
event = StripeWebhooks::Event.create(:
|
8
|
+
event = StripeWebhooks::Event.create(stripe_event_id: mock_event.id)
|
10
9
|
expect(event).to receive(:run_callbacks!)
|
11
10
|
StripeWebhooks::ProcessorJob.new(event).perform_now
|
12
11
|
end
|
13
12
|
|
14
13
|
it 'should not run callbacks on an event that is not authentic' do
|
15
|
-
event = StripeWebhooks::Event.create(:
|
14
|
+
event = StripeWebhooks::Event.create(stripe_event_id: 'fail!')
|
16
15
|
expect(event).to_not receive(:run_callbacks!)
|
17
16
|
StripeWebhooks::ProcessorJob.perform_now(event)
|
18
17
|
end
|
19
|
-
|
20
18
|
end
|
21
|
-
|
22
19
|
end
|
23
20
|
end
|
@@ -2,19 +2,22 @@ require 'rails_helper'
|
|
2
2
|
|
3
3
|
module StripeWebhooks
|
4
4
|
RSpec.describe Callbacks do
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
# We need to reset the callbacks array to its original state after this spec runs
|
6
|
+
before(:all) do
|
7
|
+
@callbacks_original = StripeWebhooks.callbacks
|
8
|
+
StripeWebhooks.instance_variable_set(:@callbacks, [])
|
9
|
+
end
|
10
|
+
after(:all) do
|
11
|
+
StripeWebhooks.instance_variable_set(:@callbacks, @callbacks_original)
|
8
12
|
end
|
9
13
|
|
10
14
|
describe '.register_callback' do
|
11
15
|
it 'should append the label to the list of callbacks' do
|
12
|
-
expect
|
13
|
-
StripeWebhooks.register_callback('
|
14
|
-
|
15
|
-
expect(StripeWebhooks.callbacks.last).to eq('
|
16
|
+
expect do
|
17
|
+
StripeWebhooks.register_callback('some_callback')
|
18
|
+
end.to change(StripeWebhooks.callbacks, :length).by(1)
|
19
|
+
expect(StripeWebhooks.callbacks.last).to eq('some_callback')
|
16
20
|
end
|
17
21
|
end
|
18
|
-
|
19
22
|
end
|
20
23
|
end
|
@@ -2,17 +2,15 @@ require 'rails_helper'
|
|
2
2
|
|
3
3
|
module StripeWebhooks
|
4
4
|
RSpec.describe Callback, type: :model do
|
5
|
-
|
6
5
|
class ::CustomerCallback < StripeWebhooks::Callback
|
7
6
|
handles_events 'customer.created', 'customer.updated'
|
8
7
|
mattr_accessor :callback_has_ran
|
9
8
|
mattr_accessor :run_count
|
10
|
-
def run(
|
9
|
+
def run(_event)
|
11
10
|
self.run_count += 1
|
12
11
|
self.callback_has_ran = true
|
13
12
|
end
|
14
13
|
end
|
15
|
-
StripeWebhooks.register_callback('customer')
|
16
14
|
|
17
15
|
# Reset the callback_has_ran flag for each test so we can tell if the callback was triggered
|
18
16
|
before(:each) do
|
@@ -40,6 +38,20 @@ module StripeWebhooks
|
|
40
38
|
end
|
41
39
|
end
|
42
40
|
|
41
|
+
describe '.inherited' do
|
42
|
+
it 'should automatically register subclasses' do
|
43
|
+
expect do
|
44
|
+
class ::HelloWorldCallback < ApplicationCallback
|
45
|
+
handles_events 'test'
|
46
|
+
end
|
47
|
+
end.to change(StripeWebhooks.callbacks, :length).by(1)
|
48
|
+
expect(StripeWebhooks.callbacks.last).to eq('hello_world')
|
49
|
+
end
|
50
|
+
it 'should not register the application callback base class' do
|
51
|
+
expect(StripeWebhooks.callbacks).to_not include('application')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
43
55
|
describe '#label' do
|
44
56
|
it 'should return a label string' do
|
45
57
|
expect(CustomerCallback.new.label).to eq('customer')
|
@@ -58,9 +70,9 @@ module StripeWebhooks
|
|
58
70
|
callback = CustomerCallback.new()
|
59
71
|
event = StripeMock.mock_webhook_event('customer.created')
|
60
72
|
callback.run_once(event)
|
61
|
-
expect
|
73
|
+
expect do
|
62
74
|
callback.run_once(event)
|
63
|
-
|
75
|
+
end.to_not change(CustomerCallback, :run_count)
|
64
76
|
expect(CustomerCallback.run_count).to eq(1)
|
65
77
|
end
|
66
78
|
end
|
@@ -76,6 +88,5 @@ module StripeWebhooks
|
|
76
88
|
expect(callback.handles?('charge.succeeded')).to eq(false)
|
77
89
|
end
|
78
90
|
end
|
79
|
-
|
80
91
|
end
|
81
92
|
end
|
@@ -2,24 +2,23 @@ require 'rails_helper'
|
|
2
2
|
|
3
3
|
module StripeWebhooks
|
4
4
|
RSpec.describe Event, type: :model do
|
5
|
-
|
6
5
|
describe '#stripe_event' do
|
7
6
|
it 'should return the event from the Stripe API' do
|
8
7
|
mock_event = StripeMock.mock_webhook_event('customer.created')
|
9
|
-
event = StripeWebhooks::Event.create(:
|
8
|
+
event = StripeWebhooks::Event.create(stripe_event_id: mock_event.id)
|
10
9
|
expect(event.stripe_event).to be_a(Stripe::Event)
|
11
10
|
end
|
12
11
|
|
13
12
|
it 'should trigger a Stripe::InvalidRequestError error' do
|
14
|
-
event = StripeWebhooks::Event.create(:
|
15
|
-
expect
|
13
|
+
event = StripeWebhooks::Event.create(stripe_event_id: 'fail!')
|
14
|
+
expect do
|
16
15
|
event.stripe_event
|
17
|
-
|
16
|
+
end.to raise_error(Stripe::InvalidRequestError)
|
18
17
|
end
|
19
18
|
|
20
19
|
it 'should log a warning if the event is too old' do
|
21
20
|
mock_event = StripeMock.mock_webhook_event('customer.created')
|
22
|
-
event = StripeWebhooks::Event.create(:
|
21
|
+
event = StripeWebhooks::Event.create(stripe_event_id: mock_event.id, created_at: 31.days.ago)
|
23
22
|
expect(Rails.logger).to receive(:warn)
|
24
23
|
event.stripe_event
|
25
24
|
end
|
@@ -28,14 +27,14 @@ module StripeWebhooks
|
|
28
27
|
describe '#validate!' do
|
29
28
|
it 'should mark the event as authentic and update the columns' do
|
30
29
|
mock_event = StripeMock.mock_webhook_event('customer.created')
|
31
|
-
event = StripeWebhooks::Event.create(:
|
30
|
+
event = StripeWebhooks::Event.create(stripe_event_id: mock_event.id)
|
32
31
|
event.validate!
|
33
32
|
expect(event.is_authentic).to eq(true)
|
34
33
|
expect(event.is_processed).to eq(true)
|
35
34
|
end
|
36
35
|
|
37
36
|
it 'should mark the event as not authentic' do
|
38
|
-
event = StripeWebhooks::Event.create(:
|
37
|
+
event = StripeWebhooks::Event.create(stripe_event_id: 'fail!')
|
39
38
|
event.validate!
|
40
39
|
expect(event.is_authentic).to eq(false)
|
41
40
|
expect(event.is_processed).to eq(true)
|
@@ -45,12 +44,14 @@ module StripeWebhooks
|
|
45
44
|
describe '#run_callbacks!' do
|
46
45
|
it 'should run callbacks associated with the given event type' do
|
47
46
|
mock_event = StripeMock.mock_webhook_event('customer.created')
|
48
|
-
event = StripeWebhooks::Event.create(:
|
47
|
+
event = StripeWebhooks::Event.create(stripe_event_id: mock_event.id)
|
49
48
|
event.validate!
|
50
|
-
expect(StripeWebhooks::Callback).to receive(:run_callbacks_for).with(
|
49
|
+
expect(StripeWebhooks::Callback).to receive(:run_callbacks_for).with(
|
50
|
+
event.stripe_event_type,
|
51
|
+
event.stripe_event
|
52
|
+
)
|
51
53
|
event.run_callbacks!
|
52
54
|
end
|
53
55
|
end
|
54
|
-
|
55
56
|
end
|
56
57
|
end
|
data/spec/rails_helper.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
1
|
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
2
|
ENV['RAILS_ENV'] ||= 'test'
|
3
3
|
require 'spec_helper'
|
4
|
-
require File.expand_path(
|
4
|
+
require File.expand_path('../dummy/config/environment.rb', __FILE__)
|
5
5
|
require 'rspec/rails'
|
6
6
|
|
7
7
|
# Add additional requires below this line. Rails is not loaded until this point!
|
8
8
|
require 'database_cleaner'
|
9
|
-
require
|
9
|
+
require 'factory_girl_rails'
|
10
10
|
require 'stripe_mock'
|
11
11
|
require 'simplecov'
|
12
|
-
|
13
12
|
SimpleCov.start 'rails'
|
14
13
|
|
14
|
+
require 'codeclimate-test-reporter'
|
15
|
+
CodeClimate::TestReporter.start
|
16
|
+
|
15
17
|
# Requires supporting ruby files with custom matchers and macros, etc, in
|
16
18
|
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
17
19
|
# run as spec files by default. This means that files in spec/support that end
|
@@ -81,5 +83,4 @@ RSpec.configure do |config|
|
|
81
83
|
example.run
|
82
84
|
end
|
83
85
|
end
|
84
|
-
|
85
86
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -40,48 +40,46 @@ RSpec.configure do |config|
|
|
40
40
|
mocks.verify_partial_doubles = true
|
41
41
|
end
|
42
42
|
|
43
|
-
# The settings below are suggested to provide a good initial experience
|
44
|
-
# with RSpec, but feel free to customize to your heart's content.
|
45
|
-
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
config.
|
51
|
-
|
52
|
-
|
53
|
-
#
|
54
|
-
#
|
55
|
-
# - http://
|
56
|
-
# - http://
|
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
|
-
Kernel.srand config.seed
|
86
|
-
=end
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
# # These two settings work together to allow you to limit a spec run
|
46
|
+
# # to individual examples or groups you care about by tagging them with
|
47
|
+
# # `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
48
|
+
# # get run.
|
49
|
+
# config.filter_run :focus
|
50
|
+
# config.run_all_when_everything_filtered = true
|
51
|
+
#
|
52
|
+
# # Limits the available syntax to the non-monkey patched syntax that is
|
53
|
+
# # recommended. For more details, see:
|
54
|
+
# # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
55
|
+
# # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
56
|
+
# # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
57
|
+
# config.disable_monkey_patching!
|
58
|
+
#
|
59
|
+
# # Many RSpec users commonly either run the entire suite or an individual
|
60
|
+
# # file, and it's useful to allow more verbose output when running an
|
61
|
+
# # individual spec file.
|
62
|
+
# if config.files_to_run.one?
|
63
|
+
# # Use the documentation formatter for detailed output,
|
64
|
+
# # unless a formatter has already been configured
|
65
|
+
# # (e.g. via a command-line flag).
|
66
|
+
# config.default_formatter = 'doc'
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# # Print the 10 slowest examples and example groups at the
|
70
|
+
# # end of the spec run, to help surface which specs are running
|
71
|
+
# # particularly slow.
|
72
|
+
# config.profile_examples = 10
|
73
|
+
#
|
74
|
+
# # Run specs in random order to surface order dependencies. If you find an
|
75
|
+
# # order dependency and want to debug it, you can fix the order by providing
|
76
|
+
# # the seed, which is printed after each run.
|
77
|
+
# # --seed 1234
|
78
|
+
# config.order = :random
|
79
|
+
#
|
80
|
+
# # Seed global randomization in this process using the `--seed` CLI option.
|
81
|
+
# # Setting this allows you to use `--seed` to deterministically reproduce
|
82
|
+
# # test failures related to randomization by passing the same `--seed` value
|
83
|
+
# # as the one that triggered the failure.
|
84
|
+
# Kernel.srand config.seed
|
87
85
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stripe_webhooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Moser Consulting
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "<"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '5.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "<"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: stripe
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -112,20 +112,49 @@ dependencies:
|
|
112
112
|
name: stripe-ruby-mock
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: codeclimate-test-reporter
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
116
144
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
145
|
+
version: '0'
|
118
146
|
type: :development
|
119
147
|
prerelease: false
|
120
148
|
version_requirements: !ruby/object:Gem::Requirement
|
121
149
|
requirements:
|
122
|
-
- -
|
150
|
+
- - ">="
|
123
151
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
125
|
-
description:
|
126
|
-
|
152
|
+
version: '0'
|
153
|
+
description: |-
|
154
|
+
Provides an endpoint for captring data posted via Webhooks in Stripe,
|
155
|
+
and a system for running callbacks in response to desired event types.
|
127
156
|
email:
|
128
|
-
- greg@
|
157
|
+
- greg.woods@moserit.com
|
129
158
|
executables: []
|
130
159
|
extensions: []
|
131
160
|
extra_rdoc_files: []
|
@@ -148,6 +177,7 @@ files:
|
|
148
177
|
- lib/generators/stripe_webhooks/callback_generator.rb
|
149
178
|
- lib/generators/stripe_webhooks/templates/application_callback.rb.erb
|
150
179
|
- lib/generators/stripe_webhooks/templates/callback.rb.erb
|
180
|
+
- lib/generators/stripe_webhooks/templates/callback_spec.rb.erb
|
151
181
|
- lib/stripe_webhooks.rb
|
152
182
|
- lib/stripe_webhooks/callbacks.rb
|
153
183
|
- lib/stripe_webhooks/engine.rb
|
@@ -223,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
223
253
|
version: '0'
|
224
254
|
requirements: []
|
225
255
|
rubyforge_project:
|
226
|
-
rubygems_version: 2.
|
256
|
+
rubygems_version: 2.5.1
|
227
257
|
signing_key:
|
228
258
|
specification_version: 4
|
229
259
|
summary: Stripe Webhook event capture and processing engine.
|