admin_users_spec.rb 11.1 KB
Newer Older
G
gitlabhq 已提交
1 2
require 'spec_helper'

3 4 5 6 7 8
describe "Admin::Users", feature: true do
  let!(:user) do
    create(:omniauth_user, provider: 'twitter', extern_uid: '123456')
  end

  let!(:current_user) { login_as :admin }
G
gitlabhq 已提交
9 10

  describe "GET /admin/users" do
N
Nihad Abbasov 已提交
11
    before do
G
gitlabhq 已提交
12 13 14
      visit admin_users_path
    end

15
    it "is ok" do
16
      expect(current_path).to eq(admin_users_path)
G
gitlabhq 已提交
17 18
    end

19
    it "has users list" do
20 21 22 23
      expect(page).to have_content(current_user.email)
      expect(page).to have_content(current_user.name)
      expect(page).to have_content(user.email)
      expect(page).to have_content(user.name)
24
      expect(page).to have_link('Block', href: block_admin_user_path(user))
25 26
      expect(page).to have_link('Remove user', href: admin_user_path(user))
      expect(page).to have_link('Remove user and contributions', href: admin_user_path(user, hard_delete: true))
G
gitlabhq 已提交
27
    end
28 29 30

    describe 'Two-factor Authentication filters' do
      it 'counts users who have enabled 2FA' do
31
        create(:user, :two_factor)
32 33 34 35 36 37 38 39 40

        visit admin_users_path

        page.within('.filter-two-factor-enabled small') do
          expect(page).to have_content('1')
        end
      end

      it 'filters by users who have enabled 2FA' do
41
        user = create(:user, :two_factor)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

        visit admin_users_path
        click_link '2FA Enabled'

        expect(page).to have_content(user.email)
      end

      it 'counts users who have not enabled 2FA' do
        visit admin_users_path

        page.within('.filter-two-factor-disabled small') do
          expect(page).to have_content('2') # Including admin
        end
      end

      it 'filters by users who have not enabled 2FA' do
        visit admin_users_path
        click_link '2FA Disabled'

        expect(page).to have_content(user.email)
      end
    end
G
gitlabhq 已提交
64 65
  end

N
Nihad Abbasov 已提交
66 67
  describe "GET /admin/users/new" do
    before do
G
gitlabhq 已提交
68
      visit new_admin_user_path
69
      fill_in "user_name", with: "Big Bang"
70
      fill_in "user_username", with: "bang"
71
      fill_in "user_email", with: "bigbang@mail.com"
G
gitlabhq 已提交
72 73
    end

74
    it "creates new user" do
75
      expect { click_button "Create user" }.to change {User.count}.by(1)
G
gitlabhq 已提交
76 77
    end

78
    it "applies defaults to user" do
79
      click_button "Create user"
D
Dmitriy Zaporozhets 已提交
80
      user = User.find_by(username: 'bang')
J
Jeroen van Baarsen 已提交
81 82 83 84
      expect(user.projects_limit).
        to eq(Gitlab.config.gitlab.default_projects_limit)
      expect(user.can_create_group).
        to eq(Gitlab.config.gitlab.default_can_create_group)
85 86
    end

87
    it "creates user with valid data" do
88
      click_button "Create user"
D
Dmitriy Zaporozhets 已提交
89
      user = User.find_by(username: 'bang')
J
Jeroen van Baarsen 已提交
90 91
      expect(user.name).to eq('Big Bang')
      expect(user.email).to eq('bigbang@mail.com')
G
gitlabhq 已提交
92 93
    end

94
    it "calls send mail" do
V
Valery Sizov 已提交
95
      expect_any_instance_of(NotificationService).to receive(:new_user)
96

97
      click_button "Create user"
G
gitlabhq 已提交
98 99
    end

100
    it "sends valid email to user with email & password" do
V
Valery Sizov 已提交
101 102 103 104
      perform_enqueued_jobs do
        click_button "Create user"
      end

D
Dmitriy Zaporozhets 已提交
105
      user = User.find_by(username: 'bang')
106
      email = ActionMailer::Base.deliveries.last
J
Jeroen van Baarsen 已提交
107
      expect(email.subject).to have_content('Account was created')
108 109
      expect(email.text_part.body).to have_content(user.email)
      expect(email.text_part.body).to have_content('password')
M
Marin Jankovski 已提交
110
    end
G
gitlabhq 已提交
111 112
  end

N
Nihad Abbasov 已提交
113
  describe "GET /admin/users/:id" do
114
    it "has user info" do
G
gitlabhq 已提交
115
      visit admin_users_path
116
      click_link user.name
G
gitlabhq 已提交
117

118 119
      expect(page).to have_content(user.email)
      expect(page).to have_content(user.name)
G
gitlabhq 已提交
120
    end
121

122 123 124
    describe 'Impersonation' do
      let(:another_user) { create(:user) }
      before { visit admin_user_path(another_user) }
125

126 127 128 129
      context 'before impersonating' do
        it 'shows impersonate button for other users' do
          expect(page).to have_content('Impersonate')
        end
130

131
        it 'does not show impersonate button for admin itself' do
132
          visit admin_user_path(current_user)
133

134
          expect(page).not_to have_content('Impersonate')
135
        end
A
Andrew Tomaka 已提交
136

137
        it 'does not show impersonate button for blocked user' do
A
Andrew Tomaka 已提交
138 139 140 141 142 143 144 145
          another_user.block

          visit admin_user_path(another_user)

          expect(page).not_to have_content('Impersonate')

          another_user.activate
        end
146 147
      end

148 149 150 151
      context 'when impersonating' do
        before { click_link 'Impersonate' }

        it 'logs in as the user when impersonate is clicked' do
152
          expect(page.find(:css, '.header-user .profile-link')['data-user']).to eql(another_user.username)
153 154 155 156 157
        end

        it 'sees impersonation log out icon' do
          icon = first('.fa.fa-user-secret')

158
          expect(icon).not_to eql nil
159 160
        end

161
        it 'logs out of impersonated user back to original user' do
162 163
          find(:css, 'li.impersonation a').click

164
          expect(page.find(:css, '.header-user .profile-link')['data-user']).to eql(current_user.username)
165 166 167 168 169 170 171
        end

        it 'is redirected back to the impersonated users page in the admin after stopping' do
          find(:css, 'li.impersonation a').click

          expect(current_path).to eql "/admin/users/#{another_user.username}"
        end
172 173 174
      end
    end

175 176
    describe 'Two-factor Authentication status' do
      it 'shows when enabled' do
177
        user.update_attribute(:otp_required_for_login, true)
178

179
        visit admin_user_path(user)
180 181 182 183 184

        expect_two_factor_status('Enabled')
      end

      it 'shows when disabled' do
185
        visit admin_user_path(user)
186 187 188 189 190 191 192 193 194 195

        expect_two_factor_status('Disabled')
      end

      def expect_two_factor_status(status)
        page.within('.two-factor-status') do
          expect(page).to have_content(status)
        end
      end
    end
G
gitlabhq 已提交
196 197
  end

N
Nihad Abbasov 已提交
198 199
  describe "GET /admin/users/:id/edit" do
    before do
G
gitlabhq 已提交
200
      visit admin_users_path
201
      click_link "edit_user_#{user.id}"
G
gitlabhq 已提交
202 203
    end

204
    it "has user edit page" do
J
Jeroen van Baarsen 已提交
205 206
      expect(page).to have_content('Name')
      expect(page).to have_content('Password')
G
gitlabhq 已提交
207 208 209
    end

    describe "Update user" do
N
Nihad Abbasov 已提交
210
      before do
211 212
        fill_in "user_name", with: "Big Bang"
        fill_in "user_email", with: "bigbang@mail.com"
213 214
        fill_in "user_password", with: "AValidPassword1"
        fill_in "user_password_confirmation", with: "AValidPassword1"
215
        choose "user_access_level_admin"
216
        click_button "Save changes"
G
gitlabhq 已提交
217 218
      end

219
      it "shows page with new data" do
J
Jeroen van Baarsen 已提交
220 221
        expect(page).to have_content('bigbang@mail.com')
        expect(page).to have_content('Big Bang')
G
gitlabhq 已提交
222 223
      end

224
      it "changes user entry" do
225 226
        user.reload
        expect(user.name).to eq('Big Bang')
B
blackst0ne 已提交
227
        expect(user.admin?).to be_truthy
228 229 230 231 232 233 234 235 236 237 238 239 240 241
        expect(user.password_expires_at).to be <= Time.now
      end
    end

    describe 'update username to non ascii char' do
      it do
        fill_in 'user_username', with: '\u3042\u3044'
        click_button('Save')

        page.within '#error_explanation' do
          expect(page).to have_content('Username')
        end

        expect(page).to have_selector(%(form[action="/admin/users/#{user.username}"]))
G
gitlabhq 已提交
242 243 244
      end
    end
  end
245 246

  describe "GET /admin/users/:id/projects" do
247 248 249
    let(:group) { create(:group) }
    let!(:project) { create(:project, group: group) }

250
    before do
251
      group.add_developer(user)
252

253
      visit projects_admin_user_path(user)
254 255 256 257 258
    end

    it "lists group projects" do
      within(:css, '.append-bottom-default + .panel') do
        expect(page).to have_content 'Group projects'
259
        expect(page).to have_link group.name, admin_group_path(group)
260 261 262 263 264
      end
    end

    it 'allows navigation to the group details' do
      within(:css, '.append-bottom-default + .panel') do
265
        click_link group.name
266 267
      end
      within(:css, 'h3.page-title') do
268
        expect(page).to have_content "Group: #{group.name}"
269
      end
270
      expect(page).to have_content project.name
271
    end
272 273 274 275 276 277 278 279 280 281 282

    it 'shows the group access level' do
      within(:css, '.append-bottom-default + .panel') do
        expect(page).to have_content 'Developer'
      end
    end

    it 'allows group membership to be revoked', js: true do
      page.within(first('.group_member')) do
        find('.btn-remove').click
      end
283
      wait_for_requests
284 285 286

      expect(page).not_to have_selector('.group_member')
    end
287
  end
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

  describe 'show user attributes' do
    it do
      visit admin_users_path

      click_link user.name

      expect(page).to have_content 'Account'
      expect(page).to have_content 'Personal projects limit'
    end
  end

  describe 'remove users secondary email', js: true do
    let!(:secondary_email) do
      create :email, email: 'secondary@example.com', user: user
    end

    it do
      visit admin_user_path(user.username)

      expect(page).to have_content("Secondary email: #{secondary_email.email}")

      find("#remove_email_#{secondary_email.id}").click

      expect(page).not_to have_content(secondary_email.email)
    end
  end

  describe 'show user keys' do
    let!(:key1) do
      create(:key, user: user, title: "ssh-rsa Key1", key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4FIEBXGi4bPU8kzxMefudPIJ08/gNprdNTaO9BR/ndy3+58s2HCTw2xCHcsuBmq+TsAqgEidVq4skpqoTMB+Uot5Uzp9z4764rc48dZiI661izoREoKnuRQSsRqUTHg5wrLzwxlQbl1MVfRWQpqiz/5KjBC7yLEb9AbusjnWBk8wvC1bQPQ1uLAauEA7d836tgaIsym9BrLsMVnR4P1boWD3Xp1B1T/ImJwAGHvRmP/ycIqmKdSpMdJXwxcb40efWVj0Ibbe7ii9eeoLdHACqevUZi6fwfbymdow+FeqlkPoHyGg3Cu4vD/D8+8cRc7mE/zGCWcQ15Var83Tczour Key1")
    end

    let!(:key2) do
      create(:key, user: user, title: "ssh-rsa Key2", key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQSTWXhJAX/He+nG78MiRRRn7m0Pb0XbcgTxE0etArgoFoh9WtvDf36HG6tOSg/0UUNcp0dICsNAmhBKdncp6cIyPaXJTURPRAGvhI0/VDk4bi27bRnccGbJ/hDaUxZMLhhrzY0r22mjVf8PF6dvv5QUIQVm1/LeaWYsHHvLgiIjwrXirUZPnFrZw6VLREoBKG8uWvfSXw1L5eapmstqfsME8099oi+vWLR8MgEysZQmD28M73fgW4zek6LDQzKQyJx9nB+hJkKUDvcuziZjGmRFlNgSA2mguERwL1OXonD8WYUrBDGKroIvBT39zS5d9tQDnidEJZ9Y8gv5ViYP7x Key2")
    end

    it do
      visit admin_users_path

      click_link user.name
      click_link 'SSH keys'

      expect(page).to have_content(key1.title)
      expect(page).to have_content(key2.title)

      click_link key2.title

      expect(page).to have_content(key2.title)
      expect(page).to have_content(key2.key)

      click_link 'Remove'

      expect(page).not_to have_content(key2.title)
    end
  end

  describe 'show user identities' do
    it 'shows user identities' do
      visit admin_user_identities_path(user)

      expect(page).to have_content(user.name)
      expect(page).to have_content('twitter')
    end
  end

  describe 'update user identities' do
    before do
      allow(Gitlab::OAuth::Provider).to receive(:providers).and_return([:twitter, :twitter_updated])
    end

    it 'modifies twitter identity' do
      visit admin_user_identities_path(user)

      find('.table').find(:link, 'Edit').click
      fill_in 'identity_extern_uid', with: '654321'
      select 'twitter_updated', from: 'identity_provider'
      click_button 'Save changes'

      expect(page).to have_content(user.name)
      expect(page).to have_content('twitter_updated')
      expect(page).to have_content('654321')
    end
  end

  describe 'remove user with identities' do
    it 'removes user with twitter identity' do
      visit admin_user_identities_path(user)

      click_link 'Delete'

      expect(page).to have_content(user.name)
      expect(page).not_to have_content('twitter')
    end
  end
G
gitlabhq 已提交
383
end