# frozen_string_literal: true

require 'spec_helper'

RSpec.describe InstanceConfiguration do
  context 'without cache' do
    describe '#settings' do
      describe '#ssh_algorithms_hashes' do
        let(:md5) { '5a:65:6c:4d:d4:4c:6d:e6:59:25:b8:cf:ba:34:e7:64' }
        let(:sha256) { 'SHA256:2KJDT7xf2i68mBgJ3TVsjISntg4droLbXYLfQj0VvSY' }

        it 'does not return anything if file does not exist' do
          stub_pub_file(pub_file(exist: false))

          expect(subject.settings[:ssh_algorithms_hashes]).to be_empty
        end

        it 'does not return anything if file is empty' do
          stub_pub_file(pub_file)

          stub_file_read(pub_file, content: '')

          expect(subject.settings[:ssh_algorithms_hashes]).to be_empty
        end

        it 'returns the md5 and sha256 if file valid and exists' do
          stub_pub_file(pub_file)

          result = subject.settings[:ssh_algorithms_hashes].select { |o| o[:md5] == md5 && o[:sha256] == sha256 }

          expect(result.size).to eq(InstanceConfiguration::SSH_ALGORITHMS.size)
        end

        def pub_file(exist: true)
          path = exist ? 'spec/fixtures/ssh_host_example_key.pub' : 'spec/fixtures/ssh_host_example_key.pub.random'

          Rails.root.join(path)
        end

        def stub_pub_file(path)
          allow(subject).to receive(:ssh_algorithm_file).and_return(path)
        end
      end

      describe '#host' do
        it 'returns current instance host' do
          allow(Settings.gitlab).to receive(:host).and_return('exampledomain')

          expect(subject.settings[:host]).to eq(Settings.gitlab.host)
        end
      end

      describe '#gitlab_pages' do
        let(:gitlab_pages) { subject.settings[:gitlab_pages] }

        it 'returns Settings.pages' do
          gitlab_pages.delete(:ip_address)

          expect(gitlab_pages).to eq(Settings.pages.symbolize_keys)
        end

        it 'returns the GitLab\'s pages host ip address' do
          expect(gitlab_pages.keys).to include(:ip_address)
        end

        it 'returns the ip address as nil if the domain is invalid' do
          allow(Settings.pages).to receive(:host).and_return('exampledomain')

          expect(gitlab_pages[:ip_address]).to eq nil
        end

        it 'returns the ip address of the domain' do
          allow(Settings.pages).to receive(:host).and_return('localhost')

          expect(gitlab_pages[:ip_address]).to eq('127.0.0.1').or eq('::1')
        end
      end

      describe '#gitlab_ci' do
        let(:gitlab_ci) { subject.settings[:gitlab_ci] }

        it 'returns Settings.gitalb_ci' do
          gitlab_ci.delete(:artifacts_max_size)

          expect(gitlab_ci).to eq(Settings.gitlab_ci.symbolize_keys)
        end

        it 'returns the key artifacts_max_size' do
          expect(gitlab_ci.keys).to include(:artifacts_max_size)
        end

        it 'returns the key artifacts_max_size with values' do
          stub_application_setting(max_artifacts_size: 200)

          expect(gitlab_ci[:artifacts_max_size][:default]).to eq(100.megabytes)
          expect(gitlab_ci[:artifacts_max_size][:value]).to eq(200.megabytes)
        end
      end

      describe '#rate_limits' do
        before do
          Gitlab::CurrentSettings.current_application_settings.update!(
            throttle_unauthenticated_enabled: false,
            throttle_unauthenticated_requests_per_period: 1001,
            throttle_unauthenticated_period_in_seconds: 1002,
            throttle_authenticated_api_enabled: true,
            throttle_authenticated_api_requests_per_period: 1003,
            throttle_authenticated_api_period_in_seconds: 1004,
            throttle_authenticated_web_enabled: true,
            throttle_authenticated_web_requests_per_period: 1005,
            throttle_authenticated_web_period_in_seconds: 1006,
            throttle_protected_paths_enabled: true,
            throttle_protected_paths_requests_per_period: 1007,
            throttle_protected_paths_period_in_seconds: 1008,
            throttle_unauthenticated_packages_api_enabled: false,
            throttle_unauthenticated_packages_api_requests_per_period: 1009,
            throttle_unauthenticated_packages_api_period_in_seconds: 1010,
            throttle_authenticated_packages_api_enabled: true,
            throttle_authenticated_packages_api_requests_per_period: 1011,
            throttle_authenticated_packages_api_period_in_seconds: 1012,
            issues_create_limit: 1013,
            notes_create_limit: 1014,
            project_export_limit: 1015,
            project_download_export_limit: 1016,
            project_import_limit: 1017,
            group_export_limit: 1018,
            group_download_export_limit: 1019,
            group_import_limit: 1020,
            raw_blob_request_limit: 1021
          )
        end

        it 'returns rate limits from application settings' do
          rate_limits = subject.settings[:rate_limits]

          expect(rate_limits[:unauthenticated]).to eq({ enabled: false, requests_per_period: 1001, period_in_seconds: 1002 })
          expect(rate_limits[:authenticated_api]).to eq({ enabled: true, requests_per_period: 1003, period_in_seconds: 1004 })
          expect(rate_limits[:authenticated_web]).to eq({ enabled: true, requests_per_period: 1005, period_in_seconds: 1006 })
          expect(rate_limits[:protected_paths]).to eq({ enabled: true, requests_per_period: 1007, period_in_seconds: 1008 })
          expect(rate_limits[:unauthenticated_packages_api]).to eq({ enabled: false, requests_per_period: 1009, period_in_seconds: 1010 })
          expect(rate_limits[:authenticated_packages_api]).to eq({ enabled: true, requests_per_period: 1011, period_in_seconds: 1012 })
          expect(rate_limits[:issue_creation]).to eq({ enabled: true, requests_per_period: 1013, period_in_seconds: 60 })
          expect(rate_limits[:note_creation]).to eq({ enabled: true, requests_per_period: 1014, period_in_seconds: 60 })
          expect(rate_limits[:project_export]).to eq({ enabled: true, requests_per_period: 1015, period_in_seconds: 60 })
          expect(rate_limits[:project_export_download]).to eq({ enabled: true, requests_per_period: 1016, period_in_seconds: 60 })
          expect(rate_limits[:project_import]).to eq({ enabled: true, requests_per_period: 1017, period_in_seconds: 60 })
          expect(rate_limits[:group_export]).to eq({ enabled: true, requests_per_period: 1018, period_in_seconds: 60 })
          expect(rate_limits[:group_export_download]).to eq({ enabled: true, requests_per_period: 1019, period_in_seconds: 60 })
          expect(rate_limits[:group_import]).to eq({ enabled: true, requests_per_period: 1020, period_in_seconds: 60 })
          expect(rate_limits[:raw_blob]).to eq({ enabled: true, requests_per_period: 1021, period_in_seconds: 60 })
        end
      end
    end
  end

  context 'with cache', :use_clean_rails_memory_store_caching do
    it 'caches settings content' do
      expect(Rails.cache.read(described_class::CACHE_KEY)).to be_nil

      settings = subject.settings

      expect(Rails.cache.read(described_class::CACHE_KEY)).to eq(settings)
    end

    describe 'cached settings' do
      before do
        subject.settings
      end

      it 'expires after EXPIRATION_TIME' do
        allow(Time).to receive(:now).and_return(Time.current + described_class::EXPIRATION_TIME)
        Rails.cache.cleanup

        expect(Rails.cache.read(described_class::CACHE_KEY)).to eq(nil)
      end
    end
  end
end
