diff --git a/spec/features/users/managing_password_spec.rb b/spec/features/users/managing_password_spec.rb new file mode 100644 index 000000000..3d0393f1d --- /dev/null +++ b/spec/features/users/managing_password_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +feature 'Managing password:' do + context 'for simple users' do + let(:user) { create(:user) } + let(:new_password) { 'a simple password' } + + scenario 'a simple user can reset their password' do + visit root_path + click_on 'Connexion' + click_on 'Mot de passe oublié ?' + expect(page).to have_current_path(new_user_password_path) + + fill_in 'Email', with: user.email + perform_enqueued_jobs do + click_on 'Réinitialiser' + end + expect(page).to have_content 'vous allez recevoir un lien de réinitialisation par email' + + click_reset_password_link_for user.email + expect(page).to have_content 'Changement de mot de passe' + + fill_in 'user_password', with: new_password + fill_in 'user_password_confirmation', with: new_password + click_on 'Changer le mot de passe' + expect(page).to have_content('Votre mot de passe a été changé avec succès') + end + end + + context 'for admins' do + let(:user) { create(:user) } + let(:administrateur) { create(:administrateur, user: user) } + let(:new_password) { 'a new, long, and complicated password!' } + + scenario 'an admin can reset their password' do + visit root_path + click_on 'Connexion' + click_on 'Mot de passe oublié ?' + expect(page).to have_current_path(new_user_password_path) + + fill_in 'Email', with: user.email + perform_enqueued_jobs do + click_on 'Réinitialiser' + end + expect(page).to have_content 'vous allez recevoir un lien de réinitialisation par email' + + click_reset_password_link_for user.email + + expect(page).to have_content 'Changement de mot de passe' + + fill_in 'user_password', with: new_password + fill_in 'user_password_confirmation', with: new_password + click_on 'Changer le mot de passe' + expect(page).to have_content('Votre mot de passe a été changé avec succès') + end + end +end diff --git a/spec/support/feature_helpers.rb b/spec/support/feature_helpers.rb index 00240a295..545406561 100644 --- a/spec/support/feature_helpers.rb +++ b/spec/support/feature_helpers.rb @@ -78,6 +78,13 @@ module FeatureHelpers value end end + + def click_reset_password_link_for(email) + reset_password_email = open_email(email) + token_params = reset_password_email.body.match(/reset_password_token=[^"]+/) + + visit "/users/password/edit?#{token_params}" + end end RSpec.configure do |config|