Fix Rpush app when creating Android notification (#15529)

* Fix Rpush app when creating Android notification

* Include Android devices in Service spec

* Apply suggestions from code review

Co-authored-by: Michael Kohl <citizen428@forem.com>

Co-authored-by: Michael Kohl <citizen428@forem.com>
This commit is contained in:
Fernando Valverde 2021-11-29 22:35:40 -06:00 committed by GitHub
parent 334a4ec30d
commit 7caafc8ea7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View file

@ -52,7 +52,10 @@ class Device < ApplicationRecord
def android_notification(title, body, payload) def android_notification(title, body, payload)
n = Rpush::Gcm::Notification.new n = Rpush::Gcm::Notification.new
n.app = ConsumerApp.rpush_app(app_bundle: app_bundle, platform: platform) n.app = ConsumerApps::RpushAppQuery.call(
app_bundle: consumer_app.app_bundle,
platform: platform,
)
n.registration_ids = [token] n.registration_ids = [token]
n.priority = "high" n.priority = "high"
n.content_available = true n.content_available = true

View file

@ -22,6 +22,7 @@ RSpec.describe PushNotifications::Send, type: :service do
before do before do
allow(ApplicationConfig).to receive(:[]).with("RPUSH_IOS_PEM").and_return("dGVzdGluZw==") allow(ApplicationConfig).to receive(:[]).with("RPUSH_IOS_PEM").and_return("dGVzdGluZw==")
allow(ApplicationConfig).to receive(:[]).with("RPUSH_FCM_KEY").and_return("dGVzdGluZw==")
allow(ApplicationConfig).to receive(:[]).and_return("stub") allow(ApplicationConfig).to receive(:[]).and_return("stub")
end end
@ -59,31 +60,33 @@ RSpec.describe PushNotifications::Send, type: :service do
end end
context "with devices for multiple users" do context "with devices for multiple users" do
let(:consumer_app) { create(:consumer_app) } let(:consumer_app_ios) { create(:consumer_app, platform: Device::IOS) }
let(:consumer_app_android) { create(:consumer_app, platform: Device::ANDROID) }
before do before do
create(:device, user: user, consumer_app: consumer_app) create(:device, user: user, consumer_app: consumer_app_ios)
create(:device, user: user2, consumer_app: consumer_app) create(:device, user: user2, consumer_app: consumer_app_ios)
create(:device, user: user2, consumer_app: consumer_app_android, platform: Device::ANDROID)
end end
it "creates a notification and enqueues it" do it "creates a notification and enqueues it" do
mocked_objects = mock_rpush(consumer_app) mocked_objects = mock_rpush(consumer_app_ios)
described_class.call(**many_targets_params) described_class.call(**params)
expect(mocked_objects[:rpush_notification]).to have_received(:save!).exactly(2).times expect(mocked_objects[:rpush_notification]).to have_received(:save!).once
sidekiq_assert_enqueued_jobs(1, only: PushNotifications::DeliverWorker) sidekiq_assert_enqueued_jobs(1, only: PushNotifications::DeliverWorker)
end end
it "creates a single notification for each of the user's devices when they have multiple" do it "creates a single notification for each of the user's devices when they have multiple" do
create(:device, user: user) ios_mocked_objects = mock_rpush(consumer_app_ios)
android_mocked_objects = mock_rpush(consumer_app_android)
mocked_objects = mock_rpush(consumer_app)
described_class.call(**many_targets_params) described_class.call(**many_targets_params)
expect(mocked_objects[:rpush_notification]).to have_received(:save!).exactly(3).times expect(ios_mocked_objects[:rpush_notification]).to have_received(:save!).twice
expect(android_mocked_objects[:rpush_notification]).to have_received(:save!).once
sidekiq_assert_enqueued_jobs(1, only: PushNotifications::DeliverWorker) sidekiq_assert_enqueued_jobs(1, only: PushNotifications::DeliverWorker)
end end