docbrown/spec/models/consumer_app_spec.rb
Fernando Valverde e602d50d32
Push Notification multi app support (#13304)
* PushNotificationTarget model + /admin/push_notifications (index)

* Admin panel CRUD + request tests

* Migrate Redis backed Rpush model responsibilities into PushNotificationTarget

* Fix failing specs

* Fix conflicts + clean up test by using constant reference

* Removed unused policy

* policy + services + misc feedback

* PushNotificationTarget refactored to AppIntegration

* Review feedback

* Some small cleanup

* Refactor AppIntegration -> ConsumerApp

* Fixing specs

* Trigger Travis

* More naming refactor changes

* Refactor services into queries

* Revert to where(...).first, fix typo and tests

* Apply suggestions from code review

Co-authored-by: rhymes <rhymes@hey.com>

* PR review feedback - create_or_find_by, validations, renaming + more tests

* Fix aria-label text

* Remove unnecessary individual index - composite index will do

Co-authored-by: rhymes <rhymes@hey.com>
2021-04-21 17:13:01 -06:00

62 lines
2.2 KiB
Ruby

require "rails_helper"
RSpec.describe ConsumerApp, type: :model do
let!(:consumer_app) { create(:consumer_app, platform: Device::IOS) }
describe "operational?" do
context "with non-forem apps" do
it "returns false if not active in DB or credentials are unavailable" do
inactive_consumer_app = create(:consumer_app, active: false)
expect(inactive_consumer_app.operational?).to be false
consumer_app_without_credentials = create(:consumer_app, auth_key: nil)
expect(consumer_app_without_credentials.operational?).to be false
end
it "returns true if both active && credentials are available" do
expect(consumer_app.operational?).to be true
end
end
context "with forem apps" do
it "returns true/false based on the ENV variable for the forem apps" do
forem_consumer_app = ConsumerApps::FindOrCreateByQuery.call(
app_bundle: ConsumerApp::FOREM_BUNDLE,
platform: ConsumerApp::FOREM_APP_PLATFORMS.sample,
)
allow(ApplicationConfig).to receive(:[]).with("RPUSH_IOS_PEM").and_return("asdf123")
expect(forem_consumer_app.operational?).to be true
allow(ApplicationConfig).to receive(:[]).with("RPUSH_IOS_PEM").and_return(nil)
expect(forem_consumer_app.operational?).to be false
end
end
end
describe "after an update" do
it "recreates the Redis-backed Rpush app" do
rpush_app = ConsumerApps::RpushAppQuery.call(
app_bundle: consumer_app.app_bundle,
platform: consumer_app.platform,
)
auth_key = rpush_app.certificate
# The Redis-backed Rpush App has the ConsumerApp's auth_credentials
expect(rpush_app).to be_instance_of(Rpush::Apns2::App)
expect(auth_key).to eq(consumer_app.auth_credentials)
consumer_app.auth_key = "new_auth_key"
expect(consumer_app.save).to be true
# After update fetch again
rpush_app = ConsumerApps::RpushAppQuery.call(
app_bundle: consumer_app.app_bundle,
platform: consumer_app.platform,
)
# The Redis-backed Rpush App has the new ConsumerApp's auth_credentials
expect(rpush_app).to be_instance_of(Rpush::Apns2::App)
expect(rpush_app.certificate).to eq("new_auth_key")
end
end
end