# frozen_string_literal: true

describe IPService do
  describe '.ip_trusted?' do
    after(:each) { ENV['TRUSTED_NETWORKS'] = nil }

    subject { IPService.ip_trusted?(ip) }

    context 'when the ip is nil' do
      let(:ip) { nil }

      it { is_expected.to be(false) }
    end

    context 'when the ip is defined' do
      let(:ip) { '192.168.1.10' }

      context 'when it belongs to a trusted network' do
        before do
          ENV['TRUSTED_NETWORKS'] = '10.0.0.0/8 192.168.0.0/16 bad_network'
        end

        it { is_expected.to be(true) }
      end

      context 'when it does not belong to a trusted network' do
        before do
          ENV['TRUSTED_NETWORKS'] = '10.0.0.0/8'
        end

        it { is_expected.to be(false) }
      end

      context 'when the trusted network is not defined' do
        it { is_expected.to be(false) }
      end

      context 'when the trusted network is malformed' do
        before do
          ENV['TRUSTED_NETWORKS'] = 'bad network'
        end

        it { is_expected.to be(false) }
      end
    end

    context 'when a trusted network is defined' do
      before { ENV['TRUSTED_NETWORKS'] = '10.0.0.0/8' }

      context 'when the ip is nil' do
        let(:ip) { nil }

        it { is_expected.to be(false) }
      end

      context 'when the ip is badly formatted' do
        let(:ip) { 'yop' }

        it { is_expected.to be(false) }
      end
    end
  end
end
