summaryrefslogtreecommitdiff
path: root/spec/models/broadcast_message_spec.rb
blob: 8fdc9852f6e86a150f0fbf7aa8848c7254daefe6 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BroadcastMessage do
  subject { build(:broadcast_message) }

  it { is_expected.to be_valid }

  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) }

    it { is_expected.to allow_value(1).for(:broadcast_type) }
    it { is_expected.not_to allow_value(nil).for(:broadcast_type) }
    it { is_expected.not_to allow_value(nil).for(:target_access_levels) }
    it { is_expected.to validate_inclusion_of(:target_access_levels).in_array(described_class::ALLOWED_TARGET_ACCESS_LEVELS) }
  end

  describe 'default values' do
    subject(:message) { described_class.new }

    it { expect(message.color).to eq('#E75E40') }
    it { expect(message.font).to eq('#FFFFFF') }
  end

  shared_examples 'time constrainted' do |broadcast_type|
    it 'returns message if time match' do
      message = create(:broadcast_message, broadcast_type: broadcast_type)

      expect(subject.call).to include(message)
    end

    it 'returns multiple messages if time match' do
      message1 = create(:broadcast_message, broadcast_type: broadcast_type)
      message2 = create(:broadcast_message, broadcast_type: broadcast_type)

      expect(subject.call).to contain_exactly(message1, message2)
    end

    it 'returns empty list if time not come' do
      create(:broadcast_message, :future, broadcast_type: broadcast_type)

      expect(subject.call).to be_empty
    end

    it 'returns empty list if time has passed' do
      create(:broadcast_message, :expired, broadcast_type: broadcast_type)

      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, broadcast_type: broadcast_type)

      expect(described_class).to receive(:current_and_future_messages).and_call_original.twice

      subject.call

      travel_to(3.weeks.from_now) do
        subject.call
      end
    end

    it 'expires the value if a broadcast message has ended', :request_store do
      message = create(:broadcast_message, broadcast_type: broadcast_type, ends_at: Time.current.utc + 1.day)

      expect(subject.call).to match_array([message])
      expect(described_class.cache).to receive(:expire).and_call_original

      travel_to(1.week.from_now) do
        2.times { expect(subject.call).to be_empty }
      end
    end

    it 'does not create new records' do
      create(:broadcast_message, broadcast_type: broadcast_type)

      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, broadcast_type: broadcast_type)

      future = create(
        :broadcast_message,
        starts_at: Time.current + 10.minutes,
        ends_at: Time.current + 20.minutes,
        broadcast_type: broadcast_type
      )

      expect(subject.call.length).to eq(1)

      travel_to(future.starts_at + 1.second) do
        expect(subject.call.length).to eq(2)
      end
    end

    it 'does not clear the cache if only a future message should be displayed' do
      create(:broadcast_message, :future)

      expect(Rails.cache).not_to receive(:delete).with(described_class::CACHE_KEY)
      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", broadcast_type: broadcast_type)

      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", broadcast_type: broadcast_type)

      expect(subject.call('/users/name/issues').length).to eq(1)
    end

    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

    it 'returns the message for empty target path' do
      create(:broadcast_message, target_path: "", broadcast_type: broadcast_type)

      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, broadcast_type: broadcast_type)

      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", broadcast_type: broadcast_type)

      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", broadcast_type: broadcast_type)

      expect(subject.call('/group/groupname/issues').length).to eq(0)
    end

    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

    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
  end

  shared_examples "matches with user access level" do |broadcast_type|
    let_it_be(:target_access_levels) { [Gitlab::Access::GUEST] }

    let(:feature_flag_state) { true }

    before do
      stub_feature_flags(role_targeted_broadcast_messages: feature_flag_state)
    end

    context 'when feature flag is disabled' do
      let(:feature_flag_state) { false }

      context 'when message is role-targeted' do
        let_it_be(:message) { create(:broadcast_message, target_access_levels: target_access_levels, broadcast_type: broadcast_type) }

        it 'does not return the message' do
          expect(subject.call(nil, Gitlab::Access::GUEST)).to be_empty
        end
      end

      context 'when message is not role-targeted' do
        let_it_be(:message) { create(:broadcast_message, target_access_levels: [], broadcast_type: broadcast_type) }

        it 'returns the message' do
          expect(subject.call(nil, Gitlab::Access::GUEST)).to include(message)
        end
      end
    end

    context 'when target_access_levels is empty' do
      let_it_be(:message) { create(:broadcast_message, target_access_levels: [], broadcast_type: broadcast_type) }

      it 'returns the message if user access level is not nil' do
        expect(subject.call(nil, Gitlab::Access::MINIMAL_ACCESS)).to include(message)
      end

      it 'returns the message if user access level is nil' do
        expect(subject.call).to include(message)
      end
    end

    context 'when target_access_levels is not empty' do
      let_it_be(:message) { create(:broadcast_message, target_access_levels: target_access_levels, broadcast_type: broadcast_type) }

      it "does not return the message if user access level is nil" do
        expect(subject.call).to be_empty
      end

      it "returns the message if user access level is in target_access_levels" do
        expect(subject.call(nil, Gitlab::Access::GUEST)).to include(message)
      end

      it "does not return the message if user access level is not in target_access_levels" do
        expect(subject.call(nil, Gitlab::Access::MINIMAL_ACCESS)).to be_empty
      end
    end
  end

  shared_examples "handles stale cache data gracefully" do
    # Regression test for https://gitlab.com/gitlab-org/gitlab/-/issues/353076
    context 'when cache returns stale data (e.g. nil target_access_levels)' do
      let(:message) { build(:broadcast_message, :banner, target_access_levels: nil) }
      let(:cache) { Gitlab::JsonCache.new }

      before do
        cache.write(described_class::BANNER_CACHE_KEY, [message])
        allow(BroadcastMessage).to receive(:cache) { cache }
      end

      it 'does not raise error (e.g. NoMethodError from nil.empty?)' do
        expect { subject.call }.not_to raise_error
      end

      context 'when feature flag is disabled' do
        it 'does not raise error (e.g. NoMethodError from nil.empty?)' do
          stub_feature_flags(role_targeted_broadcast_messages: false)

          expect { subject.call }.not_to raise_error
        end
      end
    end
  end

  describe '.current', :use_clean_rails_memory_store_caching do
    subject do
      -> (path = nil, user_access_level = nil) do
        described_class.current(current_path: path, user_access_level: user_access_level)
      end
    end

    it_behaves_like 'time constrainted', :banner
    it_behaves_like 'message cache', :banner
    it_behaves_like 'matches with current path', :banner
    it_behaves_like 'matches with user access level', :banner
    it_behaves_like 'handles stale cache data gracefully'

    context 'when message is from cache' do
      before do
        subject.call
      end

      it_behaves_like 'matches with current path', :banner
      it_behaves_like 'matches with user access level', :banner
      it_behaves_like 'matches with current path', :notification
      it_behaves_like 'matches with user access level', :notification
    end

    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 do
      -> (path = nil, user_access_level = nil) do
        described_class.current_banner_messages(current_path: path, user_access_level: user_access_level)
      end
    end

    it_behaves_like 'time constrainted', :banner
    it_behaves_like 'message cache', :banner
    it_behaves_like 'matches with current path', :banner
    it_behaves_like 'matches with user access level', :banner
    it_behaves_like 'handles stale cache data gracefully'

    context 'when message is from cache' do
      before do
        subject.call
      end

      it_behaves_like 'matches with current path', :banner
      it_behaves_like 'matches with user access level', :banner
    end

    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 do
      -> (path = nil, user_access_level = nil) do
        described_class.current_notification_messages(current_path: path, user_access_level: user_access_level)
      end
    end

    it_behaves_like 'time constrainted', :notification
    it_behaves_like 'message cache', :notification
    it_behaves_like 'matches with current path', :notification
    it_behaves_like 'matches with user access level', :notification
    it_behaves_like 'handles stale cache data gracefully'

    context 'when message is from cache' do
      before do
        subject.call
      end

      it_behaves_like 'matches with current path', :notification
      it_behaves_like 'matches with user access level', :notification
    end

    it 'only returns notifications' do
      notification_message = create(:broadcast_message, broadcast_type: :notification)
      create(:broadcast_message, broadcast_type: :banner)

      expect(subject.call).to contain_exactly(notification_message)
    end
  end

  describe '#attributes' do
    it 'includes message_html field' do
      expect(subject.attributes.keys).to include("cached_markdown_version", "message_html")
    end
  end

  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

  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}:#{Gitlab.revision}")
      expect(Rails.cache).to receive(:delete).with("#{described_class::BANNER_CACHE_KEY}:#{Gitlab.revision}")
      expect(Rails.cache).to receive(:delete).with("#{described_class::NOTIFICATION_CACHE_KEY}:#{Gitlab.revision}")

      message.flush_redis_cache
    end
  end
end