webhookr 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +1937 -0
  3. data/.travis.yml +8 -8
  4. data/Gemfile +12 -15
  5. data/Guardfile +12 -15
  6. data/MIT-LICENSE +1 -1
  7. data/README.md +8 -7
  8. data/Rakefile +3 -3
  9. data/app/controllers/webhookr/events_controller.rb +23 -16
  10. data/config/routes.rb +9 -8
  11. data/lib/generators/webhookr/add_route_generator.rb +4 -4
  12. data/lib/generators/webhookr/init_generator.rb +14 -12
  13. data/lib/tasks/webhookr_tasks.rake +7 -7
  14. data/lib/webhookr.rb +13 -6
  15. data/lib/webhookr/adapter_response.rb +3 -1
  16. data/lib/webhookr/engine.rb +5 -4
  17. data/lib/webhookr/invalid_payload_error.rb +8 -2
  18. data/lib/webhookr/invalid_security_token_error.rb +3 -1
  19. data/lib/webhookr/invalid_service_name_error.rb +9 -0
  20. data/lib/webhookr/missing_callback_class_error.rb +9 -0
  21. data/lib/webhookr/ostruct_utils.rb +19 -19
  22. data/lib/webhookr/service.rb +28 -24
  23. data/lib/webhookr/services.rb +1 -1
  24. data/lib/webhookr/services/adapter.rb +1 -1
  25. data/lib/webhookr/services/adapter/base.rb +2 -2
  26. data/lib/webhookr/version.rb +3 -1
  27. data/test/dummy/Rakefile +0 -1
  28. data/test/dummy/app/controllers/application_controller.rb +2 -0
  29. data/test/dummy/app/helpers/application_helper.rb +2 -0
  30. data/test/dummy/config.ru +1 -1
  31. data/test/dummy/config/application.rb +8 -8
  32. data/test/dummy/config/boot.rb +4 -1
  33. data/test/dummy/config/environment.rb +2 -0
  34. data/test/dummy/config/environments/development.rb +2 -1
  35. data/test/dummy/config/environments/production.rb +2 -1
  36. data/test/dummy/config/environments/test.rb +5 -4
  37. data/test/dummy/config/initializers/backtrace_silencers.rb +2 -0
  38. data/test/dummy/config/initializers/inflections.rb +2 -0
  39. data/test/dummy/config/initializers/mime_types.rb +2 -0
  40. data/test/dummy/config/initializers/secret_token.rb +3 -1
  41. data/test/dummy/config/initializers/session_store.rb +3 -1
  42. data/test/dummy/config/initializers/webhookr.rb +3 -1
  43. data/test/dummy/config/initializers/wrap_parameters.rb +3 -2
  44. data/test/dummy/config/routes.rb +3 -1
  45. data/test/dummy/script/rails +2 -2
  46. data/test/functional/webhookr/events_controller_test.rb +38 -42
  47. data/test/functional/webhookr/events_routes_test.rb +22 -15
  48. data/test/functional/webhookr/service_test.rb +53 -48
  49. data/test/integration/webhookr/add_route_generator_test.rb +5 -4
  50. data/test/integration/webhookr/init_generator_test.rb +7 -6
  51. data/test/stubs/service_under_test_stubs.rb +26 -33
  52. data/test/test_helper.rb +10 -9
  53. data/test/unit/webhookr/adapter_response_test.rb +7 -6
  54. data/test/unit/webhookr/ostruct_utils_test.rb +14 -14
  55. data/test/unit/webhookr/{Services/ServiceUnderTest → services/service_under_test}/adapter_test.rb +14 -14
  56. data/test/webhookr_test.rb +5 -3
  57. data/webhookr.gemspec +3 -4
  58. 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 = { :format => "json", :controller => "webhookr/events", :action => "show", :service_id => "service_id" }
9
- @show_controller_with_token = @show_controller.merge({ :security_token => "secure" })
10
- @create_controller = { :format => "json", :controller => "webhookr/events", :action => "create", :service_id => "service_id" }
11
- @create_controller_with_token = @create_controller.merge({ :security_token => "secure" })
12
- @path = "/events/service_id"
13
- @path_with_token = "/events/service_id/secure"
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 ":get route to events" do
17
- assert_recognizes(@show_controller, { :path => @path, :method => :get })
17
+ test ':get route to events' do
18
+ assert_recognizes(@show_controller, path: @path, method: :get)
18
19
  end
19
20
 
20
- test ":get route to events with optional :security_token" do
21
- assert_recognizes(@show_controller_with_token, { :path => @path_with_token, :method => :get })
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 ":post route to events" do
25
- assert_recognizes(@create_controller, { :path => @path, :method => :post })
25
+ test ':post route to events' do
26
+ assert_recognizes(@create_controller, path: @path, method: :post)
26
27
  end
27
28
 
28
- test ":post route to events with optional :security_token" do
29
- assert_recognizes(@create_controller_with_token, { :path => @path_with_token, :method => :post })
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 "should raise NameError for a nil service" do
10
- assert_raise(NameError) {
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 "should raise NameError for an unknown service" do
16
- assert_raise(NameError) {
17
- Webhookr::Service.new("unknown_service")
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 "should raise a Webhookr::InvalidPayloadError for an invalid payload" do
22
- assert_raise(Webhookr::InvalidPayloadError) {
23
- Webhookr::Service.new(stub.service_name,
24
- :payload => "blort").process!
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 "should raise a Runtime error if there is no callback class configured" do
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(RuntimeError) {
31
- Webhookr::Service.new(stub.service_name,
32
- :payload => stub.payload).process!
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 "should accept a callback class configuration" do
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 "a service should be initialized" do
61
- # pending "more time" do
62
- # assert_false
63
- # end
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 "process! should silently ignore on_event not present in callback class" do
67
- assert_nothing_raised {
68
- Webhookr::Service.new(stub.service_name,
69
- :payload => stub(:event_type => "no_event").payload).process!
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 "process! should call the on_event method of the callback class for a valid payload" do
74
- Webhookr::Service.new(stub.service_name, :payload => stub.payload).process!
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 "process! should have the payload data email" do
79
- email = "test@test.com"
80
- Webhookr::Service.new(stub.service_name,
81
- :payload => stub(:email => email).payload).process!
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 "process! should process multiple records" do
86
- Webhookr::Service.new(stub.service_name,
87
- :payload => [ stub.payload, stub.payload ]).process!
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
- $: << File.join(File.dirname(__FILE__), %w{ .. .. })
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("../../../tmp", File.dirname(__FILE__))
9
+ destination File.expand_path('../../../tmp', File.dirname(__FILE__))
9
10
  setup :prepare_destination
10
11
 
11
- test "it should add the engine to routes" do
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
- $: << File.join(File.dirname(__FILE__), %w{ .. .. })
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("../../../tmp", File.dirname(__FILE__))
9
+ destination File.expand_path('../../../tmp', File.dirname(__FILE__))
9
10
  setup :prepare_destination
10
11
 
11
12
  def setup
12
- @name = "test_initializer"
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 "it should create the initializer" do
18
+ test 'it should create the initializer' do
18
19
  assert_file @initializer
19
20
  end
20
21
 
21
- test "it should have authorization information" do
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
- :event_type => p["event"],
15
- :data => OpenStruct.new(p["data"])
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 == "blort"
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
- :service_name => "service_under_test",
37
- :event_type => "test_event",
38
- :email => "gerry@zoocasa.com"
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
- :payload => "event=#{ops[:event_type]}&data[email]=#{ops[:email]}",
43
- :service_name => ops[:service_name],
44
- :event_type => ops[:event_type],
45
- :email => ops[:email]
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
- @@call_count = 0
54
- @@email = nil
50
+ @call_count = 0
51
+ @email = nil
55
52
 
56
53
  def self.reset!
57
- @@call_count = 0
58
- @@email = nil
59
- end
60
-
61
- def self.call_count
62
- @@call_count
54
+ @call_count = 0
55
+ @email = nil
63
56
  end
64
57
 
65
- def self.email
66
- @@email
58
+ class << self
59
+ attr_accessor :call_count, :email
67
60
  end
68
61
 
69
62
  def on_test_event(payload)
70
- @@call_count += 1
71
- @@email = payload.data.email
63
+ self.class.call_count += 1
64
+ self.class.email = payload.data.email
72
65
  end
73
- end
66
+ end
@@ -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("Webhookr Tests")
9
+ ])
10
+ SimpleCov.command_name('Webhookr Tests')
9
11
  SimpleCov.start
10
12
 
11
13
  # Configure Rails Environment
12
- ENV["RAILS_ENV"] = "test"
14
+ ENV['RAILS_ENV'] = 'test'
13
15
 
14
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
15
- require "rails/test_help"
16
- require "rails/generators/test_case"
17
- require "minitest/reporters"
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("service_name", "event_type", "payload")
7
+ @subject = Webhookr::AdapterResponse.new('service_name', 'event_type', 'payload')
6
8
  end
7
9
 
8
- test "should respond to event_type" do
10
+ test 'should respond to event_type' do
9
11
  assert_respond_to(@subject, :event_type)
10
12
  end
11
13
 
12
- test "should respond to payload" do
14
+ test 'should respond to payload' do
13
15
  assert_respond_to(@subject, :payload)
14
16
  end
15
17
 
16
- test "should respond to service_name" do
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 = { :a => { :b => { :c => 1 } }, :a1 => [ { :b1 => { :c1 => 1 } } ] }
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 "should be an OpenStruct" do
13
- assert(@converted.is_a?(OpenStruct))
13
+ test 'should be an OpenStruct' do
14
+ assert_instance_of(OpenStruct, @converted)
14
15
  end
15
16
 
16
- test "should have a nested OpenStruct" do
17
- assert(@converted.a.is_a?(OpenStruct))
17
+ test 'should have a nested OpenStruct' do
18
+ assert_instance_of(OpenStruct, @converted.a)
18
19
  end
19
20
 
20
- test "should have a nested nested OpenStruct" do
21
- assert(@converted.a.b.is_a?(OpenStruct))
21
+ test 'should have a nested nested OpenStruct' do
22
+ assert_instance_of(OpenStruct, @converted.a.b)
22
23
  end
23
24
 
24
- test "should have a nested nested nested value of 1" do
25
- assert(@converted.a.b.c == 1)
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 "should replace a nested hash in an array" do
29
- assert(@converted.a1.first.b1.c1 == 1)
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