2019-07-25 01:24:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-08 10:03:30 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe 'Profile > Password' do
|
2018-02-12 14:17:19 -05:00
|
|
|
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
|
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
context 'Password authentication enabled' do
|
|
|
|
let(:user) { create(:user, password_automatically_set: true) }
|
2016-08-08 10:03:30 -04:00
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
visit edit_profile_password_path
|
|
|
|
end
|
2016-08-08 10:03:30 -04:00
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
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('mypassword', 'mypassword2')
|
2016-08-08 10:03:30 -04:00
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
page.within('.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('mypassword', 'mypassword2')
|
2016-08-08 10:03:30 -04:00
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
expect(page).to have_no_field('user[current_password]')
|
2016-08-08 10:03:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
describe 'User puts the same passwords in the field and in the confirmation' do
|
|
|
|
it 'shows a success message' do
|
2022-01-10 10:14:26 -05:00
|
|
|
fill_passwords(Gitlab::Password.test_default, Gitlab::Password.test_default)
|
2016-08-08 10:03:30 -04:00
|
|
|
|
2022-02-10 07:18:48 -05:00
|
|
|
page.within('[data-testid="alert-info"]') do
|
2020-08-10 08:09:55 -04:00
|
|
|
expect(page).to have_content('Password was successfully updated. Please sign in again.')
|
2017-06-27 14:02:09 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-08 10:03:30 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-27 14:02:09 -04:00
|
|
|
end
|
2016-08-08 10:03:30 -04:00
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
context 'Password authentication unavailable' do
|
|
|
|
before do
|
|
|
|
gitlab_sign_in(user)
|
|
|
|
end
|
2016-08-08 10:03:30 -04:00
|
|
|
|
2017-06-27 14:02:09 -04:00
|
|
|
context 'Regular user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2017-11-23 08:16:14 -05:00
|
|
|
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)
|
2017-06-27 14:02:09 -04:00
|
|
|
|
|
|
|
visit edit_profile_password_path
|
|
|
|
|
2020-01-24 10:09:00 -05:00
|
|
|
expect(page).to have_gitlab_http_status(:not_found)
|
2017-06-27 14:02:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'LDAP user' do
|
|
|
|
let(:user) { create(:omniauth_user, provider: 'ldapmain') }
|
|
|
|
|
|
|
|
it 'renders 404' do
|
|
|
|
visit edit_profile_password_path
|
|
|
|
|
2020-01-24 10:09:00 -05:00
|
|
|
expect(page).to have_gitlab_http_status(:not_found)
|
2016-08-08 10:03:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-12 14:17:19 -05:00
|
|
|
|
2021-09-30 14:11:31 -04:00
|
|
|
context 'Change password' do
|
2022-01-10 10:14:26 -05:00
|
|
|
let(:new_password) { "new" + Gitlab::Password.test_default }
|
2021-09-30 14:11:31 -04:00
|
|
|
|
2018-02-12 14:17:19 -05:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
visit(edit_profile_password_path)
|
|
|
|
end
|
|
|
|
|
2021-09-30 14:11:31 -04:00
|
|
|
shared_examples 'user enters an incorrect current password' do
|
|
|
|
subject do
|
|
|
|
page.within '.update-password' do
|
2021-10-12 14:12:42 -04:00
|
|
|
fill_in 'user_password', with: user_current_password
|
2021-09-30 14:11:31 -04:00
|
|
|
fill_passwords(new_password, new_password)
|
|
|
|
end
|
2018-02-12 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2021-09-30 14:11:31 -04:00
|
|
|
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
|
2018-02-12 14:17:19 -05:00
|
|
|
|
2021-09-30 14:11:31 -04:00
|
|
|
expect(user.failed_attempts).to eq(1)
|
|
|
|
expect(user.valid_password?(new_password)).to eq(false)
|
|
|
|
expect(current_path).to eq(edit_profile_password_path)
|
|
|
|
|
|
|
|
page.within '.flash-container' do
|
|
|
|
expect(page).to have_content('You must provide a valid current password')
|
|
|
|
end
|
2018-02-12 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2021-09-30 14:11:31 -04:00
|
|
|
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(current_path).to eq(new_user_session_path)
|
|
|
|
|
|
|
|
page.within '.flash-container' do
|
|
|
|
expect(page).to have_content('Your account is locked.')
|
|
|
|
end
|
2018-02-12 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 14:11:31 -04:00
|
|
|
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
|
2021-10-12 14:12:42 -04:00
|
|
|
let(:user_current_password) { 'invalid' }
|
2021-09-30 14:11:31 -04:00
|
|
|
|
|
|
|
it_behaves_like 'user enters an incorrect current password'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the password reset is successful' do
|
|
|
|
subject do
|
|
|
|
page.within '.update-password' do
|
2021-10-12 14:12:42 -04:00
|
|
|
fill_in "user_password", with: user.password
|
2021-09-30 14:11:31 -04:00
|
|
|
fill_passwords(new_password, new_password)
|
|
|
|
end
|
2018-02-12 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2021-09-30 14:11:31 -04:00
|
|
|
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(current_path).to eq new_user_session_path
|
|
|
|
|
|
|
|
page.within '.flash-container' do
|
|
|
|
expect(page).to have_content('Password was successfully updated. Please sign in again.')
|
|
|
|
end
|
|
|
|
end
|
2018-02-12 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when password is expired' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
|
2021-03-31 08:08:55 -04:00
|
|
|
user.update!(password_expires_at: 1.hour.ago)
|
2018-02-12 14:17:19 -05:00
|
|
|
user.identities.delete
|
|
|
|
expect(user.ldap_user?).to eq false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'needs change user password' do
|
|
|
|
visit edit_profile_password_path
|
|
|
|
|
|
|
|
expect(current_path).to eq new_profile_password_path
|
|
|
|
|
2021-10-12 14:12:42 -04:00
|
|
|
fill_in :user_password, with: user.password
|
2022-01-10 10:14:26 -05:00
|
|
|
fill_in :user_new_password, with: Gitlab::Password.test_default
|
|
|
|
fill_in :user_password_confirmation, with: Gitlab::Password.test_default
|
2018-02-12 14:17:19 -05:00
|
|
|
click_button 'Set new password'
|
|
|
|
|
|
|
|
expect(current_path).to eq new_user_session_path
|
|
|
|
end
|
2018-03-01 11:28:15 -05:00
|
|
|
|
|
|
|
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(current_path).to eq new_profile_password_path
|
|
|
|
end
|
|
|
|
end
|
2018-02-12 14:17:19 -05:00
|
|
|
end
|
2016-08-08 10:03:30 -04:00
|
|
|
end
|