base_spec.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sessions::Event::Base do
  4. describe '#remote_ip' do
  5. let(:instance) { described_class.new(headers:, clients: {}) }
  6. context 'without X-Forwarded-For' do
  7. let(:headers) { {} }
  8. it 'returns no value' do
  9. expect(instance.remote_ip).to be_nil
  10. end
  11. end
  12. context 'with X-Forwarded-For' do
  13. before do
  14. allow(Rails.application.config.action_dispatch).to receive(:trusted_proxies).and_return(trusted_proxies)
  15. end
  16. let(:trusted_proxies) { ['127.0.0.1', '::1'] }
  17. context 'with external IP' do
  18. let(:headers) { { 'X-Forwarded-For' => '1.2.3.4 , 5.6.7.8, 127.0.0.1 , ::1' } }
  19. it 'returns the correct value' do
  20. expect(instance.remote_ip).to eq('5.6.7.8')
  21. end
  22. end
  23. context 'without external IP' do
  24. let(:headers) { { 'X-Forwarded-For' => ' 127.0.0.1 , ::1' } }
  25. it 'returns no value' do
  26. expect(instance.remote_ip).to be_nil
  27. end
  28. end
  29. end
  30. end
  31. end