discourse/spec/jobs/push_notification_spec.rb
David Battersby 0954ae70a6
FEATURE: add delay to native push notifications (#28314)
This change ensures native push notifications respect the site setting for push_notification_time_window_mins. Previously only web push notifications would account for the delay, now we can bring more consistency between Discourse in browser vs Hub, by applying the same delay strategy to both forms of push notifications.
2024-08-13 12:12:05 +04:00

50 lines
1.1 KiB
Ruby

# frozen_string_literal: true
require "excon"
RSpec.describe Jobs::PushNotification do
fab!(:user)
fab!(:post)
let(:data) do
{
"user_id" => user.id,
"payload" => {
"notification_type" => 1,
"post_url" => "/t/#{post.topic_id}/#{post.post_number}",
"excerpt" => "Hello you",
},
"clients" => [[user.id, "http://test.localhost"]],
}
end
before { SiteSetting.push_notification_time_window_mins = 5 }
context "with valid user" do
it "does not send push notification when user is online" do
user.update!(last_seen_at: 1.minute.ago)
Excon.expects(:post).never
Jobs::PushNotification.new.execute(data)
end
it "sends push notification when user is offline" do
user.update!(last_seen_at: 10.minutes.ago)
Excon.expects(:post).once
Jobs::PushNotification.new.execute(data)
end
end
context "with invalid user" do
it "does not send push notification" do
data["user_id"] = -999
Excon.expects(:post).never
Jobs::PushNotification.new.execute(data)
end
end
end