signup_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::Signup do
  4. subject(:service) { described_class.new(user_data: user_data, resend: resend) }
  5. let(:resend) { false }
  6. let(:user_data) do
  7. {
  8. email: 'bender@futurama.fiction',
  9. firstname: 'Bender',
  10. lastname: 'Rodríguez',
  11. password: 'IloveBender1337'
  12. }
  13. end
  14. shared_examples 'raising an error' do |klass, message|
  15. it 'raises an error' do
  16. expect { service.execute }.to raise_error(klass, include(message))
  17. end
  18. end
  19. shared_examples 'returning success' do |with_new_user: false, with_existing_user: false, with_resend: false|
  20. it 'returns success' do
  21. expect(service.execute).to be(true)
  22. end
  23. it 'creates an unverified user account', if: with_new_user do
  24. service.execute
  25. expect(User.find_by(email: 'bender@futurama.fiction')).to be_present.and have_attributes(verified: false)
  26. end
  27. it 'sends an email with the verification link', if: with_new_user || (!with_existing_user && with_resend) do
  28. message = nil
  29. allow(NotificationFactory::Mailer).to receive(:deliver) do |params|
  30. message = params[:body]
  31. end
  32. service.execute
  33. expect(message).to include("<a href=\"http://zammad.example.com/desktop/signup/verify/#{Token.last[:token]}\">")
  34. end
  35. it 'sends an email with the password reset link', if: with_existing_user && !with_resend do
  36. message = nil
  37. allow(NotificationFactory::Mailer).to receive(:deliver) do |params|
  38. message = params[:body]
  39. end
  40. service.execute
  41. expect(message).to include("<a href=\"http://zammad.example.com/desktop/reset-password/verify/#{Token.last[:token]}\">")
  42. end
  43. it 'sends no email', if: !with_new_user && with_existing_user && with_resend do
  44. message = nil
  45. allow(NotificationFactory::Mailer).to receive(:deliver) do |params|
  46. message = params[:body]
  47. end
  48. service.execute
  49. expect(message).to be_nil
  50. end
  51. end
  52. describe '#execute' do
  53. context 'with disabled user signup' do
  54. before do
  55. Setting.set('user_create_account', false)
  56. end
  57. it_behaves_like 'raising an error', Service::CheckFeatureEnabled::FeatureDisabledError, 'This feature is not enabled.'
  58. end
  59. context 'with valid user data' do
  60. it_behaves_like 'returning success', with_new_user: true
  61. end
  62. context 'with invalid user data' do
  63. let(:password) { 'IloveBender1337' }
  64. let(:user_data) do
  65. {
  66. email: 'bender@futurama.fiction',
  67. firstname: 'Bender',
  68. lastname: 'Rodríguez',
  69. password: password
  70. }
  71. end
  72. context 'when the password is weak' do
  73. let(:password) { 'idonotlovebenderandthisiswrong' }
  74. it_behaves_like 'raising an error', PasswordPolicy::Error, 'Invalid password'
  75. end
  76. context 'when the email is already taken' do
  77. before do
  78. create(:user, email: 'bender@futurama.fiction')
  79. end
  80. it_behaves_like 'returning success', with_existing_user: true
  81. end
  82. end
  83. context 'when resending verification email' do
  84. let(:resend) { true }
  85. let(:verified) { false }
  86. before do
  87. create(:user, email: 'bender@futurama.fiction', verified: verified)
  88. end
  89. it_behaves_like 'returning success', with_resend: true
  90. context 'when user is already verified' do
  91. let(:verified) { true }
  92. it_behaves_like 'returning success', with_existing_user: true, with_resend: true
  93. end
  94. end
  95. end
  96. end