# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Resolvers::Admin::CloudLicenses::CurrentLicenseResolver do
  include GraphqlHelpers

  specify do
    expect(described_class).to have_nullable_graphql_type(::Types::Admin::CloudLicenses::CurrentLicenseType)
  end

  describe '#resolve' do
    subject(:result) { resolve_current_license }

    let_it_be(:admin) { create(:admin) }
    let_it_be(:license) { create_current_license }

    def resolve_current_license(current_user: admin)
      resolve(described_class, ctx: { current_user: current_user })
    end

    before do
      stub_application_setting(cloud_license_enabled: true)
    end

    context 'when application setting for cloud license is disabled', :enable_admin_mode do
      it 'returns nil' do
        stub_application_setting(cloud_license_enabled: false)

        expect(result).to be_nil
      end
    end

    context 'when current user is unauthorized' do
      it 'raises error' do
        unauthorized_user = create(:user)

        expect do
          resolve_current_license(current_user: unauthorized_user)
        end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end

    context 'when there is no current license', :enable_admin_mode do
      it 'returns nil' do
        License.delete_all # delete existing license

        expect(result).to be_nil
      end
    end

    it 'returns the current license', :enable_admin_mode do
      expect(result).to eq(license)
    end
  end
end
