signup_verify_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::SignupVerify do
  4. subject(:service) { described_class.new(token: token) }
  5. let(:user) { create(:user, verified: false) }
  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 'returning the verified user' do
  12. it 'returns the verified user' do
  13. expect(service.execute).to eq(user).and have_attributes(verified: true)
  14. end
  15. end
  16. describe '#execute' do
  17. context 'with disabled user signup' do
  18. let(:token) { User.signup_new_token(user)[:token].token } # NB: Don't ask!
  19. before do
  20. Setting.set('user_create_account', 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. let(:token) { User.signup_new_token(user)[:token].token } # NB: Don't ask!
  26. it_behaves_like 'returning the verified user'
  27. end
  28. context 'without a token parameter' do
  29. let(:token) { nil }
  30. it_behaves_like 'raising an error', Service::User::SignupVerify::InvalidTokenError, 'The provided token is invalid.'
  31. end
  32. context 'with an invalid token' do
  33. let(:token) { SecureRandom.urlsafe_base64(48) }
  34. it_behaves_like 'raising an error', Service::User::SignupVerify::InvalidTokenError, 'The provided token is invalid.'
  35. end
  36. context 'with current user' do
  37. context 'when same as the user being verified' do
  38. subject(:service) { described_class.new(token: token, current_user: user) }
  39. let(:token) { User.signup_new_token(user)[:token].token } # NB: Don't ask!
  40. it_behaves_like 'returning the verified user'
  41. end
  42. context 'when different than the user being verified' do
  43. subject(:service) { described_class.new(token: token, current_user: agent) }
  44. let(:token) { User.signup_new_token(user)[:token].token } # NB: Don't ask!
  45. let(:agent) { create(:agent) }
  46. it_behaves_like 'raising an error', Service::User::SignupVerify::InvalidTokenError, 'The provided token is invalid.'
  47. end
  48. end
  49. end
  50. end