mirror of
https://github.com/discourse/discourse.git
synced 2024-12-02 05:03:40 +08:00
6ec8728ebf
This change is mainly a refactor of the desktop notifications service to improve readability and have standardised values for tracking state for current user in regards to the Notification API and Push API. Also improves readability when handling push notification jobs, especially in scenarios where the push_notification_time_window_mins site setting is set to 0, which will allow sending push notifications instantly.
39 lines
1.1 KiB
Ruby
39 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe Jobs::SendPushNotification do
|
|
fab!(:user)
|
|
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
|
|
|
|
context "with valid user" do
|
|
it "does not send push notification when user is online" do
|
|
user.update!(last_seen_at: 2.minutes.ago)
|
|
|
|
PushNotificationPusher.expects(:push).with(user, payload).never
|
|
|
|
Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)
|
|
end
|
|
|
|
it "sends push notification when user is offline" do
|
|
user.update!(last_seen_at: 20.minutes.ago)
|
|
|
|
PushNotificationPusher.expects(:push).with(user, payload)
|
|
|
|
Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)
|
|
end
|
|
end
|
|
|
|
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
|
|
end
|