summaryrefslogtreecommitdiff
path: root/spec/helpers/broadcast_messages_helper_spec.rb
blob: 480aeb9487637e9432f8ccf80adab56631d91884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BroadcastMessagesHelper, feature_category: :onboarding do
  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
    before do
      allow(helper).to receive(:cookies).and_return({})
    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 }
    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 }
    end

    context 'when not in a project, group, or sub-group page' do
      it { is_expected.to be_nil }
    end
  end

  describe '#current_broadcast_notification_message' do
    subject { helper.current_broadcast_notification_message }

    context 'with available broadcast notification messages' do
      let!(:broadcast_message_1) do
        create(:broadcast_message, broadcast_type: 'notification', starts_at: Time.now - 1.day)
      end

      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
          helper.request.cookies["hide_broadcast_message_#{broadcast_message_2.id}"] = 'true'
        end

        it { is_expected.to eq broadcast_message_1 }
      end
    end

    context 'without broadcast notification messages' do
      it { is_expected.to be_nil }
    end

    describe 'user access level targeted messages' do
      let_it_be(:message) do
        create(:broadcast_message,
          broadcast_type: 'notification',
          starts_at: Time.now,
          target_access_levels: [Gitlab::Access::DEVELOPER]
        )
      end

      include_examples 'returns role-targeted broadcast message when in project, group, or sub-group URL'
    end
  end

  describe '#current_broadcast_banner_messages' do
    describe 'user access level targeted messages' do
      let_it_be(:message) do
        create(:broadcast_message,
          broadcast_type: 'banner',
          starts_at: Time.now,
          target_access_levels: [Gitlab::Access::DEVELOPER]
        )
      end

      subject { helper.current_broadcast_banner_messages.first }

      include_examples 'returns role-targeted broadcast message when in project, group, or sub-group URL'
    end
  end

  describe '#broadcast_message' do
    let(:current_broadcast_message) { BroadcastMessage.new(message: 'Current Message') }

    it 'returns nil when no current message' do
      expect(helper.broadcast_message(nil)).to be_nil
    end

    it 'includes the current message' do
      expect(helper.broadcast_message(current_broadcast_message)).to include 'Current Message'
    end
  end

  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)

      expect(helper.broadcast_message_status(message)).to eq 'Expired'
    end

    it 'returns Pending' do
      message = build(:broadcast_message, :future)

      expect(helper.broadcast_message_status(message)).to eq 'Pending'
    end
  end

  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

  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
end