Skip to content

Commit

Permalink
better controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skatkov committed Jun 5, 2024
1 parent 8ccd967 commit d33159c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
7 changes: 1 addition & 6 deletions lib/munster/controllers/receive_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ def create
raise HandlerInactive unless handler.active?
raise HandlerRefused unless handler.valid?(request)

# FIXME: Duplicated webhook will be overwritten here and processing job will be quite for second time.
# This will generate a following error in this case:
# Error performing Munster::ProcessingJob (Job ID: b40f3f28-81be-4c99-bce8-9ad879ec9754) from Async(default) in 9.95ms: ActiveRecord::RecordInvalid (Validation failed: Status Invalid transition from processing to received):
#
# This should be handled properly.
handler.handle(request)
head :ok
rescue KeyError
rescue KeyError # handler was not found, so we return generic 404 error.
render_error("Required parameters were not present in the request", :not_found)
rescue => e
# TODO: add exception handler here
Expand Down
19 changes: 19 additions & 0 deletions test/controllers/webhooks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class WebhooksControllerTest < ActionDispatch::IntegrationTest
setup { @body_str = received_webhooks(:received_provider_disruption).body}

test "accepts a customer.io webhook with changed notification preferences" do
post "/munster/test", params: @body_str, headers: {"CONTENT_TYPE" => "application/json"}
assert_response 200
Expand Down Expand Up @@ -31,4 +32,22 @@ class WebhooksControllerTest < ActionDispatch::IntegrationTest
assert_response 403
assert_equal "Webhook handler did not validate the request (signature or authentication may be invalid)", response.parsed_body["error"]
end

test "will not expose errors, if handler doesn't do that" do
post "/munster/private", params: @body_str, headers: {"CONTENT_TYPE" => "application/json"}

assert_response 200
assert_nil response.parsed_body["error"]
end

test "saves only one webhook" do
body = { event_id: SecureRandom.uuid, body: 'test'}.to_json

assert_changes_by -> { Munster::ReceivedWebhook.count }, exactly: 1 do
3.times do
post "/munster/extract_id", params: body, headers: {"CONTENT_TYPE" => "application/json"}
assert_response 200
end
end
end
end
7 changes: 7 additions & 0 deletions test/dummy/app/webhooks/extract_id_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ExtractIdHandler < WebhookTestHandler
def self.extract_event_id_from_request(action_dispatch_request)
JSON.parse(action_dispatch_request.body.read).fetch("event_id")
end

def self.service_id = :extract_id
end
3 changes: 0 additions & 3 deletions test/dummy/app/webhooks/inactive_handler.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

# This handler accepts webhooks from our integration tests. This webhook gets dispatched
# if a banking provider test fails, indicating that the bank might be having an incident

class InactiveHandler < WebhookTestHandler
def self.active? = false

Expand Down
3 changes: 0 additions & 3 deletions test/dummy/app/webhooks/invalid_handler.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

# This handler accepts webhooks from our integration tests. This webhook gets dispatched
# if a banking provider test fails, indicating that the bank might be having an incident

class InvalidHandler < WebhookTestHandler
def self.valid?(request) = false

Expand Down
7 changes: 7 additions & 0 deletions test/dummy/app/webhooks/private_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

class PrivateHandler < WebhookTestHandler
def self.valid?(request) = false

def self.service_id = :private
def self.expose_errors_to_sender? = false
end
4 changes: 3 additions & 1 deletion test/dummy/config/initializers/munster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require_relative "../../app/webhooks/webhook_test_handler"
require_relative "../../app/webhooks/inactive_handler"
require_relative "../../app/webhooks/invalid_handler"
require_relative "../../app/webhooks/private_handler"
require_relative "../../app/webhooks/extract_id_handler"

Munster.configure do |config|
config.active_handlers = [WebhookTestHandler, InactiveHandler, InvalidHandler]
config.active_handlers = [WebhookTestHandler, InactiveHandler, InvalidHandler, PrivateHandler, ExtractIdHandler]
end

0 comments on commit d33159c

Please sign in to comment.