File: user_preference_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (360 lines) | stat: -rw-r--r-- 10,306 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe UserPreference, feature_category: :user_profile do
  let_it_be(:user) { create(:user) }

  let(:user_preference) { create(:user_preference, user: user) }

  describe 'validations' do
    it do
      is_expected.to validate_numericality_of(:tab_width)
                       .only_integer
                       .is_greater_than_or_equal_to(Gitlab::TabWidth::MIN)
                       .is_less_than_or_equal_to(Gitlab::TabWidth::MAX)
    end

    describe 'diffs_deletion_color and diffs_addition_color' do
      using RSpec::Parameterized::TableSyntax

      where(color: [
        '#000000',
              '#123456',
              '#abcdef',
              '#AbCdEf',
              '#ffffff',
              '#fFfFfF',
              '#000',
              '#123',
              '#abc',
              '#AbC',
              '#fff',
              '#fFf',
              ''
      ])

      with_them do
        it { is_expected.to allow_value(color).for(:diffs_deletion_color) }
        it { is_expected.to allow_value(color).for(:diffs_addition_color) }
      end

      where(color: [
        '#1',
              '#12',
              '#1234',
              '#12345',
              '#1234567',
              '123456',
              '#12345x'
      ])

      with_them do
        it { is_expected.not_to allow_value(color).for(:diffs_deletion_color) }
        it { is_expected.not_to allow_value(color).for(:diffs_addition_color) }
      end
    end

    describe 'pass_user_identities_to_ci_jwt' do
      it { is_expected.not_to allow_value("").for(:pass_user_identities_to_ci_jwt) }
    end

    describe 'visibility_pipeline_id_type' do
      it 'is set to 0 by default' do
        pref = described_class.new

        expect(pref.visibility_pipeline_id_type).to eq('id')
      end

      it { is_expected.to define_enum_for(:visibility_pipeline_id_type) }
    end

    describe 'extensions_marketplace_opt_in_status' do
      it 'is set to 0 by default' do
        pref = described_class.new

        expect(pref.extensions_marketplace_opt_in_status).to eq('unset')
      end

      it do
        is_expected
          .to define_enum_for(:extensions_marketplace_opt_in_status).with_values(unset: 0, enabled: 1, disabled: 2)
      end
    end

    describe 'organization_groups_projects_display' do
      it 'is set to 0 by default' do
        pref = described_class.new

        expect(pref.organization_groups_projects_display).to eq('projects')
      end

      it { is_expected.to define_enum_for(:organization_groups_projects_display).with_values(projects: 0, groups: 1) }
    end

    describe 'user belongs to the home organization' do
      let_it_be(:organization) { create(:organization) }

      before do
        user_preference.home_organization = organization
      end

      context 'when user is an organization user' do
        before do
          create(:organization_user, organization: organization, user: user)
        end

        it 'does not add any validation errors' do
          user_preference.home_organization = organization

          expect(user_preference).to be_valid
          expect(user_preference.errors).to be_empty
        end
      end

      context 'when user is not an organization user' do
        it 'adds a validation error' do
          user_preference.home_organization = organization

          expect(user_preference).to be_invalid
          expect(user_preference.errors.messages[:user].first).to eq(_("is not part of the given organization"))
        end
      end
    end
  end

  describe 'associations' do
    it { is_expected.to belong_to(:home_organization).class_name('Organizations::Organization').optional }
  end

  describe 'notes filters global keys' do
    it 'contains expected values' do
      expect(UserPreference::NOTES_FILTERS.keys).to match_array([:all_notes, :only_comments, :only_activity])
    end
  end

  describe '#set_notes_filter' do
    let(:issuable) { build_stubbed(:issue) }

    shared_examples 'setting system notes' do
      it 'returns updated discussion filter' do
        filter_name =
          user_preference.set_notes_filter(filter, issuable)

        expect(filter_name).to eq(filter)
      end

      it 'updates discussion filter for issuable class' do
        user_preference.set_notes_filter(filter, issuable)

        expect(user_preference.reload.issue_notes_filter).to eq(filter)
      end
    end

    context 'when filter is set to all notes' do
      let(:filter) { described_class::NOTES_FILTERS[:all_notes] }

      it_behaves_like 'setting system notes'
    end

    context 'when filter is set to only comments' do
      let(:filter) { described_class::NOTES_FILTERS[:only_comments] }

      it_behaves_like 'setting system notes'
    end

    context 'when filter is set to only activity' do
      let(:filter) { described_class::NOTES_FILTERS[:only_activity] }

      it_behaves_like 'setting system notes'
    end

    context 'when notes_filter parameter is invalid' do
      let(:only_comments) { described_class::NOTES_FILTERS[:only_comments] }

      it 'returns the current notes filter' do
        user_preference.set_notes_filter(only_comments, issuable)

        expect(user_preference.set_notes_filter(non_existing_record_id, issuable)).to eq(only_comments)
      end
    end
  end

  describe 'sort_by preferences' do
    shared_examples_for 'a sort_by preference' do
      it 'allows nil sort fields' do
        user_preference.update!(attribute => nil)

        expect(user_preference).to be_valid
      end
    end

    context 'merge_requests_sort attribute' do
      let(:attribute) { :merge_requests_sort }

      it_behaves_like 'a sort_by preference'
    end

    context 'issues_sort attribute' do
      let(:attribute) { :issues_sort }

      it_behaves_like 'a sort_by preference'
    end
  end

  describe '#project_shortcut_buttons' do
    it 'is set to true by default' do
      pref = described_class.new

      expect(pref.project_shortcut_buttons).to eq(true)
    end

    it 'returns assigned value' do
      pref = described_class.new(project_shortcut_buttons: false)

      expect(pref.project_shortcut_buttons).to eq(false)
    end
  end

  describe '#keyboard_shortcuts_enabled' do
    it 'is set to true by default' do
      pref = described_class.new

      expect(pref.keyboard_shortcuts_enabled).to eq(true)
    end

    it 'returns assigned value' do
      pref = described_class.new(keyboard_shortcuts_enabled: false)

      expect(pref.keyboard_shortcuts_enabled).to eq(false)
    end
  end

  describe '#early_access_event_tracking?' do
    let(:participant) { true }
    let(:tracking) { true }
    let(:user_preference) do
      build(:user_preference, early_access_program_participant: participant, early_access_program_tracking: tracking)
    end

    context 'when user participate in beta and agreed on tracking' do
      it { expect(user_preference.early_access_event_tracking?).to be true }
    end

    context 'when user does not participate' do
      let(:participant) { false }

      it { expect(user_preference.early_access_event_tracking?).to be false }
    end

    context 'when user did not agree on tracking' do
      let(:tracking) { false }

      it { expect(user_preference.early_access_event_tracking?).to be false }
    end
  end

  describe '#extensions_marketplace_enabled' do
    where(:opt_in_status, :expected_value) do
      [
        ["enabled", true],
        ["disabled", false],
        ["unset", false]
      ]
    end

    with_them do
      it 'returns boolean from extensions_marketplace_opt_in_status' do
        user_preference.update!(extensions_marketplace_opt_in_status: opt_in_status)

        expect(user_preference.extensions_marketplace_enabled).to be expected_value
      end
    end
  end

  describe '#extensions_marketplace_enabled=' do
    where(:value, :expected_opt_in_status) do
      [
        [true, "enabled"],
        [false, "disabled"],
        [0, "disabled"],
        [1, "enabled"]
      ]
    end

    with_them do
      it 'updates extensions_marketplace_opt_in_status' do
        user_preference.update!(extensions_marketplace_opt_in_status: 'unset')

        user_preference.extensions_marketplace_enabled = value

        expect(user_preference.extensions_marketplace_opt_in_status).to be expected_opt_in_status
      end
    end
  end

  describe '#dpop_enabled' do
    let(:pref) { described_class.new(args) }

    context 'when no arguments are provided' do
      let(:args) { {} }

      it 'is set to false by default' do
        expect(pref.dpop_enabled).to eq(false)
      end
    end

    context 'when dpop_enabled is set to nil' do
      let(:args) { { dpop_enabled: nil } }

      it 'returns default value' do
        expect(pref.dpop_enabled).to eq(false)
      end
    end

    context 'when dpop_enabled is set to true' do
      let(:args) { { dpop_enabled: true } }

      it 'returns assigned value' do
        expect(pref.dpop_enabled).to eq(true)
      end
    end
  end

  describe '#text_editor' do
    let(:pref) { described_class.new(text_editor_type: text_editor_type) }
    let(:text_editor_type) { nil }

    context 'when text_editor_type is not set' do
      it 'returns rich_text_editor if rich_text_editor_as_default ff is enabled' do
        stub_feature_flags(rich_text_editor_as_default: true)

        expect(pref.text_editor).to eq(:rich_text_editor)
      end

      it 'returns plain_text_editor if rich_text_editor_as_default ff is disabled' do
        stub_feature_flags(rich_text_editor_as_default: false)

        expect(pref.text_editor).to eq(:plain_text_editor)
      end
    end

    context 'when text_editor_type is set' do
      where(:text_editor_type) { %w[plain_text_editor rich_text_editor] }

      with_them do
        it 'returns assigned text_editor_type regardless of rich_text_editor_as_default ff value' do
          expect(pref.text_editor).to eq(text_editor_type)

          stub_feature_flags(rich_text_editor_as_default: false)

          expect(pref.text_editor).to eq(text_editor_type)

          stub_feature_flags(rich_text_editor_as_default: true)

          expect(pref.text_editor).to eq(text_editor_type)
        end
      end
    end
  end
end