docbrown/spec/services/push_notifications/send_spec.rb
Fernando Valverde d6fde3ab89
Change PushNotifications::Send to use user_ids (#13036)
* Changes PushNotifications::Send to use user_ids

* Changes devices#destroy to use unauthenticated_params

* Fix failing spec - new logic reverses order

* Apply suggestions from code review

Co-authored-by: Jamie Gaskins <jgaskins@gmail.com>

* Only send new_comment PN with Rpush behind FeatureFlag + spec tweak

* Add query order explicitly to avoid flakyness

* Review feedback - remove order clause in query & other tweaks

Co-authored-by: Jamie Gaskins <jgaskins@gmail.com>
2021-03-19 13:50:04 -06:00

91 lines
3.1 KiB
Ruby

require "rails_helper"
RSpec.describe PushNotifications::Send, type: :service do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:params) do
{
user_ids: [user.id],
title: "Alert",
body: "some alert here",
payload: ""
}
end
let(:many_targets_params) do
{
user_ids: [user.id, user2.id],
title: "Alert 2",
body: "some other alert",
payload: ""
}
end
context "with feature flag set to false/off" do
before { allow(FeatureFlag).to receive(:enabled?).with(:mobile_notifications).and_return(false) }
it "does nothing if the feature flag is disabled" do
expect { described_class.call(params) }
.not_to change { Rpush::Client::Redis::Notification.all.count }
end
end
context "with no devices for user" do
before do
allow(FeatureFlag).to receive(:enabled?).with(:mobile_notifications).and_return(true)
user.devices.delete
end
it "does nothing", :aggregate_failures do
expect(user.devices.count).to eq(0)
expect { described_class.call(params) }
.not_to change { Rpush::Client::Redis::Notification.all.count }
end
end
context "with devices for one user" do
before do
allow(FeatureFlag).to receive(:enabled?).with(:mobile_notifications).and_return(true)
allow(ApplicationConfig).to receive(:[]).with("RPUSH_IOS_PEM").and_return("dGVzdGluZw==")
allow(ApplicationConfig).to receive(:[]).with("COMMUNITY_NAME").and_return("Forem")
create(:device, user: user)
end
it "creates a notification and enqueues it" do
expect { described_class.call(params) }
.to change { Rpush::Client::Redis::Notification.all.count }.by(1)
.and change(PushNotifications::DeliverWorker.jobs, :size).by(1)
end
it "creates a single notification for each of the user's devices when they have multiple" do
create(:device, user: user)
expect { described_class.call(params) }
.to change { Rpush::Client::Redis::Notification.all.count }.by(2)
.and change(PushNotifications::DeliverWorker.jobs, :size).by(1)
end
end
context "with devices for multiple users" do
before do
allow(FeatureFlag).to receive(:enabled?).with(:mobile_notifications).and_return(true)
allow(ApplicationConfig).to receive(:[]).with("RPUSH_IOS_PEM").and_return("dGVzdGluZw==")
allow(ApplicationConfig).to receive(:[]).with("COMMUNITY_NAME").and_return("Forem")
create(:device, user: user)
create(:device, user: user2)
end
it "creates a notification and enqueues it" do
expect { described_class.call(many_targets_params) }
.to change { Rpush::Client::Redis::Notification.all.count }.by(2)
.and change { PushNotifications::DeliverWorker.jobs.size }.by(1)
end
it "creates a single notification for each of the user's devices when they have multiple" do
create(:device, user: user)
expect { described_class.call(many_targets_params) }
.to change { Rpush::Client::Redis::Notification.all.count }.by(3)
.and change { PushNotifications::DeliverWorker.jobs.size }.by(1)
end
end
end