summaryrefslogtreecommitdiff
path: root/spec/controllers/admin/users_controller_spec.rb
blob: 5312a0db7f56cdb1f87f1a61ee5d1c2402d5d681 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Admin::UsersController do
  let(:user) { create(:user) }

  let_it_be(:admin) { create(:admin) }

  before do
    sign_in(admin)
  end

  describe 'GET #index' do
    it 'retrieves all users' do
      get :index

      expect(assigns(:users)).to match_array([user, admin])
    end

    it 'filters by admins' do
      get :index, params: { filter: 'admins' }

      expect(assigns(:users)).to eq([admin])
    end

    it 'eager loads authorized projects association' do
      get :index

      expect(assigns(:users).first.association(:authorized_projects)).to be_loaded
    end
  end

  describe 'GET :id' do
    it 'finds a user case-insensitively' do
      user = create(:user, username: 'CaseSensitive')

      get :show, params: { id: user.username.downcase }

      expect(response).to be_redirect
      expect(response.location).to end_with(user.username)
    end
  end

  describe 'DELETE #destroy', :sidekiq_might_not_need_inline do
    let(:project) { create(:project, namespace: user.namespace) }
    let!(:issue) { create(:issue, author: user) }

    before do
      project.add_developer(user)
    end

    it 'deletes user and ghosts their contributions' do
      delete :destroy, params: { id: user.username }, format: :json

      expect(response).to have_gitlab_http_status(:ok)
      expect(User.exists?(user.id)).to be_falsy
      expect(issue.reload.author).to be_ghost
    end

    it 'deletes the user and their contributions when hard delete is specified' do
      delete :destroy, params: { id: user.username, hard_delete: true }, format: :json

      expect(response).to have_gitlab_http_status(:ok)
      expect(User.exists?(user.id)).to be_falsy
      expect(Issue.exists?(issue.id)).to be_falsy
    end

    context 'prerequisites for account deletion' do
      context 'solo-owned groups' do
        let(:group) { create(:group) }

        context 'if the user is the sole owner of at least one group' do
          before do
            create(:group_member, :owner, group: group, user: user)
          end

          context 'soft-delete' do
            it 'fails' do
              delete :destroy, params: { id: user.username }

              message = s_('AdminUsers|You must transfer ownership or delete the groups owned by this user before you can delete their account')

              expect(flash[:alert]).to eq(message)
              expect(response).to have_gitlab_http_status(:see_other)
              expect(response).to redirect_to admin_user_path(user)
              expect(User.exists?(user.id)).to be_truthy
            end
          end

          context 'hard-delete' do
            it 'succeeds' do
              delete :destroy, params: { id: user.username, hard_delete: true }

              expect(response).to redirect_to(admin_users_path)
              expect(flash[:notice]).to eq(_('The user is being deleted.'))
              expect(User.exists?(user.id)).to be_falsy
            end
          end
        end
      end
    end
  end

  describe 'PUT #approve' do
    let(:user) { create(:user, :blocked_pending_approval) }

    subject { put :approve, params: { id: user.username } }

    context 'when feature is disabled' do
      before do
        stub_feature_flags(admin_approval_for_new_user_signups: false)
      end

      it 'responds with access denied' do
        subject

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when feature is enabled' do
      before do
        stub_feature_flags(admin_approval_for_new_user_signups: true)
      end

      context 'when successful' do
        it 'activates the user' do
          subject

          user.reload

          expect(user).to be_active
          expect(flash[:notice]).to eq('Successfully approved')
        end
      end

      context 'when unsuccessful' do
        let(:user) { create(:user, :blocked) }

        it 'displays the error' do
          subject

          expect(flash[:alert]).to eq('The user you are trying to approve is not pending an approval')
        end

        it 'does not activate the user' do
          subject

          user.reload
          expect(user).not_to be_active
        end
      end
    end
  end

  describe 'PUT #activate' do
    shared_examples 'a request that activates the user' do
      it 'activates the user' do
        put :activate, params: { id: user.username }
        user.reload
        expect(user.active?).to be_truthy
        expect(flash[:notice]).to eq('Successfully activated')
      end
    end

    context 'for a deactivated user' do
      before do
        user.deactivate
      end

      it_behaves_like 'a request that activates the user'
    end

    context 'for an active user' do
      it_behaves_like 'a request that activates the user'
    end

    context 'for a blocked user' do
      before do
        user.block
      end

      it 'does not activate the user' do
        put :activate, params: { id: user.username }
        user.reload
        expect(user.active?).to be_falsey
        expect(flash[:notice]).to eq('Error occurred. A blocked user must be unblocked to be activated')
      end
    end
  end

  describe 'PUT #deactivate' do
    shared_examples 'a request that deactivates the user' do
      it 'deactivates the user' do
        put :deactivate, params: { id: user.username }
        user.reload
        expect(user.deactivated?).to be_truthy
        expect(flash[:notice]).to eq('Successfully deactivated')
      end
    end

    context 'for an active user' do
      let(:activity) { {} }
      let(:user) { create(:user, **activity) }

      context 'with no recent activity' do
        let(:activity) { { last_activity_on: ::User::MINIMUM_INACTIVE_DAYS.next.days.ago } }

        it_behaves_like 'a request that deactivates the user'
      end

      context 'with recent activity' do
        let(:activity) { { last_activity_on: ::User::MINIMUM_INACTIVE_DAYS.pred.days.ago } }

        it 'does not deactivate the user' do
          put :deactivate, params: { id: user.username }
          user.reload
          expect(user.deactivated?).to be_falsey
          expect(flash[:notice]).to eq("The user you are trying to deactivate has been active in the past #{::User::MINIMUM_INACTIVE_DAYS} days and cannot be deactivated")
        end
      end
    end

    context 'for a deactivated user' do
      before do
        user.deactivate
      end

      it_behaves_like 'a request that deactivates the user'
    end

    context 'for a blocked user' do
      before do
        user.block
      end

      it 'does not deactivate the user' do
        put :deactivate, params: { id: user.username }
        user.reload
        expect(user.deactivated?).to be_falsey
        expect(flash[:notice]).to eq('Error occurred. A blocked user cannot be deactivated')
      end
    end

    context 'for an internal user' do
      it 'does not deactivate the user' do
        internal_user = User.alert_bot

        put :deactivate, params: { id: internal_user.username }

        expect(internal_user.reload.deactivated?).to be_falsey
        expect(flash[:notice]).to eq('Internal users cannot be deactivated')
      end
    end
  end

  describe 'PUT block/:id' do
    it 'blocks user' do
      put :block, params: { id: user.username }
      user.reload
      expect(user.blocked?).to be_truthy
      expect(flash[:notice]).to eq _('Successfully blocked')
    end
  end

  describe 'PUT unblock/:id' do
    context 'ldap blocked users' do
      let(:user) { create(:omniauth_user, provider: 'ldapmain') }

      before do
        user.ldap_block
      end

      it 'does not unblock user' do
        put :unblock, params: { id: user.username }
        user.reload
        expect(user.blocked?).to be_truthy
        expect(flash[:alert]).to eq _('This user cannot be unlocked manually from GitLab')
      end
    end

    context 'manually blocked users' do
      before do
        user.block
      end

      it 'unblocks user' do
        put :unblock, params: { id: user.username }
        user.reload
        expect(user.blocked?).to be_falsey
        expect(flash[:notice]).to eq _('Successfully unblocked')
      end
    end
  end

  describe 'PUT unlock/:id' do
    before do
      request.env["HTTP_REFERER"] = "/"
      user.lock_access!
    end

    it 'unlocks user' do
      put :unlock, params: { id: user.username }
      user.reload
      expect(user.access_locked?).to be_falsey
    end
  end

  describe 'PUT confirm/:id' do
    let(:user) { create(:user, confirmed_at: nil) }

    before do
      request.env["HTTP_REFERER"] = "/"
    end

    it 'confirms user' do
      put :confirm, params: { id: user.username }
      user.reload
      expect(user.confirmed?).to be_truthy
    end
  end

  describe 'PATCH disable_two_factor' do
    subject { patch :disable_two_factor, params: { id: user.to_param } }

    context 'for a user that has 2FA enabled' do
      let(:user) { create(:user, :two_factor) }

      it 'disables 2FA for the user' do
        subject

        expect(user.reload.two_factor_enabled?).to eq(false)
      end

      it 'redirects back' do
        subject

        expect(response).to redirect_to(admin_user_path(user))
      end

      it 'displays a notice on success' do
        subject

        expect(flash[:notice])
          .to eq _('Two-factor authentication has been disabled for this user')
      end
    end

    context 'for a user that does not have 2FA enabled' do
      it 'redirects back' do
        subject

        expect(response).to redirect_to(admin_user_path(user))
      end

      it 'displays an alert on failure' do
        subject

        expect(flash[:alert])
          .to eq _('Two-factor authentication is not enabled for this user')
      end
    end
  end

  describe 'POST create' do
    it 'creates the user' do
      expect { post :create, params: { user: attributes_for(:user) } }.to change { User.count }.by(1)
    end

    it 'shows only one error message for an invalid email' do
      post :create, params: { user: attributes_for(:user, email: 'bogus') }

      errors = assigns[:user].errors
      expect(errors).to contain_exactly(errors.full_message(:email, I18n.t('errors.messages.invalid')))
    end

    context 'admin notes' do
      it 'creates the user with note' do
        note = '2020-05-12 | Note | DCMA | Link'
        user_params = attributes_for(:user, note: note)

        expect { post :create, params: { user: user_params } }.to change { User.count }.by(1)

        new_user = User.last
        expect(new_user.note).to eq(note)
      end
    end
  end

  describe 'POST update' do
    context 'when the password has changed' do
      def update_password(user, password = User.random_password, password_confirmation = password, format = :html)
        params = {
          id: user.to_param,
          user: {
            password: password,
            password_confirmation: password_confirmation
          }
        }

        post :update, params: params, format: format
      end

      context 'when admin changes their own password' do
        context 'when password is valid' do
          it 'updates the password' do
            expect { update_password(admin) }
              .to change { admin.reload.encrypted_password }
          end

          it 'does not set the new password to expire immediately' do
            expect { update_password(admin) }
              .not_to change { admin.reload.password_expired? }
          end

          it 'does not enqueue the `admin changed your password` email' do
            expect { update_password(admin) }
              .not_to have_enqueued_mail(DeviseMailer, :password_change_by_admin)
          end

          it 'enqueues the `password changed` email' do
            expect { update_password(admin) }
              .to have_enqueued_mail(DeviseMailer, :password_change)
          end
        end
      end

      context 'when admin changes the password of another user' do
        context 'when the new password is valid' do
          it 'redirects to the user' do
            update_password(user)

            expect(response).to redirect_to(admin_user_path(user))
          end

          it 'updates the password' do
            expect { update_password(user) }
              .to change { user.reload.encrypted_password }
          end

          it 'sets the new password to expire immediately' do
            expect { update_password(user) }
              .to change { user.reload.password_expired? }.from(false).to(true)
          end

          it 'enqueues the `admin changed your password` email' do
            expect { update_password(user) }
              .to have_enqueued_mail(DeviseMailer, :password_change_by_admin)
          end

          it 'does not enqueue the `password changed` email' do
            expect { update_password(user) }
              .not_to have_enqueued_mail(DeviseMailer, :password_change)
          end
        end
      end

      context 'when the new password is invalid' do
        let(:password) { 'invalid' }

        it 'shows the edit page again' do
          update_password(user, password)

          expect(response).to render_template(:edit)
        end

        it 'returns the error message' do
          update_password(user, password)

          expect(assigns[:user].errors).to contain_exactly(a_string_matching(/too short/))
        end

        it 'does not update the password' do
          expect { update_password(user, password) }
            .not_to change { user.reload.encrypted_password }
        end
      end

      context 'when the new password does not match the password confirmation' do
        let(:password) { 'some_password' }
        let(:password_confirmation) { 'not_same_as_password' }

        it 'shows the edit page again' do
          update_password(user, password, password_confirmation)

          expect(response).to render_template(:edit)
        end

        it 'returns the error message' do
          update_password(user, password, password_confirmation)

          expect(assigns[:user].errors).to contain_exactly(a_string_matching(/doesn't match/))
        end

        it 'does not update the password' do
          expect { update_password(user, password, password_confirmation) }
            .not_to change { user.reload.encrypted_password }
        end
      end

      context 'when the update fails' do
        let(:password) { User.random_password }

        before do
          expect_next_instance_of(Users::UpdateService) do |service|
            allow(service).to receive(:execute).and_return({ message: 'failed', status: :error })
          end
        end

        it 'returns a 500 error' do
          expect { update_password(admin, password, password, :json) }
            .not_to change { admin.reload.password_expired? }

          expect(response).to have_gitlab_http_status(:error)
        end
      end
    end

    context 'admin notes' do
      it 'updates the note for the user' do
        note = '2020-05-12 | Note | DCMA | Link'
        params = {
          id: user.to_param,
          user: {
            note: note
          }
        }

        expect { post :update, params: params }.to change { user.reload.note }.to(note)
      end
    end
  end

  describe "DELETE #remove_email" do
    it 'deletes the email' do
      email = create(:email, user: user)

      delete :remove_email, params: { id: user.username, email_id: email.id }

      expect(user.reload.emails).not_to include(email)
      expect(flash[:notice]).to eq('Successfully removed email.')
    end
  end

  describe "POST impersonate" do
    context "when the user is blocked" do
      before do
        user.block!
      end

      it "shows a notice" do
        post :impersonate, params: { id: user.username }

        expect(flash[:alert]).to eq(_('You cannot impersonate a blocked user'))
      end

      it "doesn't sign us in as the user" do
        post :impersonate, params: { id: user.username }

        expect(warden.user).to eq(admin)
      end
    end

    context "when the user is not blocked" do
      it "stores the impersonator in the session" do
        post :impersonate, params: { id: user.username }

        expect(session[:impersonator_id]).to eq(admin.id)
      end

      it "signs us in as the user" do
        post :impersonate, params: { id: user.username }

        expect(warden.user).to eq(user)
      end

      it 'logs the beginning of the impersonation event' do
        expect(Gitlab::AppLogger).to receive(:info).with("User #{admin.username} has started impersonating #{user.username}").and_call_original

        post :impersonate, params: { id: user.username }
      end

      it "redirects to root" do
        post :impersonate, params: { id: user.username }

        expect(response).to redirect_to(root_path)
      end

      it "shows a notice" do
        post :impersonate, params: { id: user.username }

        expect(flash[:alert]).to eq("You are now impersonating #{user.username}")
      end
    end

    context "when impersonation is disabled" do
      before do
        stub_config_setting(impersonation_enabled: false)
      end

      it "shows error page" do
        post :impersonate, params: { id: user.username }

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end
end