update_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::PasswordReset::Update do
  4. subject(:service) { described_class.new(token: token, password: password) }
  5. let(:user) { create(:user) }
  6. let(:token) { User.password_reset_new_token(user.login)[:token].token }
  7. let(:password) { 'Cw8OH8yT2b' }
  8. shared_examples 'raising an error' do |klass, message|
  9. it 'raises an error' do
  10. expect { service.execute }.to raise_error(klass, include(message))
  11. end
  12. end
  13. shared_examples 'changing password of the user' do
  14. it 'returns user' do
  15. expect(service.execute).to eq(user)
  16. end
  17. it 'changes password of the user' do
  18. expect { service.execute }.to change { user.reload.password }
  19. end
  20. it 'sends an email notification' do
  21. message = nil
  22. allow(NotificationFactory::Mailer).to receive(:deliver) do |params|
  23. message = params[:body]
  24. end
  25. service.execute
  26. expect(message).to include 'This activity is not known to you? If not, contact your system administrator.'
  27. end
  28. end
  29. describe '#execute' do
  30. context 'with disabled lost password feature' do
  31. before do
  32. Setting.set('user_lost_password', false)
  33. end
  34. it_behaves_like 'raising an error', Service::CheckFeatureEnabled::FeatureDisabledError, 'This feature is not enabled.'
  35. end
  36. context 'with a valid token and valid password' do
  37. it_behaves_like 'changing password of the user'
  38. end
  39. context 'with an invalid token' do
  40. let(:token) { SecureRandom.urlsafe_base64(48) }
  41. it_behaves_like 'raising an error', Service::User::PasswordReset::Update::InvalidTokenError, 'The provided token is invalid.'
  42. end
  43. context 'with an invalid password' do
  44. let(:password) { 'foobar9' }
  45. it_behaves_like 'raising an error', PasswordPolicy::Error, 'Invalid password'
  46. end
  47. end
  48. end