summaryrefslogtreecommitdiff
path: root/spec/features/admin/admin_mode_spec.rb
blob: 4df035b13e8b74fdbbf1c0fd64b8d04bf946f5ca (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Admin mode' do
  include MobileHelpers
  include StubENV

  let(:admin) { create(:admin) }

  shared_examples 'combined_menu: feature flag examples' do
    before do
      stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
    end

    context 'application setting :admin_mode is enabled', :request_store do
      before do
        sign_in(admin)
      end

      context 'when not in admin mode' do
        it 'has no leave admin mode button' do
          visit new_admin_session_path

          page.within('.navbar-sub-nav') do
            expect(page).not_to have_link(href: destroy_admin_session_path)
          end
        end

        it 'can open pages not in admin scope' do
          pending_on_combined_menu_flag

          visit new_admin_session_path

          page.within('.navbar-sub-nav') do
            find_all('a', text: 'Projects').first.click
          end

          expect(page).to have_current_path(dashboard_projects_path)
        end

        it 'is necessary to provide credentials again before opening pages in admin scope' do
          visit general_admin_application_settings_path # admin logged out because not in admin_mode

          expect(page).to have_current_path(new_admin_session_path)
        end

        it 'can enter admin mode' do
          visit new_admin_session_path

          fill_in 'user_password', with: admin.password

          click_button 'Enter Admin Mode'

          expect(page).to have_current_path(admin_root_path)
        end

        context 'on a read-only instance' do
          before do
            allow(Gitlab::Database).to receive(:read_only?).and_return(true)
          end

          it 'can enter admin mode' do
            visit new_admin_session_path

            fill_in 'user_password', with: admin.password

            click_button 'Enter Admin Mode'

            expect(page).to have_current_path(admin_root_path)
          end
        end
      end

      context 'when in admin_mode' do
        before do
          gitlab_enable_admin_mode_sign_in(admin)
        end

        it 'contains link to leave admin mode' do
          pending_on_combined_menu_flag

          page.within('.navbar-sub-nav') do
            expect(page).to have_link(href: destroy_admin_session_path)
          end
        end

        it 'can leave admin mode using main dashboard link', :js do
          pending_on_combined_menu_flag

          page.within('.navbar-sub-nav') do
            click_on 'Leave Admin Mode'

            expect(page).to have_link(href: new_admin_session_path)
          end
        end

        it 'can leave admin mode using dropdown menu on smaller screens', :js do
          pending_on_combined_menu_flag

          resize_screen_xs
          visit root_dashboard_path

          find('.header-more').click

          page.within '.navbar-sub-nav' do
            click_on 'Leave Admin Mode'

            find('.header-more').click

            expect(page).to have_link(href: new_admin_session_path)
          end
        end

        it 'can open pages not in admin scope' do
          pending_on_combined_menu_flag

          page.within('.navbar-sub-nav') do
            find_all('a', text: 'Projects').first.click

            expect(page).to have_current_path(dashboard_projects_path)
          end
        end

        context 'nav bar' do
          it 'shows admin dashboard links on bigger screen' do
            pending_on_combined_menu_flag

            visit root_dashboard_path

            page.within '.navbar' do
              expect(page).to have_link(text: 'Admin Area', href: admin_root_path, visible: true)
              expect(page).to have_link(text: 'Leave Admin Mode', href: destroy_admin_session_path, visible: true)
            end
          end

          it 'relocates admin dashboard links to dropdown list on smaller screen', :js do
            pending_on_combined_menu_flag

            resize_screen_xs
            visit root_dashboard_path

            page.within '.navbar' do
              expect(page).not_to have_link(text: 'Leave Admin Mode', href: destroy_admin_session_path, visible: true)
            end

            find('.header-more').click

            page.within '.navbar' do
              expect(page).to have_link(text: 'Admin Area', href: admin_root_path, visible: true)
              expect(page).to have_link(text: 'Leave Admin Mode', href: destroy_admin_session_path, visible: true)
            end
          end
        end

        context 'on a read-only instance' do
          before do
            allow(Gitlab::Database).to receive(:read_only?).and_return(true)
          end

          it 'can leave admin mode', :js do
            pending_on_combined_menu_flag

            page.within('.navbar-sub-nav') do
              click_on 'Leave Admin Mode'

              expect(page).to have_link(href: new_admin_session_path)
            end
          end
        end
      end
    end

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

      it 'shows no admin mode buttons in navbar' do
        visit admin_root_path

        page.within('.navbar-sub-nav') do
          expect(page).not_to have_link(href: new_admin_session_path)
          expect(page).not_to have_link(href: destroy_admin_session_path)
        end
      end
    end
  end

  context 'with combined_menu: feature flag on' do
    let(:needs_rewrite_for_combined_menu_flag_on) { true }

    before do
      stub_feature_flags(combined_menu: true)
    end

    it_behaves_like 'combined_menu: feature flag examples'
  end

  context 'with combined_menu feature flag off' do
    let(:needs_rewrite_for_combined_menu_flag_on) { false }

    before do
      stub_feature_flags(combined_menu: false)
    end

    it_behaves_like 'combined_menu: feature flag examples'
  end

  def pending_on_combined_menu_flag
    pending 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/56587' if needs_rewrite_for_combined_menu_flag_on
  end
end