2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-05 06:31:48 +08:00
|
|
|
RSpec.describe PushNotificationPusher do
|
|
|
|
it "returns badges url by default" do
|
|
|
|
expect(PushNotificationPusher.get_badge).to eq("/assets/push-notifications/discourse.png")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns custom badges url" do
|
2019-01-02 15:29:17 +08:00
|
|
|
upload = Fabricate(:upload)
|
|
|
|
SiteSetting.push_notifications_icon = upload
|
|
|
|
|
|
|
|
expect(PushNotificationPusher.get_badge).to eq(UrlHelper.absolute(upload.url))
|
2018-05-05 06:31:48 +08:00
|
|
|
end
|
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
context "with user" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2023-11-07 00:13:23 +08:00
|
|
|
let(:topic_title) { "Topic" }
|
2024-01-23 05:30:35 +08:00
|
|
|
let(:post_url) { "https://example.com/t/1/2" }
|
2023-11-07 00:13:23 +08:00
|
|
|
let(:username) { "system" }
|
2021-05-27 04:49:20 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
def create_subscription
|
|
|
|
data = <<~JSON
|
2021-05-27 04:49:20 +08:00
|
|
|
{
|
|
|
|
"endpoint": "endpoint",
|
|
|
|
"keys": {
|
|
|
|
"p256dh": "p256dh",
|
|
|
|
"auth": "auth"
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 02:46:07 +08:00
|
|
|
JSON
|
|
|
|
PushSubscription.create!(user_id: user.id, data: data)
|
|
|
|
end
|
2021-05-27 04:49:20 +08:00
|
|
|
|
2023-11-07 00:13:23 +08:00
|
|
|
def execute_push(notification_type: 1, post_number: 1)
|
2021-05-27 04:49:20 +08:00
|
|
|
PushNotificationPusher.push(
|
|
|
|
user,
|
|
|
|
{
|
2023-11-07 00:13:23 +08:00
|
|
|
topic_title: topic_title,
|
|
|
|
username: username,
|
2021-05-27 04:49:20 +08:00
|
|
|
excerpt: "description",
|
|
|
|
topic_id: 1,
|
2024-01-23 05:30:35 +08:00
|
|
|
post_url: post_url,
|
2022-06-01 10:00:05 +08:00
|
|
|
notification_type: notification_type,
|
2023-11-07 00:13:23 +08:00
|
|
|
post_number: post_number,
|
2021-05-27 04:49:20 +08:00
|
|
|
},
|
|
|
|
)
|
2021-06-08 02:46:07 +08:00
|
|
|
end
|
2021-05-27 04:49:20 +08:00
|
|
|
|
2022-06-01 10:00:05 +08:00
|
|
|
it "correctly guesses an image if missing" do
|
|
|
|
message = execute_push(notification_type: -1)
|
|
|
|
expect(message[:icon]).to eq("/assets/push-notifications/discourse.png")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly finds image if exists" do
|
|
|
|
message = execute_push(notification_type: 1)
|
|
|
|
expect(message[:icon]).to eq("/assets/push-notifications/mentioned.png")
|
|
|
|
end
|
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
it "sends notification in user's locale" do
|
|
|
|
SiteSetting.allow_user_locale = true
|
|
|
|
user.update!(locale: "pt_BR")
|
|
|
|
|
2021-07-20 22:03:20 +08:00
|
|
|
TranslationOverride.upsert!(
|
|
|
|
"pt_BR",
|
|
|
|
"discourse_push_notifications.popup.mentioned",
|
|
|
|
"pt_BR notification",
|
|
|
|
)
|
|
|
|
|
2022-12-28 02:28:13 +08:00
|
|
|
WebPush
|
|
|
|
.expects(:payload_send)
|
2021-07-20 22:03:20 +08:00
|
|
|
.with { |*args| JSON.parse(args.first[:message])["title"] == "pt_BR notification" }
|
2021-06-08 02:46:07 +08:00
|
|
|
.once
|
|
|
|
|
|
|
|
create_subscription
|
|
|
|
execute_push
|
2021-05-27 04:49:20 +08:00
|
|
|
end
|
|
|
|
|
2024-01-23 05:30:35 +08:00
|
|
|
it "triggers a DiscourseEvent with user and message arguments" do
|
|
|
|
WebPush.expects(:payload_send)
|
|
|
|
create_subscription
|
|
|
|
pn_sent_event = DiscourseEvent.track_events { message = execute_push }.first
|
|
|
|
|
|
|
|
expect(pn_sent_event[:event_name]).to eq(:push_notification_sent)
|
|
|
|
expect(pn_sent_event[:params].first).to eq(user)
|
|
|
|
expect(pn_sent_event[:params].second[:url]).to eq(post_url)
|
|
|
|
end
|
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
it "deletes subscriptions which are erroring regularly" do
|
|
|
|
start = freeze_time
|
2021-05-27 04:49:20 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
sub = create_subscription
|
2021-02-26 02:10:37 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
response = Struct.new(:body, :inspect, :message).new("test", "test", "failed")
|
2022-12-28 02:28:13 +08:00
|
|
|
error = WebPush::ResponseError.new(response, "localhost")
|
2021-05-27 04:49:20 +08:00
|
|
|
|
2022-12-28 02:28:13 +08:00
|
|
|
WebPush.expects(:payload_send).raises(error).times(4)
|
2021-06-08 02:46:07 +08:00
|
|
|
|
|
|
|
# 3 failures in more than 24 hours
|
|
|
|
3.times do
|
|
|
|
execute_push
|
|
|
|
|
|
|
|
freeze_time 1.minute.from_now
|
|
|
|
end
|
|
|
|
|
|
|
|
sub.reload
|
|
|
|
expect(sub.error_count).to eq(3)
|
|
|
|
expect(sub.first_error_at).to eq_time(start)
|
|
|
|
|
|
|
|
freeze_time(2.days.from_now)
|
2021-02-26 02:10:37 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
execute_push
|
2021-03-19 21:24:03 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
expect(PushSubscription.where(id: sub.id).exists?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes invalid subscriptions during send" do
|
|
|
|
missing_endpoint =
|
|
|
|
PushSubscription.create!(
|
|
|
|
user_id: user.id,
|
|
|
|
data: { p256dh: "public ECDH key", keys: { auth: "private ECDH key" } }.to_json,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
missing_p256dh =
|
|
|
|
PushSubscription.create!(
|
|
|
|
user_id: user.id,
|
|
|
|
data: { endpoint: "endpoint 1", keys: { auth: "private ECDH key" } }.to_json,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
missing_auth =
|
|
|
|
PushSubscription.create!(
|
|
|
|
user_id: user.id,
|
|
|
|
data: { endpoint: "endpoint 2", keys: { p256dh: "public ECDH key" } }.to_json,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
valid_subscription =
|
|
|
|
PushSubscription.create!(
|
|
|
|
user_id: user.id,
|
2023-01-09 19:18:21 +08:00
|
|
|
data: {
|
2021-06-08 02:46:07 +08:00
|
|
|
endpoint: "endpoint 3",
|
2023-01-09 19:18:21 +08:00
|
|
|
keys: {
|
2021-06-08 02:46:07 +08:00
|
|
|
p256dh: "public ECDH key",
|
|
|
|
auth: "private ECDH key",
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2021-06-08 02:46:07 +08:00
|
|
|
}.to_json,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
expect(PushSubscription.where(user_id: user.id)).to contain_exactly(
|
|
|
|
missing_endpoint,
|
|
|
|
missing_p256dh,
|
|
|
|
missing_auth,
|
2022-12-28 02:28:13 +08:00
|
|
|
valid_subscription,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2022-12-28 02:28:13 +08:00
|
|
|
WebPush
|
|
|
|
.expects(:payload_send)
|
|
|
|
.with(
|
|
|
|
has_entries(endpoint: "endpoint 3", p256dh: "public ECDH key", auth: "private ECDH key"),
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2022-12-28 02:28:13 +08:00
|
|
|
.once
|
2021-03-19 21:24:03 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
execute_push
|
|
|
|
|
|
|
|
expect(PushSubscription.where(user_id: user.id)).to contain_exactly(valid_subscription)
|
|
|
|
end
|
2021-03-19 21:24:03 +08:00
|
|
|
|
2021-06-08 02:46:07 +08:00
|
|
|
it "handles timeouts" do
|
2022-12-28 02:28:13 +08:00
|
|
|
WebPush.expects(:payload_send).raises(Net::ReadTimeout.new)
|
2021-06-08 02:46:07 +08:00
|
|
|
subscription = create_subscription
|
|
|
|
|
|
|
|
expect { execute_push }.to_not raise_exception
|
|
|
|
|
|
|
|
subscription.reload
|
|
|
|
expect(subscription.error_count).to eq(1)
|
|
|
|
end
|
2023-11-07 00:13:23 +08:00
|
|
|
|
|
|
|
describe "`watching_category_or_tag` notifications" do
|
|
|
|
it "Uses the 'watching_first_post' translation when new topic was created" do
|
|
|
|
message =
|
|
|
|
execute_push(
|
|
|
|
notification_type: Notification.types[:watching_category_or_tag],
|
|
|
|
post_number: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message[:title]).to eq(
|
|
|
|
I18n.t(
|
|
|
|
"discourse_push_notifications.popup.watching_first_post",
|
|
|
|
site_title: SiteSetting.title,
|
|
|
|
topic: topic_title,
|
|
|
|
username: username,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "Uses the 'posted' translation when new post was created" do
|
|
|
|
message =
|
|
|
|
execute_push(
|
|
|
|
notification_type: Notification.types[:watching_category_or_tag],
|
|
|
|
post_number: 2,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message[:title]).to eq(
|
|
|
|
I18n.t(
|
|
|
|
"discourse_push_notifications.popup.posted",
|
|
|
|
site_title: SiteSetting.title,
|
|
|
|
topic: topic_title,
|
|
|
|
username: username,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-03-19 21:24:03 +08:00
|
|
|
end
|
2018-05-05 06:31:48 +08:00
|
|
|
end
|