email_verify_send_spec.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'User endpoint', authenticated_as: false, type: :request do
  4. context 'when user resends verification email once' do
  5. it 'creates a token' do
  6. expect do
  7. post api_v1_users_email_verify_send_path, params: { email: create(:user).email }
  8. end.to change(Token, :count)
  9. end
  10. it 'returns success' do
  11. post api_v1_users_email_verify_send_path, params: { email: create(:user).email }
  12. expect(response).to have_http_status(:ok)
  13. end
  14. end
  15. # For the throttling, see config/initializers/rack_attack.rb.
  16. context 'when user resets password more than throttle allows', :rack_attack do
  17. let(:static_email) { create(:user).email }
  18. let(:static_ipv4) { Faker::Internet.ip_v4_address }
  19. it 'blocks due to email throttling (multiple IPs)' do
  20. 4.times do
  21. post api_v1_users_email_verify_send_path, params: { email: static_email }, headers: { 'X-Forwarded-For': Faker::Internet.ip_v4_address }
  22. end
  23. expect(response).to have_http_status(:too_many_requests)
  24. end
  25. it 'blocks due to source IP address throttling (multiple emails)' do
  26. 4.times do
  27. # Ensure throttling even on modified path.
  28. post "#{api_v1_users_email_verify_send_path}.json", params: { email: create(:user).login }, headers: { 'X-Forwarded-For': static_ipv4 }
  29. end
  30. expect(response).to have_http_status(:too_many_requests)
  31. end
  32. end
  33. end