webhookr 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +1937 -0
- data/.travis.yml +8 -8
- data/Gemfile +12 -15
- data/Guardfile +12 -15
- data/MIT-LICENSE +1 -1
- data/README.md +8 -7
- data/Rakefile +3 -3
- data/app/controllers/webhookr/events_controller.rb +23 -16
- data/config/routes.rb +9 -8
- data/lib/generators/webhookr/add_route_generator.rb +4 -4
- data/lib/generators/webhookr/init_generator.rb +14 -12
- data/lib/tasks/webhookr_tasks.rake +7 -7
- data/lib/webhookr.rb +13 -6
- data/lib/webhookr/adapter_response.rb +3 -1
- data/lib/webhookr/engine.rb +5 -4
- data/lib/webhookr/invalid_payload_error.rb +8 -2
- data/lib/webhookr/invalid_security_token_error.rb +3 -1
- data/lib/webhookr/invalid_service_name_error.rb +9 -0
- data/lib/webhookr/missing_callback_class_error.rb +9 -0
- data/lib/webhookr/ostruct_utils.rb +19 -19
- data/lib/webhookr/service.rb +28 -24
- data/lib/webhookr/services.rb +1 -1
- data/lib/webhookr/services/adapter.rb +1 -1
- data/lib/webhookr/services/adapter/base.rb +2 -2
- data/lib/webhookr/version.rb +3 -1
- data/test/dummy/Rakefile +0 -1
- data/test/dummy/app/controllers/application_controller.rb +2 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/config.ru +1 -1
- data/test/dummy/config/application.rb +8 -8
- data/test/dummy/config/boot.rb +4 -1
- data/test/dummy/config/environment.rb +2 -0
- data/test/dummy/config/environments/development.rb +2 -1
- data/test/dummy/config/environments/production.rb +2 -1
- data/test/dummy/config/environments/test.rb +5 -4
- data/test/dummy/config/initializers/backtrace_silencers.rb +2 -0
- data/test/dummy/config/initializers/inflections.rb +2 -0
- data/test/dummy/config/initializers/mime_types.rb +2 -0
- data/test/dummy/config/initializers/secret_token.rb +3 -1
- data/test/dummy/config/initializers/session_store.rb +3 -1
- data/test/dummy/config/initializers/webhookr.rb +3 -1
- data/test/dummy/config/initializers/wrap_parameters.rb +3 -2
- data/test/dummy/config/routes.rb +3 -1
- data/test/dummy/script/rails +2 -2
- data/test/functional/webhookr/events_controller_test.rb +38 -42
- data/test/functional/webhookr/events_routes_test.rb +22 -15
- data/test/functional/webhookr/service_test.rb +53 -48
- data/test/integration/webhookr/add_route_generator_test.rb +5 -4
- data/test/integration/webhookr/init_generator_test.rb +7 -6
- data/test/stubs/service_under_test_stubs.rb +26 -33
- data/test/test_helper.rb +10 -9
- data/test/unit/webhookr/adapter_response_test.rb +7 -6
- data/test/unit/webhookr/ostruct_utils_test.rb +14 -14
- data/test/unit/webhookr/{Services/ServiceUnderTest → services/service_under_test}/adapter_test.rb +14 -14
- data/test/webhookr_test.rb +5 -3
- data/webhookr.gemspec +3 -4
- metadata +24 -27
@@ -1,33 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
module Webhookr
|
4
6
|
class EventsRoutesTest < ActionController::TestCase
|
5
|
-
|
6
7
|
def setup
|
7
8
|
@routes = Webhookr::Engine.routes
|
8
|
-
@show_controller = { :
|
9
|
-
@show_controller_with_token = @show_controller.merge(
|
10
|
-
@create_controller = { :
|
11
|
-
@create_controller_with_token = @create_controller.merge(
|
12
|
-
@path =
|
13
|
-
@path_with_token =
|
9
|
+
@show_controller = { format: 'json', controller: 'webhookr/events', action: 'show', service_id: 'service_id' }
|
10
|
+
@show_controller_with_token = @show_controller.merge(security_token: 'secure')
|
11
|
+
@create_controller = { format: 'json', controller: 'webhookr/events', action: 'create', service_id: 'service_id' }
|
12
|
+
@create_controller_with_token = @create_controller.merge(security_token: 'secure')
|
13
|
+
@path = '/events/service_id'
|
14
|
+
@path_with_token = '/events/service_id/secure'
|
14
15
|
end
|
15
16
|
|
16
|
-
test
|
17
|
-
assert_recognizes(@show_controller,
|
17
|
+
test ':get route to events' do
|
18
|
+
assert_recognizes(@show_controller, path: @path, method: :get)
|
18
19
|
end
|
19
20
|
|
20
|
-
test
|
21
|
-
assert_recognizes(@show_controller_with_token,
|
21
|
+
test ':get route to events with optional :security_token' do
|
22
|
+
assert_recognizes(@show_controller_with_token, path: @path_with_token, method: :get)
|
22
23
|
end
|
23
24
|
|
24
|
-
test
|
25
|
-
assert_recognizes(@create_controller,
|
25
|
+
test ':post route to events' do
|
26
|
+
assert_recognizes(@create_controller, path: @path, method: :post)
|
26
27
|
end
|
27
28
|
|
28
|
-
test
|
29
|
-
assert_recognizes(@create_controller_with_token,
|
29
|
+
test ':post route to events with optional :security_token' do
|
30
|
+
assert_recognizes(@create_controller_with_token, path: @path_with_token, method: :post)
|
30
31
|
end
|
31
32
|
|
33
|
+
test 'non-JSON routes' do
|
34
|
+
@show_controller[:format] = 'xml'
|
35
|
+
@create_controller[:format] = 'xml'
|
36
|
+
assert_recognizes(@show_controller, path: "#{@path}.xml", method: :get)
|
37
|
+
assert_recognizes(@create_controller, path: "#{@path}.xml", method: :post)
|
38
|
+
end
|
32
39
|
end
|
33
40
|
end
|
@@ -1,42 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
require 'stubs/service_under_test_stubs'
|
3
5
|
|
4
6
|
module Webhookr
|
5
7
|
class ServiceTest < ActiveSupport::TestCase
|
6
|
-
|
7
8
|
include Webhookr::ServiceUnderTest
|
8
9
|
|
9
|
-
test
|
10
|
-
assert_raise(
|
10
|
+
test 'should raise Webhookr::InvalidServiceNameError for a nil service' do
|
11
|
+
assert_raise(Webhookr::InvalidServiceNameError) do
|
11
12
|
Webhookr::Service.new(nil)
|
12
|
-
|
13
|
+
end
|
13
14
|
end
|
14
15
|
|
15
|
-
test
|
16
|
-
assert_raise(
|
17
|
-
Webhookr::Service.new(
|
18
|
-
|
16
|
+
test 'should raise Webhookr::InvalidServiceNameError for an unknown service' do
|
17
|
+
assert_raise(Webhookr::InvalidServiceNameError) do
|
18
|
+
Webhookr::Service.new('unknown_service')
|
19
|
+
end
|
19
20
|
end
|
20
21
|
|
21
|
-
test
|
22
|
-
assert_raise(Webhookr::InvalidPayloadError)
|
23
|
-
Webhookr::Service.new(
|
24
|
-
|
25
|
-
|
22
|
+
test 'should raise a Webhookr::InvalidPayloadError for an invalid payload' do
|
23
|
+
assert_raise(Webhookr::InvalidPayloadError) do
|
24
|
+
Webhookr::Service.new(
|
25
|
+
stub.service_name,
|
26
|
+
payload: 'blort'
|
27
|
+
).process!
|
28
|
+
end
|
26
29
|
end
|
27
30
|
|
28
|
-
test
|
31
|
+
test 'should raise a Webhookr::MissingCallbackClassError error if there is no callback class configured' do
|
29
32
|
Webhookr::ServiceUnderTest::Adapter.config.callback = nil
|
30
|
-
assert_raise(
|
31
|
-
Webhookr::Service.new(
|
32
|
-
|
33
|
-
|
33
|
+
assert_raise(Webhookr::MissingCallbackClassError) do
|
34
|
+
Webhookr::Service.new(
|
35
|
+
stub.service_name,
|
36
|
+
payload: stub.payload
|
37
|
+
).process!
|
38
|
+
end
|
34
39
|
end
|
35
|
-
|
36
40
|
end
|
37
41
|
|
38
42
|
class ServiceTest < ActiveSupport::TestCase
|
39
|
-
|
40
43
|
include Webhookr::ServiceUnderTest
|
41
44
|
|
42
45
|
def setup
|
@@ -44,47 +47,49 @@ module Webhookr
|
|
44
47
|
PlainOldCallBackClass.reset!
|
45
48
|
end
|
46
49
|
|
47
|
-
test
|
48
|
-
assert_nothing_raised
|
50
|
+
test 'should accept a callback class configuration' do
|
51
|
+
assert_nothing_raised do
|
49
52
|
Webhookr::ServiceUnderTest::Adapter.config.callback = Object
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
test "process! should not raise a Webhookr::InvalidPayloadError for a valid payload" do
|
54
|
-
assert_nothing_raised {
|
55
|
-
Webhookr::Service.new(stub.service_name,
|
56
|
-
:payload => stub.payload).process!
|
57
|
-
}
|
53
|
+
end
|
58
54
|
end
|
59
55
|
|
60
|
-
test
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
test 'process! should not raise a Webhookr::InvalidPayloadError for a valid payload' do
|
57
|
+
assert_nothing_raised do
|
58
|
+
Webhookr::Service.new(
|
59
|
+
stub.service_name,
|
60
|
+
payload: stub.payload
|
61
|
+
).process!
|
62
|
+
end
|
64
63
|
end
|
65
64
|
|
66
|
-
test
|
67
|
-
assert_nothing_raised
|
68
|
-
Webhookr::Service.new(
|
69
|
-
|
70
|
-
|
65
|
+
test 'process! should silently ignore on_event not present in callback class' do
|
66
|
+
assert_nothing_raised do
|
67
|
+
Webhookr::Service.new(
|
68
|
+
stub.service_name,
|
69
|
+
payload: stub(event_type: 'no_event').payload
|
70
|
+
).process!
|
71
|
+
end
|
71
72
|
end
|
72
73
|
|
73
|
-
test
|
74
|
-
Webhookr::Service.new(stub.service_name, :
|
74
|
+
test 'process! should call the on_event method of the callback class for a valid payload' do
|
75
|
+
Webhookr::Service.new(stub.service_name, payload: stub.payload).process!
|
75
76
|
assert_equal PlainOldCallBackClass.call_count, 1
|
76
77
|
end
|
77
78
|
|
78
|
-
test
|
79
|
-
email =
|
80
|
-
Webhookr::Service.new(
|
81
|
-
|
79
|
+
test 'process! should have the payload data email' do
|
80
|
+
email = 'test@test.com'
|
81
|
+
Webhookr::Service.new(
|
82
|
+
stub.service_name,
|
83
|
+
payload: stub(email: email).payload
|
84
|
+
).process!
|
82
85
|
assert_equal(email, PlainOldCallBackClass.email)
|
83
86
|
end
|
84
87
|
|
85
|
-
test
|
86
|
-
Webhookr::Service.new(
|
87
|
-
|
88
|
+
test 'process! should process multiple records' do
|
89
|
+
Webhookr::Service.new(
|
90
|
+
stub.service_name,
|
91
|
+
payload: [ stub.payload, stub.payload ]
|
92
|
+
).process!
|
88
93
|
assert_equal 2, PlainOldCallBackClass.call_count
|
89
94
|
end
|
90
95
|
end
|
@@ -1,15 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
|
3
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), %w{ .. .. })
|
3
4
|
require 'test_helper'
|
4
5
|
require 'generators/webhookr/add_route_generator'
|
5
6
|
|
6
7
|
class InitGeneratorTest < Rails::Generators::TestCase
|
7
8
|
tests Webhookr::Generators::AddRouteGenerator
|
8
|
-
destination File.expand_path(
|
9
|
+
destination File.expand_path('../../../tmp', File.dirname(__FILE__))
|
9
10
|
setup :prepare_destination
|
10
11
|
|
11
|
-
test
|
12
|
+
test 'it should add the engine to routes' do
|
12
13
|
run_generator
|
13
14
|
# TODO: Not sure how to confirm the route was added, so just run it for now
|
14
15
|
end
|
15
|
-
end
|
16
|
+
end
|
@@ -1,27 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
|
3
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), %w{ .. .. })
|
3
4
|
require 'test_helper'
|
4
5
|
require 'generators/webhookr/init_generator'
|
5
6
|
|
6
7
|
class InitGeneratorTest < Rails::Generators::TestCase
|
7
8
|
tests Webhookr::Generators::InitGenerator
|
8
|
-
destination File.expand_path(
|
9
|
+
destination File.expand_path('../../../tmp', File.dirname(__FILE__))
|
9
10
|
setup :prepare_destination
|
10
11
|
|
11
12
|
def setup
|
12
|
-
@name =
|
13
|
+
@name = 'test_initializer'
|
13
14
|
@initializer = "config/initializers/#{@name}.rb"
|
14
15
|
run_generator Array.wrap(@name)
|
15
16
|
end
|
16
17
|
|
17
|
-
test
|
18
|
+
test 'it should create the initializer' do
|
18
19
|
assert_file @initializer
|
19
20
|
end
|
20
21
|
|
21
|
-
test
|
22
|
+
test 'it should have authorization information' do
|
22
23
|
assert_file @initializer do |content|
|
23
24
|
assert_match(%r{basic_auth\.username}, content)
|
24
25
|
assert_match(%r{basic_auth\.password}, content)
|
25
26
|
end
|
26
27
|
end
|
27
|
-
end
|
28
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Webhookr
|
2
4
|
module ServiceUnderTest
|
3
5
|
class Adapter
|
@@ -7,22 +9,19 @@ module Webhookr
|
|
7
9
|
|
8
10
|
class << self
|
9
11
|
def process(payload)
|
10
|
-
[*payload].collect do |p|
|
12
|
+
[ *payload ].collect do |p|
|
11
13
|
p = Rack::Utils.parse_nested_query(p)
|
12
14
|
validate(payload)
|
13
|
-
OpenStruct.new(
|
14
|
-
:
|
15
|
-
:
|
16
|
-
|
15
|
+
OpenStruct.new(
|
16
|
+
event_type: p['event'],
|
17
|
+
data: OpenStruct.new(p['data'])
|
18
|
+
)
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
22
|
def validate(payload)
|
21
|
-
if payload.nil? || payload ==
|
22
|
-
raise Webhookr::InvalidPayloadError.new("'#{payload}' is not valid")
|
23
|
-
end
|
23
|
+
raise Webhookr::InvalidPayloadError, payload if payload.nil? || payload == 'blort'
|
24
24
|
end
|
25
|
-
|
26
25
|
end
|
27
26
|
end
|
28
27
|
end
|
@@ -30,44 +29,38 @@ end
|
|
30
29
|
|
31
30
|
module Webhookr
|
32
31
|
module ServiceUnderTest
|
33
|
-
|
34
32
|
def stub(options = {})
|
35
33
|
ops = {
|
36
|
-
:
|
37
|
-
:
|
38
|
-
:
|
34
|
+
service_name: 'service_under_test',
|
35
|
+
event_type: 'test_event',
|
36
|
+
email: 'foo@example.com'
|
39
37
|
}.merge(options)
|
40
38
|
|
41
|
-
OpenStruct.new(
|
42
|
-
:
|
43
|
-
:
|
44
|
-
:
|
45
|
-
:
|
46
|
-
|
39
|
+
OpenStruct.new(
|
40
|
+
payload: "event=#{ops[:event_type]}&data[email]=#{ops[:email]}",
|
41
|
+
service_name: ops[:service_name],
|
42
|
+
event_type: ops[:event_type],
|
43
|
+
email: ops[:email]
|
44
|
+
)
|
47
45
|
end
|
48
|
-
|
49
46
|
end
|
50
47
|
end
|
51
48
|
|
52
49
|
class PlainOldCallBackClass
|
53
|
-
|
54
|
-
|
50
|
+
@call_count = 0
|
51
|
+
@email = nil
|
55
52
|
|
56
53
|
def self.reset!
|
57
|
-
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
def self.call_count
|
62
|
-
@@call_count
|
54
|
+
@call_count = 0
|
55
|
+
@email = nil
|
63
56
|
end
|
64
57
|
|
65
|
-
|
66
|
-
|
58
|
+
class << self
|
59
|
+
attr_accessor :call_count, :email
|
67
60
|
end
|
68
61
|
|
69
62
|
def on_test_event(payload)
|
70
|
-
|
71
|
-
|
63
|
+
self.class.call_count += 1
|
64
|
+
self.class.email = payload.data.email
|
72
65
|
end
|
73
|
-
end
|
66
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'simplecov'
|
2
4
|
require 'coveralls'
|
3
5
|
|
4
|
-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
6
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
|
5
7
|
SimpleCov::Formatter::HTMLFormatter,
|
6
8
|
Coveralls::SimpleCov::Formatter
|
7
|
-
]
|
8
|
-
SimpleCov.command_name(
|
9
|
+
])
|
10
|
+
SimpleCov.command_name('Webhookr Tests')
|
9
11
|
SimpleCov.start
|
10
12
|
|
11
13
|
# Configure Rails Environment
|
12
|
-
ENV[
|
14
|
+
ENV['RAILS_ENV'] = 'test'
|
13
15
|
|
14
|
-
require File.expand_path(
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
16
|
+
require File.expand_path('../dummy/config/environment.rb', __FILE__)
|
17
|
+
require 'rails/test_help'
|
18
|
+
require 'rails/generators/test_case'
|
19
|
+
require 'minitest/reporters'
|
18
20
|
|
19
21
|
Rails.backtrace_cleaner.remove_silencers!
|
20
22
|
|
@@ -26,4 +28,3 @@ MiniTest::Reporters.use!(MiniTest::Reporters::SpecReporter.new)
|
|
26
28
|
puts "Webhookr #{Webhookr::VERSION}"
|
27
29
|
puts "Rails #{Rails::VERSION::STRING}"
|
28
30
|
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} - #{RbConfig::CONFIG['RUBY_INSTALL_NAME']}"
|
29
|
-
|
@@ -1,20 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
class AdapterResponseTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@subject = Webhookr::AdapterResponse.new(
|
7
|
+
@subject = Webhookr::AdapterResponse.new('service_name', 'event_type', 'payload')
|
6
8
|
end
|
7
9
|
|
8
|
-
test
|
10
|
+
test 'should respond to event_type' do
|
9
11
|
assert_respond_to(@subject, :event_type)
|
10
12
|
end
|
11
13
|
|
12
|
-
test
|
14
|
+
test 'should respond to payload' do
|
13
15
|
assert_respond_to(@subject, :payload)
|
14
16
|
end
|
15
17
|
|
16
|
-
test
|
18
|
+
test 'should respond to service_name' do
|
17
19
|
assert_respond_to(@subject, :service_name)
|
18
20
|
end
|
19
|
-
|
20
|
-
end
|
21
|
+
end
|
@@ -1,32 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
require 'webhookr/ostruct_utils'
|
3
5
|
require 'ostruct'
|
4
6
|
|
5
7
|
class OstructUtilsTest < ActiveSupport::TestCase
|
6
|
-
|
7
8
|
def setup
|
8
|
-
@hash = { :
|
9
|
+
@hash = { a: { b: { c: 1 } }, a1: [ { b1: { c1: 1 } } ] }
|
9
10
|
@converted = Webhookr::OstructUtils.to_ostruct(@hash)
|
10
11
|
end
|
11
12
|
|
12
|
-
test
|
13
|
-
|
13
|
+
test 'should be an OpenStruct' do
|
14
|
+
assert_instance_of(OpenStruct, @converted)
|
14
15
|
end
|
15
16
|
|
16
|
-
test
|
17
|
-
|
17
|
+
test 'should have a nested OpenStruct' do
|
18
|
+
assert_instance_of(OpenStruct, @converted.a)
|
18
19
|
end
|
19
20
|
|
20
|
-
test
|
21
|
-
|
21
|
+
test 'should have a nested nested OpenStruct' do
|
22
|
+
assert_instance_of(OpenStruct, @converted.a.b)
|
22
23
|
end
|
23
24
|
|
24
|
-
test
|
25
|
-
|
25
|
+
test 'should have a nested nested nested value of 1' do
|
26
|
+
assert_equal(1, @converted.a.b.c)
|
26
27
|
end
|
27
28
|
|
28
|
-
test
|
29
|
-
|
29
|
+
test 'should replace a nested hash in an array' do
|
30
|
+
assert_equal(1, @converted.a1.first.b1.c1)
|
30
31
|
end
|
31
|
-
|
32
|
-
end
|
32
|
+
end
|