2022-12-20 04:17:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Jobs::SendPushNotification do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2022-12-20 04:17:40 +08: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 21:25:15 +08: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-20 04:17:40 +08:00
|
|
|
|
|
|
|
PushNotificationPusher.expects(:push).with(user, payload).never
|
|
|
|
|
|
|
|
Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)
|
|
|
|
end
|
|
|
|
|
2024-08-02 21:25:15 +08:00
|
|
|
it "sends push notification when user is offline" do
|
|
|
|
user.update!(last_seen_at: 20.minutes.ago)
|
2022-12-20 04:17:40 +08:00
|
|
|
|
|
|
|
PushNotificationPusher.expects(:push).with(user, payload)
|
|
|
|
|
|
|
|
Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)
|
|
|
|
end
|
|
|
|
end
|
2024-08-02 21:25:15 +08: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-20 04:17:40 +08:00
|
|
|
end
|