# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe ExternalCredential::Exchange do
  describe '.link_account' do
    let(:request_token) { 'test_oauth_state' }

    context 'when OAuth state is invalid' do
      it 'raises an error' do
        expect do
          described_class.link_account(request_token, { state: 'wrong_state' })
        end.to raise_error(Exceptions::UnprocessableContent, 'Invalid OAuth state parameter.')
      end
    end
  end

  describe '.refresh_token' do
    # https://github.com/zammad/zammad/issues/4454
    context 'when Exchange integration is not configured at all' do
      before do
        Setting.set('exchange_oauth', {})
        Setting.set('exchange_integration', true)
      end

      it 'does always return a value' do
        expect(described_class.refresh_token).to be_truthy
      end
    end

    # https://github.com/zammad/zammad/issues/4961
    context 'when Exchange integration is not enabled' do
      before do
        Setting.set('exchange_integration', false)
      end

      it 'does always return a value' do
        expect(described_class.refresh_token).to be_truthy
      end
    end
  end

  describe '.update_client_secret' do
    let(:external_credential) { create(:external_credential, name: 'exchange', credentials:) }
    let(:credentials)         { { client_id: 'id1337', client_secret: 'dummy' } }

    context 'when Exchange integration is not completely configured' do
      before do
        Setting.set('exchange_oauth', { client_id: 'id1337' })
      end

      it 'does not update the setting' do
        external_credential.update!(credentials: credentials)

        expect(Setting.get('exchange_oauth')).to eq({ 'client_id' => 'id1337' })
      end
    end

    context 'when Exchange integration is completely configured' do
      before do
        Setting.set('exchange_oauth', { client_id: 'id1337', client_secret: 'dummy' })
      end

      context 'when client_secret is different' do
        let(:credentials)         { { client_id: 'id1337', client_secret: 'dummy-other' } }

        it 'does not update the setting' do
          external_credential.update!(credentials: credentials.merge(client_secret: 'new-dummy'))

          expect(Setting.get('exchange_oauth')).to eq({ 'client_id' => 'id1337', 'client_secret' => 'dummy' })
        end
      end

      context 'when client_secret is the same' do
        it 'does update the setting' do
          external_credential.update!(credentials: credentials.merge(client_secret: 'new-dummy'))

          expect(Setting.get('exchange_oauth')).to eq({ 'client_id' => 'id1337', 'client_secret' => 'new-dummy' })
        end
      end
    end
  end

  describe '.user_info' do
    it 'extracts user information from an unpadded base64url encoded id_token' do
      payload = Base64.urlsafe_encode64(JSON.generate({ email: 'j.mueller@example.com', name: 'Jürgen Müller?' }), padding: false)

      info = described_class.user_info("header.#{payload}.signature")
      expect(info).to include(email: 'j.mueller@example.com', name: 'Jürgen Müller?')
    end
  end
end
