change_password_spec.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::ChangePassword do
  4. let(:user) { create(:user, password: 'password') }
  5. let(:service) { described_class.new(user: user, current_password: current_password, new_password: new_password) }
  6. shared_examples 'raising an error' do |klass, message|
  7. it 'raises an error' do
  8. expect { service.execute }.to raise_error(klass, message)
  9. end
  10. end
  11. describe '#execute' do
  12. context 'with not matching current password' do
  13. let(:current_password) { 'foobar' }
  14. let(:new_password) { 'new_password' }
  15. it_behaves_like 'raising an error', PasswordHash::Error, 'The password is invalid.'
  16. end
  17. context 'with password policy violation' do
  18. let(:current_password) { 'password' }
  19. let(:new_password) { 'FooBarbazbaz' }
  20. it_behaves_like 'raising an error', PasswordPolicy::Error, 'Invalid password, it must contain at least 1 digit!'
  21. end
  22. context 'with valid passwords' do
  23. let(:current_password) { 'password' }
  24. let(:new_password) { 'IamAnValidPassword111einseinself' }
  25. it 'returns true' do
  26. expect(service.execute).to be_truthy
  27. end
  28. it 'changes the password' do
  29. expect { service.execute }.to change { user.reload.password }
  30. end
  31. it 'notifies the user' do
  32. allow(NotificationFactory::Mailer).to receive(:notification).with(
  33. template: 'password_change',
  34. user: user,
  35. objects: {
  36. user: user,
  37. }
  38. )
  39. service.execute
  40. expect(NotificationFactory::Mailer).to have_received(:notification)
  41. end
  42. end
  43. end
  44. end