Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 18:07:52 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 18:07:52 +0300
commit27d91a629918e417a9e87825e838209b9ace79c1 (patch)
treee066c3fc84e3011641e662252810cb2c240edb90 /spec/models/broadcast_message_spec.rb
parent5e11c9b77cb1b2b77ee29359047b55807afe255d (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models/broadcast_message_spec.rb')
-rw-r--r--spec/models/broadcast_message_spec.rb119
1 files changed, 87 insertions, 32 deletions
diff --git a/spec/models/broadcast_message_spec.rb b/spec/models/broadcast_message_spec.rb
index b06fa845777..67d8284bebe 100644
--- a/spec/models/broadcast_message_spec.rb
+++ b/spec/models/broadcast_message_spec.rb
@@ -20,65 +20,71 @@ describe BroadcastMessage do
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) }
+
+ it { is_expected.to allow_value(1).for(:broadcast_type) }
+ it { is_expected.not_to allow_value(nil).for(:broadcast_type) }
end
- describe '.current', :use_clean_rails_memory_store_caching do
+ shared_examples 'time constrainted' do |broadcast_type|
it 'returns message if time match' do
- message = create(:broadcast_message)
+ message = create(:broadcast_message, broadcast_type: broadcast_type)
- expect(described_class.current).to include(message)
+ expect(subject.call).to include(message)
end
it 'returns multiple messages if time match' do
- message1 = create(:broadcast_message)
- message2 = create(:broadcast_message)
+ message1 = create(:broadcast_message, broadcast_type: broadcast_type)
+ message2 = create(:broadcast_message, broadcast_type: broadcast_type)
- expect(described_class.current).to contain_exactly(message1, message2)
+ expect(subject.call).to contain_exactly(message1, message2)
end
it 'returns empty list if time not come' do
- create(:broadcast_message, :future)
+ create(:broadcast_message, :future, broadcast_type: broadcast_type)
- expect(described_class.current).to be_empty
+ expect(subject.call).to be_empty
end
it 'returns empty list if time has passed' do
- create(:broadcast_message, :expired)
+ create(:broadcast_message, :expired, broadcast_type: broadcast_type)
- expect(described_class.current).to be_empty
+ expect(subject.call).to be_empty
end
+ end
+ shared_examples 'message cache' do |broadcast_type|
it 'caches the output of the query for two weeks' do
- create(:broadcast_message)
+ create(:broadcast_message, broadcast_type: broadcast_type)
expect(described_class).to receive(:current_and_future_messages).and_call_original.twice
- described_class.current
+ subject.call
Timecop.travel(3.weeks) do
- described_class.current
+ subject.call
end
end
it 'does not create new records' do
- create(:broadcast_message)
+ create(:broadcast_message, broadcast_type: broadcast_type)
- expect { described_class.current }.not_to change { described_class.count }
+ expect { subject.call }.not_to change { described_class.count }
end
it 'includes messages that need to be displayed in the future' do
- create(:broadcast_message)
+ create(:broadcast_message, broadcast_type: broadcast_type)
future = create(
:broadcast_message,
starts_at: Time.now + 10.minutes,
- ends_at: Time.now + 20.minutes
+ ends_at: Time.now + 20.minutes,
+ broadcast_type: broadcast_type
)
- expect(described_class.current.length).to eq(1)
+ expect(subject.call.length).to eq(1)
Timecop.travel(future.starts_at) do
- expect(described_class.current.length).to eq(2)
+ expect(subject.call.length).to eq(2)
end
end
@@ -86,43 +92,90 @@ describe BroadcastMessage do
create(:broadcast_message, :future)
expect(Rails.cache).not_to receive(:delete).with(described_class::CACHE_KEY)
- expect(described_class.current.length).to eq(0)
+ expect(subject.call.length).to eq(0)
end
+ end
+ shared_examples "matches with current path" do |broadcast_type|
it 'returns message if it matches the target path' do
- message = create(:broadcast_message, target_path: "*/onboarding_completed")
+ message = create(:broadcast_message, target_path: "*/onboarding_completed", broadcast_type: broadcast_type)
- expect(described_class.current('/users/onboarding_completed')).to include(message)
+ expect(subject.call('/users/onboarding_completed')).to include(message)
end
it 'returns message if part of the target path matches' do
- create(:broadcast_message, target_path: "/users/*/issues")
+ create(:broadcast_message, target_path: "/users/*/issues", broadcast_type: broadcast_type)
- expect(described_class.current('/users/name/issues').length).to eq(1)
+ expect(subject.call('/users/name/issues').length).to eq(1)
end
it 'returns the message for empty target path' do
- create(:broadcast_message, target_path: "")
+ create(:broadcast_message, target_path: "", broadcast_type: broadcast_type)
- expect(described_class.current('/users/name/issues').length).to eq(1)
+ expect(subject.call('/users/name/issues').length).to eq(1)
end
it 'returns the message if target path is nil' do
- create(:broadcast_message, target_path: nil)
+ create(:broadcast_message, target_path: nil, broadcast_type: broadcast_type)
- expect(described_class.current('/users/name/issues').length).to eq(1)
+ expect(subject.call('/users/name/issues').length).to eq(1)
end
it 'does not return message if target path does not match' do
- create(:broadcast_message, target_path: "/onboarding_completed")
+ create(:broadcast_message, target_path: "/onboarding_completed", broadcast_type: broadcast_type)
- expect(described_class.current('/welcome').length).to eq(0)
+ expect(subject.call('/welcome').length).to eq(0)
end
it 'does not return message if target path does not match when using wildcard' do
- create(:broadcast_message, target_path: "/users/*/issues")
+ create(:broadcast_message, target_path: "/users/*/issues", broadcast_type: broadcast_type)
+
+ expect(subject.call('/group/groupname/issues').length).to eq(0)
+ end
+ 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)
- expect(described_class.current('/group/groupname/issues').length).to eq(0)
+ expect(subject.call).to contain_exactly(notification_message)
end
end
@@ -193,6 +246,8 @@ describe BroadcastMessage do
message = create(:broadcast_message)
expect(Rails.cache).to receive(:delete).with(described_class::CACHE_KEY)
+ expect(Rails.cache).to receive(:delete).with(described_class::BANNER_CACHE_KEY)
+ expect(Rails.cache).to receive(:delete).with(described_class::NOTIFICATION_CACHE_KEY)
message.flush_redis_cache
end