2020-06-04 18:45:50 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Notification::PushNotificationService do
|
|
|
|
let!(:account) { create(:account) }
|
|
|
|
let!(:user) { create(:user, account: account) }
|
|
|
|
let!(:notification) { create(:notification, user: user, account: user.accounts.first) }
|
|
|
|
let(:fcm_double) { double }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Webpush).to receive(:payload_send).and_return(true)
|
|
|
|
allow(FCM).to receive(:new).and_return(fcm_double)
|
|
|
|
allow(fcm_double).to receive(:send).and_return({ body: { 'results': [] }.to_json })
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#perform' do
|
|
|
|
it 'sends webpush notifications for webpush subscription' do
|
2020-06-16 14:09:57 +00:00
|
|
|
ENV['VAPID_PUBLIC_KEY'] = 'test'
|
2020-06-04 18:45:50 +00:00
|
|
|
create(:notification_subscription, user: notification.user)
|
|
|
|
|
|
|
|
described_class.new(notification: notification).perform
|
|
|
|
expect(Webpush).to have_received(:payload_send)
|
|
|
|
expect(FCM).not_to have_received(:new)
|
2021-08-16 20:14:16 +00:00
|
|
|
ENV['VAPID_PUBLIC_KEY'] = nil
|
2020-06-04 18:45:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends a fcm notification for firebase subscription' do
|
2020-06-16 14:09:57 +00:00
|
|
|
ENV['FCM_SERVER_KEY'] = 'test'
|
2021-08-16 20:14:16 +00:00
|
|
|
ENV['ENABLE_PUSH_RELAY_SERVER'] = 'false'
|
2020-06-04 18:45:50 +00:00
|
|
|
create(:notification_subscription, user: notification.user, subscription_type: 'fcm')
|
|
|
|
|
|
|
|
described_class.new(notification: notification).perform
|
|
|
|
expect(FCM).to have_received(:new)
|
|
|
|
expect(Webpush).not_to have_received(:payload_send)
|
2021-08-16 20:14:16 +00:00
|
|
|
|
|
|
|
ENV['FCM_SERVER_KEY'] = nil
|
|
|
|
ENV['ENABLE_PUSH_RELAY_SERVER'] = nil
|
2020-06-04 18:45:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|