2022-12-19 15:17:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Jobs::SendPushNotification do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2022-12-19 15:17:40 -05:00
|
|
|
fab!(:subscription) { Fabricate(:push_subscription) }
|
|
|
|
let(:payload) { { notification_type: 1, excerpt: "Hello you" } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
freeze_time
|
|
|
|
SiteSetting.push_notification_time_window_mins = 10
|
|
|
|
end
|
|
|
|
|
2024-08-02 09:25:15 -04:00
|
|
|
context "with valid user" do
|
|
|
|
it "does not send push notification when user is online" do
|
|
|
|
user.update!(last_seen_at: 2.minutes.ago)
|
2022-12-19 15:17:40 -05:00
|
|
|
|
|
|
|
PushNotificationPusher.expects(:push).with(user, payload).never
|
|
|
|
|
|
|
|
Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)
|
|
|
|
end
|
|
|
|
|
2024-08-02 09:25:15 -04:00
|
|
|
it "sends push notification when user is offline" do
|
|
|
|
user.update!(last_seen_at: 20.minutes.ago)
|
2022-12-19 15:17:40 -05:00
|
|
|
|
|
|
|
PushNotificationPusher.expects(:push).with(user, payload)
|
|
|
|
|
|
|
|
Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)
|
|
|
|
end
|
|
|
|
end
|
2024-08-02 09:25:15 -04:00
|
|
|
|
|
|
|
context "with invalid user" do
|
|
|
|
it "does not send push notification" do
|
|
|
|
PushNotificationPusher.expects(:push).with(user, payload).never
|
|
|
|
|
|
|
|
Jobs::SendPushNotification.new.execute(user_id: -999, payload: payload)
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 15:17:40 -05:00
|
|
|
end
|