verify_spec.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::PasswordReset::Verify do
  4. subject(:service) { described_class.new(token: token) }
  5. let(:user) { create(:user) }
  6. let(:token) { User.password_reset_new_token(user.login)[:token].token }
  7. shared_examples 'raising an error' do |klass, message|
  8. it 'raises an error' do
  9. expect { service.execute }.to raise_error(klass, message)
  10. end
  11. end
  12. shared_examples 'returning user' do
  13. it 'returns user' do
  14. expect(service.execute).to eq(user)
  15. end
  16. end
  17. describe '#execute' do
  18. context 'with disabled lost password feature' do
  19. before do
  20. Setting.set('user_lost_password', false)
  21. end
  22. it_behaves_like 'raising an error', Service::CheckFeatureEnabled::FeatureDisabledError, 'This feature is not enabled.'
  23. end
  24. context 'with a valid token' do
  25. it_behaves_like 'returning user'
  26. end
  27. context 'with an invalid token' do
  28. let(:token) { SecureRandom.urlsafe_base64(48) }
  29. it_behaves_like 'raising an error', Service::User::PasswordReset::Verify::InvalidTokenError, 'The provided token is invalid.'
  30. end
  31. end
  32. end