2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-12 06:47:28 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe BroadcastMessage do
|
2016-11-18 17:22:02 -05:00
|
|
|
subject { build(:broadcast_message) }
|
2013-11-12 06:47:28 -05:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to be_valid }
|
2013-11-12 08:08:20 -05:00
|
|
|
|
2015-12-01 18:53:44 -05:00
|
|
|
describe 'validations' do
|
|
|
|
let(:triplet) { '#000' }
|
|
|
|
let(:hex) { '#AABBCC' }
|
|
|
|
|
|
|
|
it { is_expected.to allow_value(nil).for(:color) }
|
|
|
|
it { is_expected.to allow_value(triplet).for(:color) }
|
|
|
|
it { is_expected.to allow_value(hex).for(:color) }
|
|
|
|
it { is_expected.not_to allow_value('000').for(:color) }
|
|
|
|
|
|
|
|
it { is_expected.to allow_value(nil).for(:font) }
|
|
|
|
it { is_expected.to allow_value(triplet).for(:font) }
|
|
|
|
it { is_expected.to allow_value(hex).for(:font) }
|
|
|
|
it { is_expected.not_to allow_value('000').for(:font) }
|
2019-12-10 10:07:52 -05:00
|
|
|
|
|
|
|
it { is_expected.to allow_value(1).for(:broadcast_type) }
|
|
|
|
it { is_expected.not_to allow_value(nil).for(:broadcast_type) }
|
2015-12-01 18:53:44 -05:00
|
|
|
end
|
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
shared_examples 'time constrainted' do |broadcast_type|
|
2017-06-15 09:47:33 -04:00
|
|
|
it 'returns message if time match' do
|
2019-12-10 10:07:52 -05:00
|
|
|
message = create(:broadcast_message, broadcast_type: broadcast_type)
|
2015-12-31 17:07:11 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call).to include(message)
|
2013-11-12 08:08:20 -05:00
|
|
|
end
|
|
|
|
|
2017-06-15 09:47:33 -04:00
|
|
|
it 'returns multiple messages if time match' do
|
2019-12-10 10:07:52 -05:00
|
|
|
message1 = create(:broadcast_message, broadcast_type: broadcast_type)
|
|
|
|
message2 = create(:broadcast_message, broadcast_type: broadcast_type)
|
2017-06-15 09:47:33 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call).to contain_exactly(message1, message2)
|
2017-06-15 09:47:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns empty list if time not come' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, :future, broadcast_type: broadcast_type)
|
2015-12-31 17:07:11 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call).to be_empty
|
2013-11-12 08:08:20 -05:00
|
|
|
end
|
|
|
|
|
2017-06-15 09:47:33 -04:00
|
|
|
it 'returns empty list if time has passed' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, :expired, broadcast_type: broadcast_type)
|
2015-12-31 17:07:11 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call).to be_empty
|
2013-11-12 08:08:20 -05:00
|
|
|
end
|
2019-12-10 10:07:52 -05:00
|
|
|
end
|
2017-08-09 09:49:30 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
shared_examples 'message cache' do |broadcast_type|
|
2019-06-25 18:41:05 -04:00
|
|
|
it 'caches the output of the query for two weeks' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, broadcast_type: broadcast_type)
|
2017-08-09 09:49:30 -04:00
|
|
|
|
2019-06-25 18:41:05 -04:00
|
|
|
expect(described_class).to receive(:current_and_future_messages).and_call_original.twice
|
2017-08-09 09:49:30 -04:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
subject.call
|
2018-04-11 13:49:56 -04:00
|
|
|
|
2019-06-25 18:41:05 -04:00
|
|
|
Timecop.travel(3.weeks) do
|
2019-12-10 10:07:52 -05:00
|
|
|
subject.call
|
2018-04-11 13:49:56 -04:00
|
|
|
end
|
2017-08-09 09:49:30 -04:00
|
|
|
end
|
2017-08-18 08:52:11 -04:00
|
|
|
|
2020-03-05 13:08:19 -05:00
|
|
|
it 'expires the value if a broadcast message has ended', :request_store do
|
2020-05-22 05:08:09 -04:00
|
|
|
message = create(:broadcast_message, broadcast_type: broadcast_type, ends_at: Time.current.utc + 1.day)
|
2020-03-05 13:08:19 -05:00
|
|
|
|
|
|
|
expect(subject.call).to match_array([message])
|
|
|
|
expect(described_class.cache).to receive(:expire).and_call_original
|
|
|
|
|
|
|
|
Timecop.travel(1.week) do
|
|
|
|
2.times { expect(subject.call).to be_empty }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-07 14:16:45 -05:00
|
|
|
it 'does not create new records' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, broadcast_type: broadcast_type)
|
2018-12-07 14:16:45 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect { subject.call }.not_to change { described_class.count }
|
2018-12-07 14:16:45 -05:00
|
|
|
end
|
|
|
|
|
2017-08-18 08:52:11 -04:00
|
|
|
it 'includes messages that need to be displayed in the future' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, broadcast_type: broadcast_type)
|
2017-08-18 08:52:11 -04:00
|
|
|
|
|
|
|
future = create(
|
|
|
|
:broadcast_message,
|
2020-05-22 05:08:09 -04:00
|
|
|
starts_at: Time.current + 10.minutes,
|
|
|
|
ends_at: Time.current + 20.minutes,
|
2019-12-10 10:07:52 -05:00
|
|
|
broadcast_type: broadcast_type
|
2017-08-18 08:52:11 -04:00
|
|
|
)
|
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call.length).to eq(1)
|
2017-08-18 08:52:11 -04:00
|
|
|
|
|
|
|
Timecop.travel(future.starts_at) do
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call.length).to eq(2)
|
2017-08-18 08:52:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not clear the cache if only a future message should be displayed' do
|
|
|
|
create(:broadcast_message, :future)
|
|
|
|
|
2018-12-07 14:16:45 -05:00
|
|
|
expect(Rails.cache).not_to receive(:delete).with(described_class::CACHE_KEY)
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call.length).to eq(0)
|
2017-08-18 08:52:11 -04:00
|
|
|
end
|
2019-12-10 10:07:52 -05:00
|
|
|
end
|
2019-12-02 10:06:36 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
shared_examples "matches with current path" do |broadcast_type|
|
2019-12-02 10:06:36 -05:00
|
|
|
it 'returns message if it matches the target path' do
|
2019-12-10 10:07:52 -05:00
|
|
|
message = create(:broadcast_message, target_path: "*/onboarding_completed", broadcast_type: broadcast_type)
|
2019-12-02 10:06:36 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call('/users/onboarding_completed')).to include(message)
|
2019-12-02 10:06:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns message if part of the target path matches' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, target_path: "/users/*/issues", broadcast_type: broadcast_type)
|
2019-12-02 10:06:36 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call('/users/name/issues').length).to eq(1)
|
2019-12-02 10:06:36 -05:00
|
|
|
end
|
|
|
|
|
2021-04-22 05:09:45 -04:00
|
|
|
it 'returns message if provided a path without a preceding slash' do
|
|
|
|
create(:broadcast_message, target_path: "/users/*/issues", broadcast_type: broadcast_type)
|
|
|
|
|
|
|
|
expect(subject.call('users/name/issues').length).to eq(1)
|
|
|
|
end
|
|
|
|
|
2019-12-02 10:06:36 -05:00
|
|
|
it 'returns the message for empty target path' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, target_path: "", broadcast_type: broadcast_type)
|
2019-12-02 10:06:36 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call('/users/name/issues').length).to eq(1)
|
2019-12-02 10:06:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the message if target path is nil' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, target_path: nil, broadcast_type: broadcast_type)
|
2019-12-02 10:06:36 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call('/users/name/issues').length).to eq(1)
|
2019-12-02 10:06:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return message if target path does not match' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, target_path: "/onboarding_completed", broadcast_type: broadcast_type)
|
2019-12-02 10:06:36 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call('/welcome').length).to eq(0)
|
2019-12-02 10:06:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return message if target path does not match when using wildcard' do
|
2019-12-10 10:07:52 -05:00
|
|
|
create(:broadcast_message, target_path: "/users/*/issues", broadcast_type: broadcast_type)
|
|
|
|
|
|
|
|
expect(subject.call('/group/groupname/issues').length).to eq(0)
|
|
|
|
end
|
2020-04-24 05:09:44 -04:00
|
|
|
|
|
|
|
it 'does not return message if target path has no wild card at the end' do
|
|
|
|
create(:broadcast_message, target_path: "*/issues", broadcast_type: broadcast_type)
|
|
|
|
|
|
|
|
expect(subject.call('/group/issues/test').length).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return message if target path has wild card at the end' do
|
|
|
|
create(:broadcast_message, target_path: "/issues/*", broadcast_type: broadcast_type)
|
|
|
|
|
|
|
|
expect(subject.call('/group/issues/test').length).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does return message if target path has wild card at the beginning and the end' do
|
|
|
|
create(:broadcast_message, target_path: "*/issues/*", broadcast_type: broadcast_type)
|
|
|
|
|
|
|
|
expect(subject.call('/group/issues/test').length).to eq(1)
|
|
|
|
end
|
2020-10-28 11:08:49 -04:00
|
|
|
|
|
|
|
it "does not return message if the target path is set but no current path is provided" do
|
|
|
|
create(:broadcast_message, target_path: "*/issues/*", broadcast_type: broadcast_type)
|
|
|
|
|
|
|
|
expect(subject.call.length).to eq(0)
|
|
|
|
end
|
2019-12-10 10:07:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.current', :use_clean_rails_memory_store_caching do
|
|
|
|
subject { -> (path = nil) { described_class.current(path) } }
|
|
|
|
|
|
|
|
it_behaves_like 'time constrainted', :banner
|
|
|
|
it_behaves_like 'message cache', :banner
|
|
|
|
it_behaves_like 'matches with current path', :banner
|
|
|
|
|
|
|
|
it 'returns both types' do
|
|
|
|
banner_message = create(:broadcast_message, broadcast_type: :banner)
|
|
|
|
notification_message = create(:broadcast_message, broadcast_type: :notification)
|
|
|
|
|
|
|
|
expect(subject.call).to contain_exactly(banner_message, notification_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.current_banner_messages', :use_clean_rails_memory_store_caching do
|
|
|
|
subject { -> (path = nil) { described_class.current_banner_messages(path) } }
|
|
|
|
|
|
|
|
it_behaves_like 'time constrainted', :banner
|
|
|
|
it_behaves_like 'message cache', :banner
|
|
|
|
it_behaves_like 'matches with current path', :banner
|
|
|
|
|
|
|
|
it 'only returns banners' do
|
|
|
|
banner_message = create(:broadcast_message, broadcast_type: :banner)
|
|
|
|
create(:broadcast_message, broadcast_type: :notification)
|
|
|
|
|
|
|
|
expect(subject.call).to contain_exactly(banner_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.current_notification_messages', :use_clean_rails_memory_store_caching do
|
|
|
|
subject { -> (path = nil) { described_class.current_notification_messages(path) } }
|
|
|
|
|
|
|
|
it_behaves_like 'time constrainted', :notification
|
|
|
|
it_behaves_like 'message cache', :notification
|
|
|
|
it_behaves_like 'matches with current path', :notification
|
|
|
|
|
|
|
|
it 'only returns notifications' do
|
|
|
|
notification_message = create(:broadcast_message, broadcast_type: :notification)
|
|
|
|
create(:broadcast_message, broadcast_type: :banner)
|
2019-12-02 10:06:36 -05:00
|
|
|
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(subject.call).to contain_exactly(notification_message)
|
2019-12-02 10:06:36 -05:00
|
|
|
end
|
2013-11-12 08:08:20 -05:00
|
|
|
end
|
2015-12-31 17:07:11 -05:00
|
|
|
|
2019-03-20 14:43:40 -04:00
|
|
|
describe '#attributes' do
|
|
|
|
it 'includes message_html field' do
|
|
|
|
expect(subject.attributes.keys).to include("cached_markdown_version", "message_html")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-31 17:07:11 -05:00
|
|
|
describe '#active?' do
|
|
|
|
it 'is truthy when started and not ended' do
|
|
|
|
message = build(:broadcast_message)
|
|
|
|
|
|
|
|
expect(message).to be_active
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is falsey when ended' do
|
|
|
|
message = build(:broadcast_message, :expired)
|
|
|
|
|
|
|
|
expect(message).not_to be_active
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is falsey when not started' do
|
|
|
|
message = build(:broadcast_message, :future)
|
|
|
|
|
|
|
|
expect(message).not_to be_active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#started?' do
|
|
|
|
it 'is truthy when starts_at has passed' do
|
|
|
|
message = build(:broadcast_message)
|
|
|
|
|
|
|
|
travel_to(3.days.from_now) do
|
|
|
|
expect(message).to be_started
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is falsey when starts_at is in the future' do
|
|
|
|
message = build(:broadcast_message)
|
|
|
|
|
|
|
|
travel_to(3.days.ago) do
|
|
|
|
expect(message).not_to be_started
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ended?' do
|
|
|
|
it 'is truthy when ends_at has passed' do
|
|
|
|
message = build(:broadcast_message)
|
|
|
|
|
|
|
|
travel_to(3.days.from_now) do
|
|
|
|
expect(message).to be_ended
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is falsey when ends_at is in the future' do
|
|
|
|
message = build(:broadcast_message)
|
|
|
|
|
|
|
|
travel_to(3.days.ago) do
|
|
|
|
expect(message).not_to be_ended
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-09 09:49:30 -04:00
|
|
|
|
|
|
|
describe '#flush_redis_cache' do
|
|
|
|
it 'flushes the Redis cache' do
|
|
|
|
message = create(:broadcast_message)
|
|
|
|
|
|
|
|
expect(Rails.cache).to receive(:delete).with(described_class::CACHE_KEY)
|
2019-12-10 10:07:52 -05:00
|
|
|
expect(Rails.cache).to receive(:delete).with(described_class::BANNER_CACHE_KEY)
|
|
|
|
expect(Rails.cache).to receive(:delete).with(described_class::NOTIFICATION_CACHE_KEY)
|
2017-08-09 09:49:30 -04:00
|
|
|
|
|
|
|
message.flush_redis_cache
|
|
|
|
end
|
|
|
|
end
|
2013-11-12 06:47:28 -05:00
|
|
|
end
|