debian-mirror-gitlab/spec/helpers/broadcast_messages_helper_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

181 lines
5.8 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe BroadcastMessagesHelper, feature_category: :onboarding do
2022-05-07 20:08:51 +05:30
include Gitlab::Routing.url_helpers
let_it_be(:user) { create(:user) }
before do
allow(helper).to receive(:current_user).and_return(user)
end
shared_examples 'returns role-targeted broadcast message when in project, group, or sub-group URL' do
let(:feature_flag_state) { true }
before do
stub_feature_flags(role_targeted_broadcast_messages: feature_flag_state)
allow(helper).to receive(:cookies) { {} }
end
context 'when in a project page' do
let_it_be(:project) { create(:project) }
before do
project.add_developer(user)
assign(:project, project)
allow(helper).to receive(:controller) { ProjectsController.new }
end
it { is_expected.to eq message }
context 'when feature flag is disabled' do
let(:feature_flag_state) { false }
it { is_expected.to be_nil }
end
end
context 'when in a group page' do
let_it_be(:group) { create(:group) }
before do
group.add_developer(user)
assign(:group, group)
allow(helper).to receive(:controller) { GroupsController.new }
end
it { is_expected.to eq message }
context 'when feature flag is disabled' do
let(:feature_flag_state) { false }
it { is_expected.to be_nil }
end
end
context 'when not in a project, group, or sub-group page' do
it { is_expected.to be_nil }
context 'when feature flag is disabled' do
let(:feature_flag_state) { false }
it { is_expected.to be_nil }
end
end
end
2023-04-23 21:23:45 +05:30
describe '#current_broadcast_notification_message' do
2020-03-13 15:44:24 +05:30
subject { helper.current_broadcast_notification_message }
context 'with available broadcast notification messages' do
let!(:broadcast_message_1) { create(:broadcast_message, broadcast_type: 'notification', starts_at: Time.now - 1.day) }
let!(:broadcast_message_2) { create(:broadcast_message, broadcast_type: 'notification', starts_at: Time.now) }
it { is_expected.to eq broadcast_message_2 }
context 'when last broadcast message is hidden' do
before do
2020-04-08 14:13:33 +05:30
helper.request.cookies["hide_broadcast_message_#{broadcast_message_2.id}"] = 'true'
2020-03-13 15:44:24 +05:30
end
it { is_expected.to eq broadcast_message_1 }
end
end
context 'without broadcast notification messages' do
it { is_expected.to be_nil }
end
2022-05-07 20:08:51 +05:30
describe 'user access level targeted messages' do
let_it_be(:message) { create(:broadcast_message, broadcast_type: 'notification', starts_at: Time.now, target_access_levels: [Gitlab::Access::DEVELOPER]) }
include_examples 'returns role-targeted broadcast message when in project, group, or sub-group URL'
end
2020-03-13 15:44:24 +05:30
end
2023-04-23 21:23:45 +05:30
describe '#current_broadcast_banner_messages' do
2022-05-07 20:08:51 +05:30
describe 'user access level targeted messages' do
let_it_be(:message) { create(:broadcast_message, broadcast_type: 'banner', starts_at: Time.now, target_access_levels: [Gitlab::Access::DEVELOPER]) }
2021-04-29 21:17:54 +05:30
2022-05-07 20:08:51 +05:30
subject { helper.current_broadcast_banner_messages.first }
2020-03-13 15:44:24 +05:30
2022-05-07 20:08:51 +05:30
include_examples 'returns role-targeted broadcast message when in project, group, or sub-group URL'
2020-04-08 14:13:33 +05:30
end
2022-05-07 20:08:51 +05:30
end
2023-04-23 21:23:45 +05:30
describe '#broadcast_message' do
2022-05-07 20:08:51 +05:30
let(:current_broadcast_message) { BroadcastMessage.new(message: 'Current Message') }
2020-04-08 14:13:33 +05:30
it 'returns nil when no current message' do
expect(helper.broadcast_message(nil)).to be_nil
end
it 'includes the current message' do
2020-03-13 15:44:24 +05:30
expect(helper.broadcast_message(current_broadcast_message)).to include 'Current Message'
end
end
2023-04-23 21:23:45 +05:30
describe '#broadcast_message_status' do
it 'returns Active' do
message = build(:broadcast_message)
expect(helper.broadcast_message_status(message)).to eq 'Active'
end
it 'returns Expired' do
message = build(:broadcast_message, :expired)
2014-09-02 18:07:02 +05:30
expect(helper.broadcast_message_status(message)).to eq 'Expired'
2014-09-02 18:07:02 +05:30
end
it 'returns Pending' do
message = build(:broadcast_message, :future)
2014-09-02 18:07:02 +05:30
expect(helper.broadcast_message_status(message)).to eq 'Pending'
2014-09-02 18:07:02 +05:30
end
end
2023-04-23 21:23:45 +05:30
describe '#admin_broadcast_messages_data' do
let(:starts_at) { 1.day.ago }
let(:ends_at) { 1.day.from_now }
let(:message) { build(:broadcast_message, id: non_existing_record_id, starts_at: starts_at, ends_at: ends_at) }
subject(:single_broadcast_message) { Gitlab::Json.parse(admin_broadcast_messages_data([message])).first }
it 'returns the expected messages data attributes' do
keys = %w[id status preview starts_at ends_at target_roles target_path type edit_path delete_path]
expect(single_broadcast_message.keys).to match(keys)
end
it 'has the correct iso formatted date', time_travel_to: '2020-01-01 00:00:00 +0000' do
expect(single_broadcast_message['starts_at']).to eq('2019-12-31T00:00:00Z')
expect(single_broadcast_message['ends_at']).to eq('2020-01-02T00:00:00Z')
end
end
2023-05-27 22:25:52 +05:30
describe '#broadcast_message_data' do
let(:starts_at) { 1.day.ago }
let(:ends_at) { 1.day.from_now }
let(:message) { build(:broadcast_message, id: non_existing_record_id, starts_at: starts_at, ends_at: ends_at) }
it 'returns the expected message data attributes' do
keys = [
:id, :message, :broadcast_type, :theme, :dismissable, :target_access_levels, :messages_path,
:preview_path, :target_path, :starts_at, :ends_at, :target_access_level_options
]
expect(broadcast_message_data(message).keys).to match(keys)
end
it 'has the correct iso formatted date', time_travel_to: '2020-01-01 00:00:00 +0000' do
expect(broadcast_message_data(message)[:starts_at]).to eq('2019-12-31T00:00:00Z')
expect(broadcast_message_data(message)[:ends_at]).to eq('2020-01-02T00:00:00Z')
end
end
2014-09-02 18:07:02 +05:30
end