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

sidebars_helper_spec.rb « helpers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 421b1c178aa66139deb32c40881900b33b7ab4ac (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SidebarsHelper, feature_category: :navigation do
  include Devise::Test::ControllerHelpers

  describe '#sidebar_tracking_attributes_by_object' do
    subject(:tracking_attrs) { helper.sidebar_tracking_attributes_by_object(object) }

    before do
      stub_application_setting(snowplow_enabled: true)
    end

    context 'when object is a project' do
      let(:object) { build(:project) }

      it 'returns tracking attrs for project' do
        attrs = {
          track_label: 'projects_side_navigation',
          track_property: 'projects_side_navigation',
          track_action: 'render'
        }

        expect(tracking_attrs[:data]).to eq(attrs)
      end
    end

    context 'when object is a group' do
      let(:object) { build(:group) }

      it 'returns tracking attrs for group' do
        attrs = {
          track_label: 'groups_side_navigation',
          track_property: 'groups_side_navigation',
          track_action: 'render'
        }

        expect(tracking_attrs[:data]).to eq(attrs)
      end
    end

    context 'when object is a user' do
      let(:object) { build(:user) }

      it 'returns tracking attrs for user' do
        attrs = {
          track_label: 'user_side_navigation',
          track_property: 'user_side_navigation',
          track_action: 'render'
        }

        expect(tracking_attrs[:data]).to eq(attrs)
      end
    end

    context 'when object is something else' do
      let(:object) { build(:ci_pipeline) }

      it { is_expected.to eq({}) }
    end
  end

  describe '#super_sidebar_context' do
    include_context 'custom session'

    let_it_be(:user) { build(:user) }
    let_it_be(:group) { build(:group) }
    let_it_be(:panel) { {} }
    let_it_be(:panel_type) { 'project' }
    let(:project) { nil }
    let(:current_user_mode) { Gitlab::Auth::CurrentUserMode.new(user) }

    let(:global_shortcut_links) do
      [
        {
          title: _('Milestones'),
          href: dashboard_milestones_path,
          css_class: 'dashboard-shortcuts-milestones'
        },
        {
          title: _('Snippets'),
          href: dashboard_snippets_path,
          css_class: 'dashboard-shortcuts-snippets'
        },
        {
          title: _('Activity'),
          href: activity_dashboard_path,
          css_class: 'dashboard-shortcuts-activity'
        },
        {
          title: _('Groups'),
          href: dashboard_groups_path,
          css_class: 'dashboard-shortcuts-groups'
        },
        {
          title: _('Projects'),
          href: dashboard_projects_path,
          css_class: 'dashboard-shortcuts-projects'
        }
      ]
    end

    subject do
      helper.super_sidebar_context(user, group: group, project: project, panel: panel, panel_type: panel_type)
    end

    before do
      allow(Time).to receive(:now).and_return(Time.utc(2021, 1, 1))
      allow(helper).to receive(:current_user) { user }
      allow(helper).to receive(:can?).and_return(true)
      allow(helper).to receive(:session).and_return(session)
      allow(helper).to receive(:header_search_context).and_return({ some: "search data" })
      allow(helper).to receive(:current_user_mode).and_return(current_user_mode)
      allow(panel).to receive(:super_sidebar_menu_items).and_return(nil)
      allow(panel).to receive(:super_sidebar_context_header).and_return(nil)
      allow(user).to receive(:assigned_open_issues_count).and_return(1)
      allow(user).to receive(:assigned_open_merge_requests_count).and_return(4)
      allow(user).to receive(:review_requested_open_merge_requests_count).and_return(0)
      allow(user).to receive(:todos_pending_count).and_return(3)
      allow(user).to receive(:pinned_nav_items).and_return({ panel_type => %w[foo bar], 'another_panel' => %w[baz] })
    end

    # Tests for logged-out sidebar context
    it_behaves_like 'logged-out super-sidebar context'

    # Tests for logged-in sidebar context below
    it_behaves_like 'shared super sidebar context'
    it { is_expected.to include({ is_logged_in: true }) }

    it 'returns sidebar values from user', :use_clean_rails_memory_store_caching do
      expect(subject).to include({
        is_logged_in: true,
        is_admin: false,
        name: user.name,
        username: user.username,
        admin_url: admin_root_url,
        avatar_url: user.avatar_url,
        has_link_to_profile: helper.current_user_menu?(:profile),
        link_to_profile: user_path(user),
        status: {
          can_update: helper.can?(user, :update_user_status, user),
          busy: user.status&.busy?,
          customized: user.status&.customized?,
          availability: user.status&.availability.to_s,
          emoji: user.status&.emoji,
          message_html: user.status&.message_html&.html_safe,
          message: user.status&.message&.html_safe,
          clear_after: nil
        },
        settings: {
          has_settings: helper.current_user_menu?(:settings),
          profile_path: profile_path,
          profile_preferences_path: profile_preferences_path
        },
        user_counts: {
          assigned_issues: 1,
          assigned_merge_requests: 4,
          review_requested_merge_requests: 0,
          todos: 3,
          last_update: 1609459200000
        },
        can_sign_out: helper.current_user_menu?(:sign_out),
        sign_out_link: destroy_user_session_path,
        issues_dashboard_path: issues_dashboard_path(assignee_username: user.username),
        todos_dashboard_path: dashboard_todos_path,
        projects_path: dashboard_projects_path,
        groups_path: dashboard_groups_path,
        gitlab_com_but_not_canary: Gitlab.com_but_not_canary?,
        gitlab_com_and_canary: Gitlab.com_and_canary?,
        canary_toggle_com_url: Gitlab::Saas.canary_toggle_com_url,
        pinned_items: %w[foo bar],
        update_pins_url: pins_path,
        shortcut_links: global_shortcut_links,
        track_visits_path: track_namespace_visits_path
      })
    end

    context 'when user is admin' do
      before do
        allow(user).to receive(:can_admin_all_resources?).and_return(true)
      end

      it { is_expected.to include({ is_admin: true }) }
    end

    describe "shortcut links" do
      describe "as the anonymous user" do
        let_it_be(:user) { nil }
        let(:global_shortcut_links) do
          [
            {
              title: _('Snippets'),
              href: explore_snippets_path,
              css_class: 'dashboard-shortcuts-snippets'
            },
            {
              title: _('Groups'),
              href: explore_groups_path,
              css_class: 'dashboard-shortcuts-groups'
            },
            {
              title: _('Projects'),
              href: explore_projects_path,
              css_class: 'dashboard-shortcuts-projects'
            }
          ]
        end

        it 'returns global shortcut links' do
          expect(subject[:shortcut_links]).to eq(global_shortcut_links)
        end

        context 'in a project' do
          let_it_be(:project) { build_stubbed(:project) }

          it 'returns project-specific shortcut links' do
            expect(subject[:shortcut_links]).to eq(global_shortcut_links)
          end
        end
      end

      describe "as logged-in user" do
        it 'returns global shortcut links' do
          expect(subject[:shortcut_links]).to eq(global_shortcut_links)
        end

        context 'in a project' do
          let_it_be(:project) { build_stubbed(:project) }

          it 'returns project-specific shortcut links' do
            expect(subject[:shortcut_links]).to eq([
              *global_shortcut_links,
              {
                title: _('Create a new issue'),
                href: new_project_issue_path(project),
                css_class: 'shortcuts-new-issue'
              }
            ])
          end
        end
      end
    end

    it 'returns "Merge requests" menu', :use_clean_rails_memory_store_caching do
      expect(subject[:merge_request_menu]).to eq([
        {
          name: _('Merge requests'),
          items: [
            {
              text: _('Assigned'),
              href: merge_requests_dashboard_path(assignee_username: user.username),
              count: 4,
              userCount: 'assigned_merge_requests',
              extraAttrs: {
                'data-track-action': 'click_link',
                'data-track-label': 'merge_requests_assigned',
                'data-track-property': 'nav_core_menu',
                class: 'dashboard-shortcuts-merge_requests'
              }
            },
            {
              text: _('Review requests'),
              href: merge_requests_dashboard_path(reviewer_username: user.username),
              count: 0,
              userCount: 'review_requested_merge_requests',
              extraAttrs: {
                'data-track-action': 'click_link',
                'data-track-label': 'merge_requests_to_review',
                'data-track-property': 'nav_core_menu',
                class: 'dashboard-shortcuts-review_requests'
              }
            }
          ]
        }
      ])
    end

    it 'returns "Create new" menu groups without headers', :use_clean_rails_memory_store_caching do
      extra_attrs = ->(id) {
        {
          "data-track-label": id,
          "data-track-action": "click_link",
          "data-track-property": "nav_create_menu",
          "data-testid": 'create_menu_item',
          "data-qa-create-menu-item": id
        }
      }

      expect(subject[:create_new_menu_groups]).to eq([
        {
          name: "",
          items: [
            { href: "/projects/new", text: "New project/repository",
              component: nil,
              extraAttrs: extra_attrs.call("general_new_project") },
            { href: "/groups/new", text: "New group",
              component: nil,
              extraAttrs: extra_attrs.call("general_new_group") },
            { href: "/-/organizations/new", text: s_('Organization|New organization'),
              component: nil,
              extraAttrs: extra_attrs.call("general_new_organization") },
            { href: "/-/snippets/new", text: "New snippet",
              component: nil,
              extraAttrs: extra_attrs.call("general_new_snippet") }
          ]
        }
      ])
    end

    it 'returns "Create new" menu groups with headers', :use_clean_rails_memory_store_caching do
      extra_attrs = ->(id) {
        {
          "data-track-label": id,
          "data-track-action": "click_link",
          "data-track-property": "nav_create_menu",
          "data-testid": 'create_menu_item',
          "data-qa-create-menu-item": id
        }
      }

      allow(group).to receive(:persisted?).and_return(true)
      allow(helper).to receive(:can?).and_return(true)

      expect(subject[:create_new_menu_groups]).to contain_exactly(
        a_hash_including(
          name: "In this group",
          items: array_including(
            { href: "/projects/new", text: "New project/repository",
              component: nil,
              extraAttrs: extra_attrs.call("new_project") },
            { href: "/groups/new#create-group-pane", text: "New subgroup",
              component: nil,
              extraAttrs: extra_attrs.call("new_subgroup") },
            { href: nil, text: "Invite members",
              component: 'invite_members',
              extraAttrs: extra_attrs.call("invite") }
          )
        ),
        a_hash_including(
          name: "In GitLab",
          items: array_including(
            { href: "/projects/new", text: "New project/repository",
              component: nil,
              extraAttrs: extra_attrs.call("general_new_project") },
            { href: "/groups/new", text: "New group",
              component: nil,
              extraAttrs: extra_attrs.call("general_new_group") },
            { href: "/-/snippets/new", text: "New snippet",
              component: nil,
              extraAttrs: extra_attrs.call("general_new_snippet") }
          )
        )
      )
    end

    describe 'current context' do
      context 'when current context is a project' do
        let_it_be(:project) { build(:project) }

        subject do
          helper.super_sidebar_context(user, group: nil, project: project, panel: panel, panel_type: panel_type)
        end

        before do
          allow(project).to receive(:persisted?).and_return(true)
        end

        it 'returns project context' do
          expect(subject[:current_context]).to eq({
            namespace: 'projects',
            item: {
              id: project.id,
              avatarUrl: project.avatar_url,
              name: project.name,
              namespace: project.full_name,
              webUrl: project_path(project)
            }
          })
        end
      end

      context 'when current context is a group' do
        subject do
          helper.super_sidebar_context(user, group: group, project: nil, panel: panel, panel_type: panel_type)
        end

        before do
          allow(group).to receive(:persisted?).and_return(true)
        end

        it 'returns group context' do
          expect(subject[:current_context]).to eq({
            namespace: 'groups',
            item: {
              id: group.id,
              avatarUrl: group.avatar_url,
              name: group.name,
              namespace: group.full_name,
              webUrl: group_path(group)
            }
          })
        end
      end

      context 'when current context is not tracked' do
        subject do
          helper.super_sidebar_context(user, group: nil, project: nil, panel: panel, panel_type: panel_type)
        end

        it 'returns no context' do
          expect(subject[:current_context]).to eq({})
        end
      end
    end

    describe 'context switcher persistent links' do
      let_it_be(:public_link) do
        { title: s_('Navigation|Explore'), link: '/explore', icon: 'compass' }
      end

      let_it_be(:public_links_for_user) do
        [
          { title: s_('Navigation|Your work'), link: '/', icon: 'work' },
          public_link,
          { title: s_('Navigation|Profile'), link: '/-/profile', icon: 'profile' },
          { title: s_('Navigation|Preferences'), link: '/-/profile/preferences', icon: 'preferences' }
        ]
      end

      let_it_be(:admin_area_link) do
        { title: s_('Navigation|Admin Area'), link: '/admin', icon: 'admin' }
      end

      let_it_be(:enter_admin_mode_link) do
        { title: s_('Navigation|Enter admin mode'), link: '/admin/session/new', icon: 'lock' }
      end

      let_it_be(:leave_admin_mode_link) do
        { title: s_('Navigation|Leave admin mode'), link: '/admin/session/destroy', icon: 'lock-open',
          data_method: 'post' }
      end

      subject do
        helper.super_sidebar_context(user, group: nil, project: nil, panel: panel, panel_type: panel_type)
      end

      context 'when user is not logged in' do
        let(:user) { nil }

        it 'returns only the public links for an anonymous user' do
          expect(subject[:context_switcher_links]).to eq([public_link])
        end
      end

      context 'when user is not an admin' do
        it 'returns only the public links for a user' do
          expect(subject[:context_switcher_links]).to eq(public_links_for_user)
        end
      end

      context 'when user is an admin' do
        before do
          allow(user).to receive(:admin?).and_return(true)
        end

        context 'when application setting :admin_mode is enabled' do
          before do
            stub_application_setting(admin_mode: true)
          end

          context 'when admin mode is on' do
            before do
              current_user_mode.request_admin_mode!
              current_user_mode.enable_admin_mode!(password: user.password)
            end

            it 'returns public links, admin area and leave admin mode links' do
              expect(subject[:context_switcher_links]).to eq([
                *public_links_for_user,
                admin_area_link,
                leave_admin_mode_link
              ])
            end
          end

          context 'when admin mode is off' do
            it 'returns public links and enter admin mode link' do
              expect(subject[:context_switcher_links]).to eq([
                *public_links_for_user,
                enter_admin_mode_link
              ])
            end
          end
        end

        context 'when application setting :admin_mode is disabled' do
          before do
            stub_application_setting(admin_mode: false)
          end

          it 'returns public links and admin area link' do
            expect(subject[:context_switcher_links]).to eq([
              *public_links_for_user,
              admin_area_link
            ])
          end
        end
      end
    end

    describe 'impersonation data' do
      it 'sets is_impersonating to `false` when not impersonating' do
        expect(subject[:is_impersonating]).to be(false)
      end

      it 'passes the stop_impersonation_path property' do
        expect(subject[:stop_impersonation_path]).to eq(admin_impersonation_path)
      end

      describe 'when impersonating' do
        before do
          session[:impersonator_id] = 5
        end

        it 'sets is_impersonating to `true`' do
          expect(subject[:is_impersonating]).to be(true)
        end
      end
    end
  end

  describe '#super_sidebar_nav_panel' do
    let(:group) { build(:group) }
    let(:project) { build(:project) }
    let(:organization) { build(:organization) }

    before do
      allow(helper).to receive(:project_sidebar_context_data).and_return(
        { current_user: nil, container: project, can_view_pipeline_editor: false, learn_gitlab_enabled: false })
      allow(helper).to receive(:group_sidebar_context_data).and_return(
        { current_user: nil, container: group, show_discover_group_security: false })

      allow(group).to receive(:to_global_id).and_return(5)
    end

    shared_examples 'nav panels available to logged-out users' do
      it 'returns Project Panel for project nav' do
        expect(helper.super_sidebar_nav_panel(nav: 'project',
          user: user)).to be_a(Sidebars::Projects::SuperSidebarPanel)
      end

      it 'returns Group Panel for group nav' do
        expect(helper.super_sidebar_nav_panel(nav: 'group', user: user)).to be_a(Sidebars::Groups::SuperSidebarPanel)
      end

      it 'returns User profile Panel for user profile nav' do
        viewed_user = build(:user)
        expect(helper.super_sidebar_nav_panel(nav: 'user_profile', user: user,
          viewed_user: viewed_user)).to be_a(Sidebars::UserProfile::Panel)
      end

      it 'returns Explore Panel for explore nav' do
        expect(helper.super_sidebar_nav_panel(nav: 'explore', user: user)).to be_a(Sidebars::Explore::Panel)
      end

      it 'returns Organization Panel for organization nav' do
        expect(
          helper.super_sidebar_nav_panel(nav: 'organization', organization: organization, user: user)
        ).to be_a(Sidebars::Organizations::SuperSidebarPanel)
      end

      it 'returns Search Panel for search nav' do
        expect(helper.super_sidebar_nav_panel(nav: 'search', user: user)).to be_a(Sidebars::Search::Panel)
      end
    end

    describe 'when logged-in' do
      let(:user) { build(:user) }

      before do
        Rails.cache.write(['users', user.id, 'assigned_open_issues_count'], 1)
        Rails.cache.write(['users', user.id, 'assigned_open_merge_requests_count'], 4)
        Rails.cache.write(['users', user.id, 'review_requested_open_merge_requests_count'], 0)
        Rails.cache.write(['users', user.id, 'todos_pending_count'], 3)
      end

      it 'returns User Settings Panel for profile nav' do
        expect(helper.super_sidebar_nav_panel(nav: 'profile', user: user)).to be_a(Sidebars::UserSettings::Panel)
      end

      describe 'admin user' do
        it 'returns Admin Panel for admin nav', :aggregate_failures do
          allow(user).to receive(:can_admin_all_resources?).and_return(true)

          expect(helper.super_sidebar_nav_panel(nav: 'admin', user: user)).to be_a(Sidebars::Admin::Panel)
        end
      end

      it 'returns Your Work Panel for admin nav' do
        expect(helper.super_sidebar_nav_panel(nav: 'admin', user: user)).to be_a(Sidebars::YourWork::Panel)
      end

      it 'returns "Your Work" Panel for your_work nav', :use_clean_rails_memory_store_caching do
        expect(helper.super_sidebar_nav_panel(nav: 'your_work', user: user)).to be_a(Sidebars::YourWork::Panel)
      end

      it 'returns "Your Work" Panel as a fallback', :use_clean_rails_memory_store_caching do
        expect(helper.super_sidebar_nav_panel(user: user)).to be_a(Sidebars::YourWork::Panel)
      end

      it_behaves_like 'nav panels available to logged-out users'
    end

    describe 'when logged-out' do
      let(:user) { nil }

      it_behaves_like 'nav panels available to logged-out users'

      it 'returns "Explore" Panel as a fallback' do
        expect(helper.super_sidebar_nav_panel(user: user)).to be_a(Sidebars::Explore::Panel)
      end
    end
  end

  describe '#command_palette_data' do
    it 'returns data for project files search' do
      project = create(:project, :repository) # rubocop:disable RSpec/FactoryBot/AvoidCreate

      expect(helper.command_palette_data(project: project)).to eq(
        project_files_url: project_files_path(
          project, project.default_branch, format: :json),
        project_blob_url: project_blob_path(
          project, project.default_branch)
      )
    end

    it 'returns empty object when project is nil' do
      expect(helper.command_palette_data(project: nil)).to eq({})
    end

    it 'returns empty object when project does not have repo' do
      project = build(:project)
      expect(helper.command_palette_data(project: project)).to eq({})
    end

    it 'returns empty object when project has repo but it is empty' do
      project = build(:project, :empty_repo)
      expect(helper.command_palette_data(project: project)).to eq({})
    end
  end
end