webhukhs 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3649db576ac151f62fa71f5e929d9b62f9a7dcbbff4c9b831e67fc4141e6524
4
- data.tar.gz: 6d1fa28e296c7449e1c5a55b7f0758f043ff1b3c66098cd16c0e9a55db9d3f16
3
+ metadata.gz: ac61cd0cbe0da35c7a6ad5f2be48cfff0dae74f712ce215eee4e3577fb3ce864
4
+ data.tar.gz: a27c3a7d108b70ddf72a073fba8333641e14530d9afea09e720c54b2ffd41867
5
5
  SHA512:
6
- metadata.gz: 2c05c1b968a0ac107f3366e9d07903a460da24c1d835c9d60e94948e7cd46946d9f067c733433c18fc092a80c2bf0c1eabee4c6d161a6ec2779bc5ccc30214a9
7
- data.tar.gz: 211a266f0ff4951265f2d568427c034010f9367c5a9a0cf653d767457fa9a57b2da0c45b98c2b534015a2e58900e8592d71342accfd247b888060f4f8e54f8c0
6
+ metadata.gz: b09a61434f3b2b92edd9d5774363f29556f790963bd31e6e93684284cd03395685f1d61f5cb2477bf268168d31ce1cb6ab95da5b6c234e28d98a010fa14cebb9
7
+ data.tar.gz: ebb82e8c71c7e449a76bbf87e0db2c3c8350ae7ad85f2ba13708af3cef6554805d1e759310c46da9f78e08db49441e991740d9eb301155eee5b9a33c12768e5d
data/CHANGELOG.md CHANGED
@@ -5,8 +5,16 @@ This format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
5
5
 
6
6
  ## Unreleased
7
7
 
8
+ ## 0.6.0
9
+
10
+ ### Changed
11
+ - ProcessingJob to discard job and report an error on DeserializationError
12
+ - ProcessingJob to handle nil or wrong class argument gracefully
13
+ - Ensure that ID is always generated for ReceivedWebhook model
14
+
8
15
  ## 0.5.0
9
16
 
17
+ ### Changed
10
18
  - Forked from [munster](https://github.com/cheddar-me/munster) and renamed to webhukhs
11
19
  - Changed GitHub repository to https://github.com/skatkov/webhukhs
12
20
  - Testing against rails 7.1, rails 8.0 and rails 8.1
@@ -4,8 +4,27 @@ require "active_job/railtie"
4
4
 
5
5
  module Webhukhs
6
6
  class ProcessingJob < ActiveJob::Base
7
+ class InvalidWebhookArgument < StandardError; end
8
+
9
+ discard_on ActiveJob::DeserializationError do |job, error|
10
+ Rails.error.report(error, context: {
11
+ job_id: job.job_id,
12
+ arguments: job.arguments.map(&:inspect)
13
+ }, severity: :error)
14
+ end
15
+
16
+ discard_on InvalidWebhookArgument do |job, error|
17
+ Rails.error.report(error, context: {
18
+ job_id: job.job_id,
19
+ arguments: job.arguments.map(&:inspect)
20
+ }, severity: :error)
21
+ end
22
+
7
23
  def perform(webhook)
8
- Rails.error.set_context(webhukhs_handler_module_name: webhook.handler_module_name, **Webhukhs.configuration.error_context)
24
+ raise InvalidWebhookArgument, "ProcessingJob received nil webhook argument" if webhook.nil?
25
+ unless webhook.is_a?(Webhukhs::ReceivedWebhook)
26
+ raise InvalidWebhookArgument, "ProcessingJob expected Webhukhs::ReceivedWebhook, got #{webhook.class}"
27
+ end
9
28
 
10
29
  webhook_details_for_logs = "Webhukhs::ReceivedWebhook#%s (handler: %s)" % [webhook.id, webhook.handler]
11
30
  webhook.with_lock do
@@ -9,6 +9,8 @@ module Webhukhs
9
9
 
10
10
  include StateMachineEnum
11
11
 
12
+ before_create :ensure_id
13
+
12
14
  state_machine_enum :status do |s|
13
15
  s.permit_transition(:received, :processing)
14
16
  s.permit_transition(:processing, :failed_validation)
@@ -95,5 +97,21 @@ module Webhukhs
95
97
  def handler
96
98
  handler_module_name.constantize.new
97
99
  end
100
+
101
+ private
102
+
103
+ def ensure_id
104
+ return if id.present?
105
+ # let DB auto-increment handle it
106
+ return if self.class.columns_hash["id"].type == :integer
107
+
108
+ self.id = if SecureRandom.respond_to?(:uuid_v7)
109
+ # Requires Ruby 3.3+. This is preferred approach for time-sortable id's.
110
+ SecureRandom.uuid_v7
111
+ else
112
+ # Ruby < 3.2 fallback - uuid v4
113
+ SecureRandom.uuid
114
+ end
115
+ end
98
116
  end
99
117
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Webhukhs
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+ require_relative "test_app"
5
+
6
+ class ProcessingJobTest < ActiveJob::TestCase
7
+ teardown { Webhukhs::ReceivedWebhook.delete_all }
8
+
9
+ test "discards job and reports error when webhook argument is nil" do
10
+ assert_error_reported(Webhukhs::ProcessingJob::InvalidWebhookArgument) do
11
+ Webhukhs::ProcessingJob.perform_now(nil)
12
+ end
13
+
14
+ assert_no_enqueued_jobs only: Webhukhs::ProcessingJob
15
+ end
16
+
17
+ test "discards job and reports error when webhook argument is not a ReceivedWebhook" do
18
+ assert_error_reported(Webhukhs::ProcessingJob::InvalidWebhookArgument) do
19
+ Webhukhs::ProcessingJob.perform_now("not a webhook")
20
+ end
21
+
22
+ assert_no_enqueued_jobs only: Webhukhs::ProcessingJob
23
+ end
24
+
25
+ test "discards job and reports error when webhook record has been deleted (DeserializationError)" do
26
+ webhook = Webhukhs::ReceivedWebhook.create!(
27
+ handler_event_id: "deserialization-test",
28
+ handler_module_name: "WebhookTestHandler",
29
+ status: "received",
30
+ body: {isValid: true}.to_json
31
+ )
32
+
33
+ Webhukhs::ProcessingJob.perform_later(webhook)
34
+ webhook.destroy!
35
+
36
+ assert_error_reported(ActiveJob::DeserializationError) do
37
+ perform_enqueued_jobs
38
+ end
39
+
40
+ assert_no_enqueued_jobs only: Webhukhs::ProcessingJob
41
+ end
42
+ end
@@ -200,4 +200,16 @@ class TestWebhukhs < ActionDispatch::IntegrationTest
200
200
  assert_equal "received", webhook.status
201
201
  end
202
202
  end
203
+
204
+ test "ensure_id does not override id for integer primary key columns" do
205
+ webhook = Webhukhs::ReceivedWebhook.create!(
206
+ handler_event_id: SecureRandom.uuid,
207
+ handler_module_name: "WebhookTestHandler",
208
+ status: "received",
209
+ body: {isValid: true}.to_json
210
+ )
211
+
212
+ assert_kind_of Integer, webhook.id
213
+ assert webhook.id > 0
214
+ end
203
215
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webhukhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stanislav Katkov
@@ -209,6 +209,7 @@ files:
209
209
  - lib/webhukhs/templates/create_webhukhs_tables.rb.erb
210
210
  - lib/webhukhs/templates/webhukhs.rb
211
211
  - lib/webhukhs/version.rb
212
+ - test/processing_job_test.rb
212
213
  - test/test-webhook-handlers/.DS_Store
213
214
  - test/test-webhook-handlers/extract_id_handler.rb
214
215
  - test/test-webhook-handlers/failing_with_concealed_errors.rb