email_verify_spec.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'User email verify endpoint', authenticated_as: false, type: :request do
  4. context 'when user verifies via email' do
  5. let(:user) { create(:user, verified: false) }
  6. let(:params) { { token: token } }
  7. shared_examples 'returning unprocessable entity' do |message|
  8. it 'returns unprocessable entity' do
  9. expect(response).to have_http_status(:unprocessable_entity).and have_attributes(body: include(message))
  10. end
  11. end
  12. shared_examples 'returning success' do
  13. it 'returns success' do
  14. expect(response).to have_http_status(:ok)
  15. end
  16. end
  17. before do
  18. disable_user_create_account if defined?(disable_user_create_account)
  19. post api_v1_users_email_verify_path, params: params
  20. end
  21. context 'with disabled user signup' do
  22. let(:token) { User.signup_new_token(user)[:token].token } # NB: Don't ask!
  23. let(:disable_user_create_account) do
  24. Setting.set('user_create_account', false)
  25. end
  26. it_behaves_like 'returning unprocessable entity', 'This feature is not enabled.'
  27. end
  28. context 'with a valid token' do
  29. let(:token) { User.signup_new_token(user)[:token].token } # NB: Don't ask!
  30. it_behaves_like 'returning success'
  31. end
  32. context 'without a token parameter' do
  33. let(:params) { { foo: 'bar' } }
  34. it_behaves_like 'returning unprocessable entity', 'No token!'
  35. end
  36. context 'with an invalid token' do
  37. let(:token) { SecureRandom.urlsafe_base64(48) }
  38. it_behaves_like 'returning unprocessable entity', 'The provided token is invalid.'
  39. end
  40. end
  41. end