feat(ntfy): add Ntfy::Publisher service with HTTP POST + auth
This commit is contained in:
105
test/lib/my_smsa_pio/notifications/ntfy/publisher_test.rb
Normal file
105
test/lib/my_smsa_pio/notifications/ntfy/publisher_test.rb
Normal file
@@ -0,0 +1,105 @@
|
||||
require "test_helper"
|
||||
|
||||
module MySmsaPio
|
||||
module Notifications
|
||||
module Ntfy
|
||||
class PublisherTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@admin = AdminUser.create!(
|
||||
name: "Notifier", email: "notif@example.com",
|
||||
password: "password123",
|
||||
ntfy_topic: "mysmsa-test-topic",
|
||||
ntfy_token: "tk_test_token",
|
||||
ntfy_enabled: true,
|
||||
ntfy_server_url: "https://ntfy.example.com"
|
||||
)
|
||||
end
|
||||
|
||||
test "publish sends POST to the correct ntfy URL with JSON body and auth header" do
|
||||
stubbed = stub_request(:post, "https://ntfy.example.com/mysmsa-test-topic")
|
||||
.with(
|
||||
headers: {
|
||||
"Authorization" => "Bearer tk_test_token",
|
||||
"Content-Type" => "application/json"
|
||||
},
|
||||
body: hash_including(
|
||||
"topic" => "mysmsa-test-topic",
|
||||
"title" => "Gateway offline",
|
||||
"message" => /gw-001 went offline/,
|
||||
"priority" => 4,
|
||||
"tags" => ->(tags) { tags.include?("rotating_light") }
|
||||
)
|
||||
)
|
||||
.to_return(status: 200, body: "", headers: {})
|
||||
|
||||
result = Publisher.new(@admin).publish(
|
||||
title: "Gateway offline",
|
||||
message: "gw-001 went offline at #{Time.current}",
|
||||
priority: 4,
|
||||
tags: ["rotating_light"]
|
||||
)
|
||||
|
||||
assert result
|
||||
assert_requested stubbed
|
||||
end
|
||||
|
||||
test "publish returns false on HTTP failure without raising" do
|
||||
stub_request(:post, "https://ntfy.example.com/mysmsa-test-topic")
|
||||
.to_return(status: 500, body: "error")
|
||||
|
||||
result = Publisher.new(@admin).publish(
|
||||
title: "Test",
|
||||
message: "body",
|
||||
priority: 3,
|
||||
tags: []
|
||||
)
|
||||
|
||||
assert_not result
|
||||
end
|
||||
|
||||
test "publish returns false if admin not ntfy_configured?" do
|
||||
unconfigured = AdminUser.create!(
|
||||
name: "No Ntfy", email: "none@example.com",
|
||||
password: "password123"
|
||||
)
|
||||
|
||||
result = Publisher.new(unconfigured).publish(
|
||||
title: "X", message: "Y", priority: 3, tags: []
|
||||
)
|
||||
|
||||
assert_not result
|
||||
end
|
||||
|
||||
test "publish uses default server URL from ENV when ntfy_server_url is nil" do
|
||||
ENV["NTFY_SERVER_URL"] = "https://default-ntfy.example.com"
|
||||
admin = AdminUser.create!(
|
||||
name: "Default", email: "default@example.com",
|
||||
password: "password123",
|
||||
ntfy_topic: "def-topic", ntfy_token: "tk_def",
|
||||
ntfy_enabled: true
|
||||
)
|
||||
|
||||
stub = stub_request(:post, "https://default-ntfy.example.com/def-topic")
|
||||
.to_return(status: 200, body: "", headers: {})
|
||||
|
||||
Publisher.new(admin).publish(title: "T", message: "M", priority: 3, tags: [])
|
||||
assert_requested stub
|
||||
ensure
|
||||
ENV.delete("NTFY_SERVER_URL")
|
||||
end
|
||||
|
||||
test "publish includes click URL when provided" do
|
||||
stub = stub_request(:post, "https://ntfy.example.com/mysmsa-test-topic")
|
||||
.with(body: hash_including("click" => "https://app.example.com/admin/gateways"))
|
||||
.to_return(status: 200, body: "", headers: {})
|
||||
|
||||
Publisher.new(@admin).publish(
|
||||
title: "T", message: "M", priority: 3, tags: [],
|
||||
click: "https://app.example.com/admin/gateways"
|
||||
)
|
||||
assert_requested stub
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user