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

password_spec.rb « profiles « features « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b324ee178732dea9167b8c9210dc231e2ac262b5 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Profile > Password', feature_category: :users do
  let(:user) { create(:user) }

  def fill_passwords(password, confirmation)
    fill_in 'New password',          with: password
    fill_in 'Password confirmation', with: confirmation

    click_button 'Save password'
  end

  context 'Password authentication enabled' do
    let(:new_password) { User.random_password }
    let(:user) { create(:user, password_automatically_set: true) }

    before do
      sign_in(user)
      visit edit_profile_password_path
    end

    context 'User with password automatically set' do
      describe 'User puts different passwords in the field and in the confirmation' do
        it 'shows an error message' do
          fill_passwords(new_password, "#{new_password}2")

          page.within('.gl-alert-danger') do
            expect(page).to have_content("Password confirmation doesn't match Password")
          end
        end

        it 'does not contain the current password field after an error' do
          fill_passwords(new_password, "#{new_password}2")

          expect(page).to have_no_field('user[current_password]')
        end
      end

      describe 'User puts the same passwords in the field and in the confirmation' do
        it 'shows a success message' do
          fill_passwords(new_password, new_password)

          page.within('[data-testid="alert-info"]') do
            expect(page).to have_content('Password was successfully updated. Please sign in again.')
          end
        end
      end
    end
  end

  context 'Password authentication unavailable' do
    context 'Regular user' do
      before do
        gitlab_sign_in(user)
      end

      let(:user) { create(:user) }

      it 'renders 404 when password authentication is disabled for the web interface and Git' do
        stub_application_setting(password_authentication_enabled_for_web: false)
        stub_application_setting(password_authentication_enabled_for_git: false)

        visit edit_profile_password_path

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

    context 'LDAP user' do
      include LdapHelpers

      let(:ldap_settings) { { enabled: true } }
      let(:user) { create(:omniauth_user, provider: 'ldapmain') }
      let(:provider) { 'ldapmain' }
      let(:provider_label) { 'Main LDAP' }

      before do
        stub_ldap_setting(ldap_settings)
        stub_ldap_access(user, provider, provider_label)
        sign_in_using_ldap!(user, provider_label, provider)
      end

      after(:all) do
        Rails.application.reload_routes!
      end

      it 'renders 404' do
        visit edit_profile_password_path

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

  context 'Change password' do
    let(:new_password) { User.random_password }

    before do
      sign_in(user)
      visit(edit_profile_password_path)
    end

    shared_examples 'user enters an incorrect current password' do
      subject do
        page.within '.update-password' do
          fill_in 'user_password', with: user_current_password
          fill_passwords(new_password, new_password)
        end
      end

      it 'handles the invalid password attempt, and prompts the user to try again', :aggregate_failures do
        expect(Gitlab::AppLogger).to receive(:info)
          .with(message: 'Invalid current password when attempting to update user password', username: user.username, ip: user.current_sign_in_ip)

        subject

        user.reload

        expect(user.failed_attempts).to eq(1)
        expect(user.valid_password?(new_password)).to eq(false)
        expect(page).to have_current_path(edit_profile_password_path, ignore_query: true)

        page.within '.flash-container' do
          expect(page).to have_content('You must provide a valid current password')
        end
      end

      it 'locks the user account when user passes the maximum attempts threshold', :aggregate_failures do
        user.update!(failed_attempts: User.maximum_attempts.pred)

        subject

        expect(page).to have_current_path(new_user_session_path, ignore_query: true)

        page.within '.flash-container' do
          expect(page).to have_content('Your account is locked.')
        end
      end
    end

    context 'when current password is blank' do
      let(:user_current_password) { nil }

      it_behaves_like 'user enters an incorrect current password'
    end

    context 'when current password is incorrect' do
      let(:user_current_password) { 'invalid' }

      it_behaves_like 'user enters an incorrect current password'
    end

    context 'when the password is too weak' do
      let(:new_password) { 'password' }

      subject do
        page.within '.update-password' do
          fill_in "user_password", with: user.password
          fill_passwords(new_password, new_password)
        end
      end

      it 'tracks the error and does not change the password', :aggregate_failures do
        expect { subject }.not_to change { user.reload.valid_password?(new_password) }
        expect(user.failed_attempts).to eq(0)

        page.within '.gl-alert-danger' do
          expect(page).to have_content('must not contain commonly used combinations of words and letters')
        end

        expect_snowplow_event(
          category: 'Gitlab::Tracking::Helpers::WeakPasswordErrorEvent',
          action: 'track_weak_password_error',
          controller: 'Profiles::PasswordsController',
          method: 'update'
        )
      end
    end

    context 'when the password reset is successful' do
      subject do
        page.within '.update-password' do
          fill_in "user_password", with: user.password
          fill_passwords(new_password, new_password)
        end
      end

      it 'changes the password, logs the user out and prompts them to sign in again', :aggregate_failures do
        expect { subject }.to change { user.reload.valid_password?(new_password) }.to(true)
        expect(page).to have_current_path new_user_session_path, ignore_query: true

        page.within '.flash-container' do
          expect(page).to have_content('Password was successfully updated. Please sign in again.')
        end
      end
    end
  end

  context 'when password is expired' do
    let(:new_password) { User.random_password }

    before do
      sign_in(user)

      user.update!(password_expires_at: 1.hour.ago)
      user.identities.delete
      expect(user.ldap_user?).to eq false
    end

    it 'needs change user password' do
      visit edit_profile_password_path

      expect(page).to have_current_path new_profile_password_path, ignore_query: true

      fill_in :user_password,      with: user.password
      fill_in :user_new_password,  with: new_password
      fill_in :user_password_confirmation, with: new_password
      click_button 'Set new password'

      expect(page).to have_current_path new_user_session_path, ignore_query: true
    end

    it 'tracks weak password error' do
      visit edit_profile_password_path

      expect(page).to have_current_path new_profile_password_path, ignore_query: true

      fill_in :user_password,      with: user.password
      fill_in :user_new_password,  with: "password"
      fill_in :user_password_confirmation, with: "password"
      click_button 'Set new password'
      expect_snowplow_event(
        category: 'Gitlab::Tracking::Helpers::WeakPasswordErrorEvent',
        action: 'track_weak_password_error',
        controller: 'Profiles::PasswordsController',
        method: 'create'
      )
    end

    context 'when global require_two_factor_authentication is enabled' do
      it 'needs change user password' do
        stub_application_setting(require_two_factor_authentication: true)

        visit profile_path

        expect(page).to have_current_path new_profile_password_path, ignore_query: true
      end
    end
  end
end