From a515ea3b1ff032f4a684b18637284393dba8d354 Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Tue, 9 Sep 2025 13:32:42 +0200 Subject: [PATCH] Create webhooks --- app/jobs/webhook/delivery_job.rb | 8 + app/models/event.rb | 2 +- app/models/event/webhook_deliverable.rb | 7 + app/models/webhook.rb | 25 +++ app/models/webhook/delivery.rb | 118 ++++++++++++++ config/credentials/beta.yml.enc | 2 +- config/credentials/development.yml.enc | 2 +- config/credentials/production.yml.enc | 2 +- config/credentials/staging.yml.enc | 2 +- config/credentials/test.yml.enc | 2 +- config/environments/test.rb | 3 + config/queue.yml | 2 +- db/migrate/20250909113219_create_webhooks.rb | 12 ++ ...0250909115638_create_webhook_deliveries.rb | 13 ++ db/schema.rb | 24 ++- db/schema_cache.yml | 147 +++++++++++++++--- test/fixtures/webhook/deliveries.yml | 41 +++++ test/fixtures/webhooks.yml | 11 ++ test/models/webhook/delivery_test.rb | 108 +++++++++++++ test/models/webhook_test.rb | 46 ++++++ 20 files changed, 551 insertions(+), 26 deletions(-) create mode 100644 app/jobs/webhook/delivery_job.rb create mode 100644 app/models/event/webhook_deliverable.rb create mode 100644 app/models/webhook.rb create mode 100644 app/models/webhook/delivery.rb create mode 100644 db/migrate/20250909113219_create_webhooks.rb create mode 100644 db/migrate/20250909115638_create_webhook_deliveries.rb create mode 100644 test/fixtures/webhook/deliveries.yml create mode 100644 test/fixtures/webhooks.yml create mode 100644 test/models/webhook/delivery_test.rb create mode 100644 test/models/webhook_test.rb diff --git a/app/jobs/webhook/delivery_job.rb b/app/jobs/webhook/delivery_job.rb new file mode 100644 index 000000000..4ec08b900 --- /dev/null +++ b/app/jobs/webhook/delivery_job.rb @@ -0,0 +1,8 @@ +class Webhook::DeliveryJob < ApplicationJob + queue_as :webhooks + limits_concurrency to: 1, key: ->(delivery) { delivery.webhook_id } + + def perform(delivery) + delivery.deliver + end +end diff --git a/app/models/event.rb b/app/models/event.rb index 811e207d3..5b2ed2969 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,5 +1,5 @@ class Event < ApplicationRecord - include Notifiable, Particulars, Promptable + include Notifiable, Particulars, Promptable, WebhookDeliverable belongs_to :collection belongs_to :creator, class_name: "User" diff --git a/app/models/event/webhook_deliverable.rb b/app/models/event/webhook_deliverable.rb new file mode 100644 index 000000000..1666390bd --- /dev/null +++ b/app/models/event/webhook_deliverable.rb @@ -0,0 +1,7 @@ +module Event::WebhookDeliverable + extend ActiveSupport::Concern + + included do + has_many :webhook_deliveries, class_name: "Webhook::Delivery", dependent: :delete_all + end +end diff --git a/app/models/webhook.rb b/app/models/webhook.rb new file mode 100644 index 000000000..6878b62b3 --- /dev/null +++ b/app/models/webhook.rb @@ -0,0 +1,25 @@ +class Webhook < ApplicationRecord + PERMITTED_SCHEMES = %w[ http https ].freeze + + encrypts :signing_secret + has_secure_token :signing_secret + + has_many :deliveries, dependent: :delete_all + + validate :validate_url + + def deactivate + update_columns active: false + end + + private + def validate_url + uri = URI.parse(url.presence) + + if PERMITTED_SCHEMES.exclude?(uri.scheme) + errors.add :url, "must use #{PERMITTED_SCHEMES.to_choice_sentence}" + end + rescue URI::InvalidURIError + errors.add :url, "not a URL" + end +end diff --git a/app/models/webhook/delivery.rb b/app/models/webhook/delivery.rb new file mode 100644 index 000000000..5e3252dea --- /dev/null +++ b/app/models/webhook/delivery.rb @@ -0,0 +1,118 @@ +class Webhook::Delivery < ApplicationRecord + USER_AGENT = "fizzy/1.0.0 Webhook" + ENDPOINT_TIMEOUT = 7.seconds + DNS_RESOLUTION_TIMEOUT = 2 + PRIVATE_IP_RANGES = [ + # Loopback + IPAddr.new("127.0.0.0/8"), + IPAddr.new("::1/128"), + # IPv4 mapped to IPv6 + IPAddr.new("::ffff:0:0/96"), + # RFC1918 - local network IP addresses + IPAddr.new("10.0.0.0/8"), + IPAddr.new("172.16.0.0/12"), + IPAddr.new("192.168.0.0/16"), + # Link-local (DHCP and router stuff) + IPAddr.new("169.254.0.0/16"), + IPAddr.new("fe80::/10"), + # ULA + IPAddr.new("fc00::/7") + ].freeze + + belongs_to :webhook + belongs_to :event + + store :request, coder: JSON + store :response, coder: JSON + + encrypts :request, :response + + enum :state, %w[ pending in_progress completed errored ].index_by(&:itself), default: :pending + + def deliver_later + Webhook::DeliveryJob.perform_later(self) + end + + def deliver + in_progress! + + self.request[:headers] = headers + self.response = perform_request + + completed! + save! + rescue + errored! + raise + end + + def succeeded? + completed? && response[:error].blank? && response[:code].between?(200, 299) + end + + private + def perform_request + if private_uri? + { error: :private_uri } + else + response = http.request( + Net::HTTP::Post.new(uri, headers).tap { |request| request.body = payload } + ) + + { code: response.code.to_i, headers: response.to_hash } + end + rescue Resolv::ResolvTimeout, Resolv::ResolvError, SocketError + { error: :dns_lookup_failed } + rescue Net::OpenTimeout, Net::ReadTimeout, Errno::ETIMEDOUT + { error: :connection_timeout } + rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET + { error: :destination_unreachable } + rescue OpenSSL::SSL::SSLError + { error: :failed_tls } + end + + def private_uri? + ip_addresses = [] + + Resolv::DNS.open(timeouts: DNS_RESOLUTION_TIMEOUT) do |dns| + dns.each_address(uri.host) do |ip_address| + ip_addresses << IPAddr.new(ip_address) + end + end + + ip_addresses.any? do |ip_address| + PRIVATE_IP_RANGES.any? do |private_ip_range| + private_ip_range.include?(ip_address) + end + end + end + + def uri + @uri ||= URI(webhook.url) + end + + def http + Net::HTTP.new(uri.host, uri.port).tap do |http| + http.use_ssl = (uri.scheme == "https") + http.open_timeout = ENDPOINT_TIMEOUT + http.read_timeout = ENDPOINT_TIMEOUT + end + end + + def headers + { + "User-Agent" => USER_AGENT, + "Content-Type" => "application/json", + "X-Webhook-Signature" => signature, + "X-Webhook-Timestamp" => Time.current.utc.iso8601 + } + end + + def signature + OpenSSL::HMAC.hexdigest("SHA256", webhook.signing_secret, payload) + end + + def payload + { test: :test }.to_json + end +end diff --git a/config/credentials/beta.yml.enc b/config/credentials/beta.yml.enc index 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opEJcqr/z4GXHnz2+X+PXTdLGrWn9YdIW0Crm8OVhGHQF/K/zg4FUNPhI6JJm/8Rw4gTdDjniF0C0DPOKZW/7Rrk68g9tn1zB6USDjXcHcxE5rg9YFHXGCnrV2Jzd4JF3M+VLgA4wzYk9IjU6PVqtjWpwRXy09EvbspsLgSYDH4LYb3nCkj/CCYTOnI0l/6ofS63kjyNYG/YD5zKpiRcgU18qiQo1uoIS1QijAt3PMggpIbl9gkcgdbed9qd8vptWNHCFKtNmFo2FKJxsLHg+4OzSotl5PYnYzisFFQyMTKuX541fIMtynD2IgazHmE3p2NdUQKGsZXrXrD35EpDCTiHNQf6Emz30xZ2md7R0NZ4043wezZ6w8hzQSPpVPIrCsrcR5UnfO6KoBeC+iMDUS9+QKim+o7OIjggeE8yDS8lsHASRX05+uUGzz3wId6tvUtgJrXvZbGdFNWCfthvcTeb0m8zGRhHBG3aic3GQ6Hwl5TyXH1lo6v66mJWwPL3YhiJu4kyowLz/2DKiMdvuGAqkqCgokpONzraDum3Mid2N/5B/z0xNnaTpqwWW7eyGc7c/1seNZUgA7Maz7eb1wMv72Xl3VJcxoFYANbCKBcRpFL8ctwAhE3QLojqb4EKDhI31wXynu/i5VVU2kNedaBT8rGhoWvZjrd4v4PNzKy65PJxKkBkQKeeXMJMOmVm0BY4QyCOZW0B/H0z867uB9CjA7Xcz7Wl/5dflawlV1mpr6EQECZd+h8v5mGFeOs0zS87q0xQVfhAcTqYGZKjSIrZZdvppKW1PXc8rNFV/3HbawnPeot4RoUx17bnJmZsdbjYtpq8tAvCH5WRdXsJFt4NvPFdMB9fRmVOZs/XoAtyjie9PJ8cZqjkDk0yRPcB0vOqYLVLuoVm6TrRPtlKTAb4uLj0mM4e--srcdud6UpP4PxVue--MRrzzScZrcPeG9j5CpQt3A== \ No newline at end of file diff --git a/config/credentials/development.yml.enc b/config/credentials/development.yml.enc index fbd189683..b5d652eed 100644 --- a/config/credentials/development.yml.enc +++ b/config/credentials/development.yml.enc @@ -1 +1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YxbPIbYKQ== \ No newline at end of file diff --git a/config/credentials/production.yml.enc b/config/credentials/production.yml.enc index 0720c2059..cb2630e38 100644 --- a/config/credentials/production.yml.enc +++ b/config/credentials/production.yml.enc @@ -1 +1 @@ -gJofgOA5kUBwjcFwc6IDRjLbYQfG7qGpm8BcAEnS0FODuMbEE48FKN4jdUfhGAw0yCHu9DT1cJiP2wKR8s7nyf6dUGl6fPljR0sKTvO0eznmWigWCc+Ki6zVef0mRqQ2lZguiokjapG9c4qBcy2MYhVOhw3JAYvH6X4piiyO52nLL/kKLEQ64/uqC9VDKgR8TRhyVRqQN2jvB62VnIGxWX/qSPcapF/jw8ECgPsXgOSY/s4oNvQ8gCuZMweDlSYqFLAXpn2GP+wAaT9u6UmprPx7niDb1EIIm+LFWU1Ar4lY2n5Mo9ckN0ddsoMnwlsYTjlAWnIn9m0o/GbaxWmtSScnxHXbzqr630rEffvZrzLTG60dxkI0it8cVXwSHI6pMQHZw/lUkHd13IHMzR9eNGUq4pV+fdQjhvcfS2sTwVeCZ6VV1DbnY1vOeyMnVfvFLlJ7eqChkpFBAmOKS4hV/Mrqks6OP7K6ubyXt/REwuNhRMeJ1SJAfgiDSKbmNFtSnODyXKP1nFTCvvdndiP1H6t8qkJo8COSnSWWzcyyVKMwEKwbOxxtX2C+V4aySTllw0gFODLjQPxPBwqnkwcEcDEHxtsgImOYnOc0Ro/4faI1C5WdCuF6+VOBGpq1nI4HCQqepx4b/HZt/EyJSaQU08emhHht2vlCqJUN2yPUxnbCKBAbQhz0khRANBWIf8q0wfwSLaAqQHJ9FsZp4ztOC5U4lULzaMJh7dBUbv/9+mH6Yb3lFbg57178GYCgbzFwhvEOlEKBjfwDRFNmYfiVH4YYbZTIgQvu3tSywNNQJ4flu6TSjMC6zdOBi41BAIXH7j04zwpQV7Zndlu3xE1LTMbcLfBdqzOBGuRVD5KvQH5XFj0RfKTg2DN7Mo2XwSb7IZnl3Ga/Tuvb7DwKpRtql2RCYIYXwg5EnCYF3rw8J9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Xtx7ND4MgsP5/NNY29OXUChr5OL658ayJADTdouXgIOghmfEKOacIn3zCiJbSJFH29jgS+MGIA5l6OxHkzj787MR7vC3mjF6Ut0f97LYVEMUshh92DhHao/oM4lcvdoQmhO5OI0zfhvA5vXhTvC2BI+CQjgRKrjLkCpEU8ZvHQ8ceO5hzpx/vf9v5tJVX8Q1EOZ7QklD4p4I9OVRWX3g7Sz4iUP5edwG3iVF7hAw==--mvpDxs3LkB232zsJ--tC39AAOxnHH12G7pzQ0nWw== \ No newline at end of file diff --git a/config/credentials/staging.yml.enc b/config/credentials/staging.yml.enc index b9f413f86..47b2e3af6 100644 --- a/config/credentials/staging.yml.enc +++ b/config/credentials/staging.yml.enc @@ -1 +1 @@ -DHJSW4c/xFkMWWYW3b1uqnoiwuqVvRabPVvSKZWaoM+EPzUses5EaJ7GBXjpVS/4TYQ+0XsuLBTniuhUtFx56PHo1vWWFRy3n8I1YGPOs2X9mvG2wK5MlwSGO55xatQ1MabRyfjVJfW/inx4oWaqh4IsZhQ5uL/PUWwWy+7tKx3yWbPDa9K2vsgJuJJqZav0gJ7Bn5j8sdpj8xRofA0ZmiA3QxVIHOLiBYRXH9gmuzJ9BcDiAfZ1EA2FnM7T/2v5u6K4k5Lkp4Khn7t5kSF5hrUIuU7dWFaz4WFS6sfcCkRTHgPKu1ZfunzQl9vDXxkwVUjnfGnIk6XUQz8qfI1sDwhxjaiKAtn/6V8/zSo0v9lj91kfVWg/e8byzDxtZhRN71lnDgwaRIQxSDumfFIX2Yd5aXoQw7b+VymZaA+8J7vh18GPkBSsWTZ9b1fA1iVHncSDSEYH/FVXK3Ul/mpcN1bJrlBXMkZ0HmDA1EXhMJqPGosE6/OT5f1Zsn5SSpbqM6VZjQ6LOHwNCZs24F8BhGnCQtdl/VlIHKyGq8/zcyWUuROAVSxV9fiMDddWrprjDsjXPUigFqgtyco1CCZ9p6ScMihOzpOhT0dcvvjKZY56e3GR6n7u5JfSL9CpykQOXATHBXJlAc3HYxPxfESkEa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ACCuMdwmKAf9almMdxcXKHvwHLpFJbepHMeuPMlbojNEvfwLUJi2IQ3YFQy5qLbu84aPTW9oSYZEJ0Rx2gOSB1wv3FNtcSAjDiq+plQQs4O1MxtmlcwOeHhwXGSVnnEbjabXJ92JI4yhReIDtEzXrSIaNu4HZtDdzQwe6KwyXoR2dxxcrhUXe2ImjxGLlhnaygdvr5DVQx8XfcOFCIHcSlxCXND+jg9EdBS9m9wW7PpG60+40BB1hjRkoTrzdW9R73m1E22CKOVlTzB8Am9rk1fmmxDyA8/SsErDo9J8LSmzCyQVomjgPfVtAXjcaME53UKL/Q92DLlJZv7koj7bSpN7XSH+ri50BrMCIURJuoivBgu6c8g51OcDMJCUYVajbws9W8hiA4t7dhRsC+1jh+0T/qfu4Kelo/RC5rLMc6de+Mmw0E3HIpTEBttJJJyiIpT0UDzoGcDpq+4Fa+zPRbxIPcD1NnfuhR/og6KsWvLeIsUfKDD+TI/gSE2zxYfC+/XaZO+8TRNc99Ep3qNJ5ZIy1hitn/wMUvCpuMUZAxjOlfh6CZekYgOFESkGKL+b0/O8xeqV7x8qE6SW7pTjCOoykD/jB4XNXY/M2RA4sEIgge0W8v4ENzafDsIFt1eQI/T8vGnbcUxvt7TaJdQ6hwanDKZ8kdmBrO2QG2+Y2vVhoxNYm06gjNAzygnjpYXyUtHLGGhAUjqITMpdXL/ZUnSzN/U8ib0s/p41Rlej472d3NXkbyKFW3E=--bQMkphrAKssxoUy8--J00s66sXG0GcHRoPPKmG7w== \ No newline at end of file diff --git a/config/credentials/test.yml.enc b/config/credentials/test.yml.enc index 53a7eea96..c698d932e 100644 --- a/config/credentials/test.yml.enc +++ b/config/credentials/test.yml.enc @@ -1 +1 @@ -UFzTQdDbGY5BJ/SHVPWzNlBZoO2+BF0eHHwGICuSB5pi8ESFOHrGhmT7a9qpKLl8cbBMTPPayqLEuuuRbbtXEBS3EiDgAP7XaDViQHHAzBwgaATFT7U+1svnAwppTrAToXJlk51BdPLVRzftY9VZJ9RoDTfz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4hxdaYT+bLcoqN736j7db2RGF8xNauiCxYeoqZtg8MN24geNosBnIqEIQmrejZ/UCZtFWVDhtnw/xYrjDu6/3PAxTWfTqNc/WCXrMSrO9E3upAEQKSiveWaPWFdf/opW75IQP1FSLnZ2IAHm4HH1zvvdO5aL4wwB76sGxq7UrNZIBrCsKSAemIJKimuUoZ5mdE/9ZyzeK00dDvuQ/NUulQLEfa1PcQOjvd7YErZJsiaP4BF8/d5pBv3y68rpxsHp1bl2hMkqS/ww+DpYhB37OiSpdxDSR8e7MOE=--L0z/iKlmgeOYpAtv--SJvijuKNOO/M1jGTEdzxFg== \ No newline at end of file diff --git a/config/environments/test.rb b/config/environments/test.rb index 45dc01625..25207bde7 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -67,4 +67,7 @@ Rails.application.configure do # Load test helpers config.autoload_paths += %w[ test/test_helpers ] + + # Auto-encrypt test fixture attributes + config.active_record.encryption.encrypt_fixtures = true end diff --git a/config/queue.yml b/config/queue.yml index f5aa8fb72..8b04d23f8 100644 --- a/config/queue.yml +++ b/config/queue.yml @@ -3,7 +3,7 @@ default: &default - polling_interval: 1 batch_size: 500 workers: - - queues: [ "default", "solid_queue_recurring", "backend" ] + - queues: [ "default", "solid_queue_recurring", "backend", "webhooks" ] threads: 3 processes: <%= ENV.fetch("JOB_CONCURRENCY", 1) %> polling_interval: 0.1 diff --git a/db/migrate/20250909113219_create_webhooks.rb b/db/migrate/20250909113219_create_webhooks.rb new file mode 100644 index 000000000..9aca01aa0 --- /dev/null +++ b/db/migrate/20250909113219_create_webhooks.rb @@ -0,0 +1,12 @@ +class CreateWebhooks < ActiveRecord::Migration[8.1] + def change + create_table :webhooks do |t| + t.boolean :active, default: true, null: false + t.string :name + t.text :url, null: false + t.string :signing_secret, null: false + + t.timestamps + end + end +end diff --git a/db/migrate/20250909115638_create_webhook_deliveries.rb b/db/migrate/20250909115638_create_webhook_deliveries.rb new file mode 100644 index 000000000..876c51202 --- /dev/null +++ b/db/migrate/20250909115638_create_webhook_deliveries.rb @@ -0,0 +1,13 @@ +class CreateWebhookDeliveries < ActiveRecord::Migration[8.1] + def change + create_table :webhook_deliveries do |t| + t.belongs_to :webhook, null: false, foreign_key: true + t.belongs_to :event, null: false, foreign_key: true + t.string :state, null: false + t.text :request + t.text :response + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6d52e28db..5303449d5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -419,7 +419,6 @@ ActiveRecord::Schema[8.1].define(version: 2025_09_15_170056) do t.datetime "created_at", null: false t.string "title" t.datetime "updated_at", null: false - t.index ["title"], name: "index_tags_on_account_id_and_title", unique: true end create_table "user_settings", force: :cascade do |t| @@ -456,6 +455,27 @@ ActiveRecord::Schema[8.1].define(version: 2025_09_15_170056) do t.index ["user_id"], name: "index_watches_on_user_id" end + create_table "webhook_deliveries", force: :cascade do |t| + t.datetime "created_at", null: false + t.integer "event_id", null: false + t.text "request" + t.text "response" + t.string "state", null: false + t.datetime "updated_at", null: false + t.integer "webhook_id", null: false + t.index ["event_id"], name: "index_webhook_deliveries_on_event_id" + t.index ["webhook_id"], name: "index_webhook_deliveries_on_webhook_id" + end + + create_table "webhooks", force: :cascade do |t| + t.boolean "active", default: true, null: false + t.datetime "created_at", null: false + t.string "name" + t.string "signing_secret", null: false + t.datetime "updated_at", null: false + t.text "url", null: false + end + create_table "workflow_stages", force: :cascade do |t| t.string "color" t.datetime "created_at", null: false @@ -501,6 +521,8 @@ ActiveRecord::Schema[8.1].define(version: 2025_09_15_170056) do add_foreign_key "user_settings", "users" add_foreign_key "watches", "cards" add_foreign_key "watches", "users" + add_foreign_key "webhook_deliveries", "events" + add_foreign_key "webhook_deliveries", "webhooks" add_foreign_key "workflow_stages", "workflows" # Virtual tables defined in this database. diff --git a/db/schema_cache.yml b/db/schema_cache.yml index 5820b6173..36d94dfac 100644 --- a/db/schema_cache.yml +++ b/db/schema_cache.yml @@ -1248,7 +1248,7 @@ columns: - *9 - *18 users: - - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + - &37 !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column auto_increment: name: active cast_type: *32 @@ -1318,6 +1318,95 @@ columns: default_function: collation: comment: + webhook_deliveries: + - *5 + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: event_id + cast_type: *3 + sql_type_metadata: *4 + 'null': false + default: + default_function: + collation: + comment: + - *6 + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: request + cast_type: *15 + sql_type_metadata: *16 + 'null': true + default: + default_function: + collation: + comment: + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: response + cast_type: *15 + sql_type_metadata: *16 + 'null': true + default: + default_function: + collation: + comment: + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: state + cast_type: *7 + sql_type_metadata: *8 + 'null': false + default: + default_function: + collation: + comment: + - *9 + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: webhook_id + cast_type: *3 + sql_type_metadata: *4 + 'null': false + default: + default_function: + collation: + comment: + webhooks: + - *37 + - *5 + - *6 + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: name + cast_type: *7 + sql_type_metadata: *8 + 'null': true + default: + default_function: + collation: + comment: + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: signing_secret + cast_type: *7 + sql_type_metadata: *8 + 'null': false + default: + default_function: + collation: + comment: + - *9 + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: url + cast_type: *15 + sql_type_metadata: *16 + 'null': false + default: + default_function: + collation: + comment: workflow_stages: - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column auto_increment: @@ -1397,6 +1486,8 @@ primary_keys: user_settings: id users: id watches: id + webhook_deliveries: id + webhooks: id workflow_stages: id workflows: id data_sources: @@ -1448,6 +1539,8 @@ data_sources: user_settings: true users: true watches: true + webhook_deliveries: true + webhooks: true workflow_stages: true workflows: true indexes: @@ -2858,23 +2951,7 @@ indexes: nulls_not_distinct: comment: valid: true - tags: - - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition - table: tags - name: index_tags_on_account_id_and_title - unique: true - columns: - - title - lengths: {} - orders: {} - opclasses: {} - where: - type: - using: - include: - nulls_not_distinct: - comment: - valid: true + tags: [] user_settings: - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition table: user_settings @@ -2991,6 +3068,40 @@ indexes: nulls_not_distinct: comment: valid: true + webhook_deliveries: + - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition + table: webhook_deliveries + name: index_webhook_deliveries_on_event_id + unique: false + columns: + - event_id + lengths: {} + orders: {} + opclasses: {} + where: + type: + using: + include: + nulls_not_distinct: + comment: + valid: true + - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition + table: webhook_deliveries + name: index_webhook_deliveries_on_webhook_id + unique: false + columns: + - webhook_id + lengths: {} + orders: {} + opclasses: {} + where: + type: + using: + include: + nulls_not_distinct: + comment: + valid: true + webhooks: [] workflow_stages: - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition table: workflow_stages diff --git a/test/fixtures/webhook/deliveries.yml b/test/fixtures/webhook/deliveries.yml new file mode 100644 index 000000000..31fc59c59 --- /dev/null +++ b/test/fixtures/webhook/deliveries.yml @@ -0,0 +1,41 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +successfully_completed: + webhook: active + event: logo_published + state: completed + request: '{"headers": {}}' + response: '{"code": 200, "headers": {}}' + created_at: <%= 1.week.ago %> + +successfully_completed: + webhook: active + event: logo_assignment_jz + state: completed + request: '{"headers": {}}' + response: '{"code": 200, "headers": {}}' + created_at: <%= 1.week.ago + 1.hour %> + +errored: + webhook: active + event: layout_published + state: errored + request: '{"headers": {}}' + response: '{"error": "destination_unreachable"}' + created_at: <%= 1.week.ago %> + +pending: + webhook: active + event: shipping_closed + state: pending + request: null + response: null + created_at: <%= 2.day.ago %> + +in_progress: + webhook: active + event: logo_assignment_km + state: in_progress + request: null + response: null + created_at: <%= 1.day.ago %> diff --git a/test/fixtures/webhooks.yml b/test/fixtures/webhooks.yml new file mode 100644 index 000000000..c2d308c0a --- /dev/null +++ b/test/fixtures/webhooks.yml @@ -0,0 +1,11 @@ +active: + active: true + name: Production API + url: https://api.example.com/webhooks + signing_secret: p94Bx2HjempCdYB4DTyZkY1b + +inactive: + active: false + name: Test Webhook + url: https://test.example.com/webhooks + signing_secret: H8ms8ADcV92v2x17hnLEiL5m diff --git a/test/models/webhook/delivery_test.rb b/test/models/webhook/delivery_test.rb new file mode 100644 index 000000000..1a15c05f3 --- /dev/null +++ b/test/models/webhook/delivery_test.rb @@ -0,0 +1,108 @@ +require "test_helper" + +class Webhook::DeliveryTest < ActiveSupport::TestCase + test "create" do + webhook = webhooks(:active) + event = events(:layout_commented) + delivery = Webhook::Delivery.create!(webhook: webhook, event: event) + + assert_equal "pending", delivery.state + end + + test "succeeded" do + webhook = webhooks(:active) + event = events(:layout_commented) + delivery = Webhook::Delivery.new( + webhook: webhook, + event: event, + response: { code: 200 }, + state: :completed + ) + assert delivery.succeeded? + + delivery.response[:code] = 422 + assert_not delivery.succeeded?, "resonse must have a 2XX status" + + delivery.response[:code] = 200 + delivery.state = :pending + assert_not delivery.succeeded?, "state must be completed" + + delivery.state = :in_progress + assert_not delivery.succeeded?, "state must be completed" + + delivery.state = :errored + assert_not delivery.succeeded?, "state must be completed" + + delivery.state = :completed + delivery.response[:error] = :destination_unreachable + + assert_not delivery.succeeded?, "the response can't have an error" + end + + test "deliver_later" do + delivery = webhook_deliveries(:pending) + + assert_enqueued_with job: Webhook::DeliveryJob, args: [ delivery ] do + delivery.deliver_later + end + end + + test "deliver" do + delivery = webhook_deliveries(:pending) + + stub_request(:post, delivery.webhook.url) + .to_return(status: 200, headers: { "content-type" => "application/json", "x-test" => "foo" }) + + assert_equal "pending", delivery.state + + delivery.deliver + + assert delivery.persisted? + assert_equal "completed", delivery.state + assert delivery.request[:headers].present? + assert_equal [ "foo" ], delivery.response[:headers]["x-test"] + assert_equal 200, delivery.response[:code] + assert delivery.response[:error].blank? + end + + test "deliver when the network timeouts" do + delivery = webhook_deliveries(:pending) + stub_request(:post, delivery.webhook.url).to_timeout + + delivery.deliver + + assert_equal "completed", delivery.state + assert_equal "connection_timeout", delivery.response[:error] + end + + test "deliver when the connection is refused" do + delivery = webhook_deliveries(:pending) + stub_request(:post, delivery.webhook.url).to_raise(Errno::ECONNREFUSED) + + delivery.deliver + + assert_equal "completed", delivery.state + assert_equal "destination_unreachable", delivery.response[:error] + end + + test "deliver when an SSL error occurs" do + delivery = webhook_deliveries(:pending) + stub_request(:post, delivery.webhook.url).to_raise(OpenSSL::SSL::SSLError) + + delivery.deliver + + assert_equal "completed", delivery.state + assert_equal "failed_tls", delivery.response[:error] + end + + test "deliver when an unexpected error occurs" do + delivery = webhook_deliveries(:pending) + stub_request(:post, delivery.webhook.url).to_raise(StandardError, "Unexpected error") + + assert_raises(StandardError) do + delivery.deliver + end + + assert_equal "errored", delivery.state + end +end diff --git a/test/models/webhook_test.rb b/test/models/webhook_test.rb new file mode 100644 index 000000000..43dc66ed3 --- /dev/null +++ b/test/models/webhook_test.rb @@ -0,0 +1,46 @@ +require "test_helper" + +class WebhookTest < ActiveSupport::TestCase + test "create" do + webhook = Webhook.create! name: "Test", url: "https://example.com/webhook" + assert webhook.persisted? + assert webhook.active? + assert webhook.signing_secret.present? + end + + test "validates the url" do + webhook = Webhook.new name: "Test" + assert_not webhook.valid? + assert_includes webhook.errors[:url], "not a URL" + + webhook = Webhook.new name: "Test", url: "not a url" + assert_not webhook.valid? + assert_includes webhook.errors[:url], "not a URL" + + webhook = Webhook.new name: "NOTHING", url: "example.com/webhook" + assert_not webhook.valid? + assert_includes webhook.errors[:url], "must use http or https" + + webhook = Webhook.new name: "BLANK", url: "//example.com/webhook" + assert_not webhook.valid? + assert_includes webhook.errors[:url], "must use http or https" + + webhook = Webhook.new name: "GOPHER", url: "gopher://example.com/webhook" + assert_not webhook.valid? + assert_includes webhook.errors[:url], "must use http or https" + + webhook = Webhook.new name: "HTTP", url: "http://example.com/webhook" + assert webhook.valid? + + webhook = Webhook.new name: "HTTPS", url: "https://example.com/webhook" + assert webhook.valid? + end + + test "deactivate" do + webhook = webhooks(:active) + + assert_changes -> { webhook.active? }, from: true, to: false do + webhook.deactivate + end + end +end