send_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::PasswordReset::Send do
  4. subject(:service) { described_class.new(username: user.login) }
  5. let(:user) { create(:user) }
  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. shared_examples 'sending the token' do
  12. it 'returns success' do
  13. expect(service.execute).to be(true)
  14. end
  15. it 'generates a new token' do
  16. expect { service.execute }.to change(Token, :count)
  17. end
  18. it 'sends a valid password reset link' do
  19. message = nil
  20. allow(NotificationFactory::Mailer).to receive(:deliver) do |params|
  21. message = params[:body]
  22. end
  23. service.execute
  24. expect(message).to include "<a href=\"http://zammad.example.com/desktop/reset-password/verify/#{Token.last.token}\">"
  25. end
  26. end
  27. shared_examples 'returning success' do
  28. it 'returns success' do
  29. expect(service.execute).to be(true)
  30. end
  31. it 'does not generate a new token' do
  32. expect { service.execute }.to not_change(Token, :count)
  33. end
  34. end
  35. describe '#execute' do
  36. context 'with disabled lost password feature' do
  37. before do
  38. Setting.set('user_lost_password', false)
  39. end
  40. it_behaves_like 'raising an error', Service::CheckFeatureEnabled::FeatureDisabledError, 'This feature is not enabled.'
  41. end
  42. context 'with a valid user login' do
  43. it_behaves_like 'sending the token'
  44. end
  45. context 'with a valid user email' do
  46. subject(:service) { described_class.new(username: user.email) }
  47. it_behaves_like 'sending the token'
  48. end
  49. context 'with an invalid user login' do
  50. subject(:service) { described_class.new(username: 'foobar') }
  51. it_behaves_like 'returning success'
  52. end
  53. end
  54. end