Files
moreminimore-chat/spec/services/notification/push_test_service_spec.rb
2026-08-15 16:04:25 +07:00

78 lines
3.0 KiB
Ruby

require 'rails_helper'
describe Notification::PushTestService do
let!(:user) { create(:user) }
let!(:subscription) do
create(
:notification_subscription,
:fcm,
user: user,
identifier: 'fcm-test',
subscription_attributes: { 'push_token' => 'test-token', 'device_id' => 'device-123456' }
)
end
let(:fcm_double) { instance_double(FCM) }
let(:fcm_service_double) { instance_double(Notification::FcmService, fcm_client: fcm_double) }
describe '#perform' do
context 'when Firebase credentials are not configured' do
before do
allow(GlobalConfigService).to receive(:load).and_return(nil)
allow(GlobalConfigService).to receive(:load).with('FIREBASE_PROJECT_ID', nil).and_return(nil)
allow(GlobalConfigService).to receive(:load).with('FIREBASE_CREDENTIALS', nil).and_return(nil)
end
it 'skips FCM without using the Chatwoot Hub relay' do
result = described_class.new(user: user, subscription_ids: [subscription.id]).perform.first
expect(result).to include(
type: 'fcm',
status: :skipped,
message: 'Firebase credentials are not configured; no remote push relay is used'
)
end
end
context 'when Firebase credentials are configured' do
before do
allow(GlobalConfigService).to receive(:load).and_return(nil)
allow(Notification::FcmService).to receive(:new).and_return(fcm_service_double)
allow(fcm_double).to receive(:send_v1).and_return(status_code: 200, body: '{}')
allow(GlobalConfigService).to receive(:load).with('FIREBASE_PROJECT_ID', nil).and_return('test-project')
allow(GlobalConfigService).to receive(:load).with('FIREBASE_CREDENTIALS', nil).and_return('test-credentials')
end
it 'sends FCM directly without using the Chatwoot Hub relay' do
result = described_class.new(user: user, subscription_ids: [subscription.id]).perform.first
expect(Notification::FcmService).to have_received(:new)
expect(fcm_double).to have_received(:send_v1)
expect(result[:status]).to eq(:success)
end
end
it 'keeps browser WebPush available' do
browser_subscription = create(
:notification_subscription,
:browser_push,
user: user,
identifier: 'browser-test',
subscription_attributes: {
'endpoint' => 'https://push.example.test/endpoint',
'p256dh' => 'test-p256dh',
'auth' => 'test-auth'
}
)
allow(VapidService).to receive(:public_key).and_return('test-public-key')
allow(VapidService).to receive(:private_key).and_return('test-private-key')
allow(WebPush).to receive(:payload_send).and_return(true)
result = described_class.new(user: user, subscription_ids: [browser_subscription.id]).perform.first
expect(WebPush).to have_received(:payload_send)
expect(result[:type]).to eq('browser_push')
expect(result[:status]).to eq(:success)
end
end
end