summaryrefslogtreecommitdiff
path: root/spec/tasks/gitlab/password_rake_spec.rb
blob: 65bba836024dcf92c6989a387fc8e990c1f72c2d (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
# frozen_string_literal: true

require 'rake_helper'

RSpec.describe 'gitlab:password rake tasks', :silence_stdout do
  let_it_be(:user_1) { create(:user, username: 'foobar', password: 'initial_password') }

  def stub_username(username)
    allow(Gitlab::TaskHelpers).to receive(:prompt).with('Enter username: ').and_return(username)
  end

  def stub_password(password, confirmation = nil)
    confirmation ||= password
    allow(Gitlab::TaskHelpers).to receive(:prompt_for_password).and_return(password)
    allow(Gitlab::TaskHelpers).to receive(:prompt_for_password).with('Confirm password: ').and_return(confirmation)
  end

  before do
    Rake.application.rake_require 'tasks/gitlab/password'

    stub_username('foobar')
    stub_password('secretpassword')
  end

  describe ':reset' do
    context 'when all inputs are correct' do
      it 'updates the password properly' do
        run_rake_task('gitlab:password:reset', user_1.username)
        expect(user_1.reload.valid_password?('secretpassword')).to eq(true)
      end
    end

    context 'when username is not provided' do
      it 'asks for username' do
        expect(Gitlab::TaskHelpers).to receive(:prompt).with('Enter username: ')

        run_rake_task('gitlab:password:reset')
      end

      context 'when username is empty' do
        it 'aborts with an error' do
          stub_username('')
          expect { run_rake_task('gitlab:password:reset') }.to raise_error(/Username can not be empty./)
        end
      end
    end

    context 'when username is passed as argument' do
      it 'does not ask for username' do
        expect(Gitlab::TaskHelpers).not_to receive(:prompt)

        run_rake_task('gitlab:password:reset', 'foobar')
      end
    end

    context 'when passwords do not match' do
      before do
        stub_password('randompassword', 'differentpassword')
      end

      it 'aborts with an error' do
        expect { run_rake_task('gitlab:password:reset') }.to raise_error(%r{Unable to change password of the user with username foobar.\nPassword confirmation doesn't match Password})
      end
    end

    context 'when user cannot be found' do
      before do
        stub_username('nonexistentuser')
      end

      it 'aborts with an error' do
        expect { run_rake_task('gitlab:password:reset') }.to raise_error(/Unable to find user with username nonexistentuser./)
      end
    end
  end
end