feat(ntfy): add NtfyDispatchable concern for fanning out to admins
This commit is contained in:
21
app/models/concerns/ntfy_dispatchable.rb
Normal file
21
app/models/concerns/ntfy_dispatchable.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
module NtfyDispatchable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def dispatch_ntfy(event_type, title:, message:, priority: 3, tags: [], click: nil)
|
||||
AdminUser.where(ntfy_enabled: true).find_each do |admin|
|
||||
next unless admin.ntfy_configured?
|
||||
|
||||
SendNtfyNotificationJob.perform_later(
|
||||
admin.id,
|
||||
event_type,
|
||||
title: title,
|
||||
message: message,
|
||||
priority: priority,
|
||||
tags: tags,
|
||||
click: click
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
49
test/models/concerns/ntfy_dispatchable_test.rb
Normal file
49
test/models/concerns/ntfy_dispatchable_test.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
require "test_helper"
|
||||
|
||||
class NtfyDispatchableTest < ActiveJob::TestCase
|
||||
setup do
|
||||
ActiveJob::Base.queue_adapter = :test
|
||||
@admin = AdminUser.create!(
|
||||
name: "Dispatch Admin", email: "dispatch@example.com",
|
||||
password: "password123",
|
||||
ntfy_topic: "dispatch-topic", ntfy_token: "tk_dispatch",
|
||||
ntfy_enabled: true,
|
||||
ntfy_server_url: "https://ntfy.example.com"
|
||||
)
|
||||
|
||||
stub_request(:post, "https://ntfy.example.com/dispatch-topic")
|
||||
.to_return(status: 200, body: "", headers: {})
|
||||
end
|
||||
|
||||
test "dispatch_ntfy enqueues a SendNtfyNotificationJob per configured admin" do
|
||||
assert_enqueued_jobs 1 do
|
||||
TestModel.dispatch_ntfy("sms_failed",
|
||||
title: "SMS failed",
|
||||
message: "msg_abc failed",
|
||||
priority: 5,
|
||||
tags: ["x"],
|
||||
click: "https://app/admin/logs"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "dispatch_ntfy skips admins without ntfy configured" do
|
||||
AdminUser.create!(name: "No Ntfy", email: "none@example.com", password: "password123")
|
||||
|
||||
assert_enqueued_jobs 1 do
|
||||
TestModel.dispatch_ntfy("sms_failed", title: "T", message: "M", priority: 3, tags: [])
|
||||
end
|
||||
end
|
||||
|
||||
test "dispatch_ntfy enqueues zero jobs if no admins configured" do
|
||||
@admin.update!(ntfy_enabled: false)
|
||||
|
||||
assert_enqueued_jobs 0 do
|
||||
TestModel.dispatch_ntfy("sms_failed", title: "T", message: "M", priority: 3, tags: [])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class TestModel
|
||||
include NtfyDispatchable
|
||||
end
|
||||
Reference in New Issue
Block a user