0
  F   	ˆl–ßi~”Šj"Tâ‚Æ[¸ØÅ£�þE$…YÆäd2†2¼Yþ¿„]h_Ä_�uÐb&=LyZËcõÃÂ -Þ    	# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'GettingStarted', :aggregate_failures, type: :request do
  describe 'GET /api/v1/getting_started' do
    context 'when system is not yet set up' do
      it 'returns setup status without authentication' do
        get '/api/v1/getting_started', as: :json

        expect(response).to have_http_status(:ok)
        expect(json_response).to include(
          'setup_done' => false,
        )
      end
    end

    # A migration is started anonymously from the installer and imports users, so
    # it pushes the user count past the setup_done threshold while it runs. The
    # progress screens have to keep working without a user, but they read nothing
    # but import_mode and import_backend.
    context 'when a migration is running' do
      before do
        Setting.set('import_mode', true)
        Setting.set('import_backend', 'otrs')
        Setting.set('system_init_done', false)
        create_list(:customer, 3)
      end

      # The progress screens navigate by import_backend and stay on their screen
      # as long as import_mode is on, so both have to be answered.
      it 'answers an anonymous request with the migration information' do
        get '/api/v1/getting_started', as: :json

        expect(response).to have_http_status(:ok)
        expect(json_response).to include(
          'setup_done'     => true,
          'import_mode'    => true,
          'import_backend' => 'otrs',
        )
      end

      # An import only clears import_mode when it succeeds, so a failed or
      # abandoned migration leaves the window open with the imported groups and
      # addresses already in the database. It must not disclose them.
      it 'does not disclose the group or email address list to an anonymous request' do
        create(:group)

        get '/api/v1/getting_started', as: :json

        expect(json_response.keys).not_to include('groups', 'addresses', 'config', 'channel_driver')
      end

      # The importers assign the Admin role to imported users, so a migration
      # acquires admins while it runs. That must not close the window.
      it 'answers an anonymous request once the migration has imported an admin' do
        create(:admin)

        get '/api/v1/getting_started', as: :json

        expect(response).to have_http_status(:ok)
        expect(json_response).to include('import_mode' => true)
      end

      # The window is not bounded in time, so it is reduced for everyone rather
      # than by role: during a migration no screen needs the wizard data, and a
      # user who does has the dedicated group and email address endpoints.
      context 'when authenticated as admin', authenticated_as: :admin do
        let(:admin) { create(:admin) }

        it 'returns the migration information only' do
          get '/api/v1/getting_started', as: :json

          expect(response).to have_http_status(:ok)
          expect(json_response.keys).not_to include('groups', 'addresses', 'config', 'channel_driver')
        end
      end
    end

    # Regression guard for the unauthenticated disclosure fixed in 7.1.0. Only a
    # running migration is answered without a user, and an import refuses to start
    # once the setup is done (Import::Helper), so a real migration always has
    # import_mode on and system_init_done off. Every other combination of the two
    # has to stay closed, and each is reachable:
    #
    # - system_init_done is not monotonic, Service::System::CheckSetup resets it
    #   when it is set without an admin being present;
    # - an installed system can be left in import mode, which must not be enough
    #   on its own to open the payload.
    context 'when no migration is running' do
      before { create_list(:customer, 3) }

      [
        { import_mode: false, system_init_done: false },
        { import_mode: false, system_init_done: true },
        { import_mode: true,  system_ini