50
     ˆÂÁÃ544ÀÇÆ      ‹      ½•M�Ú0†ïû+\zK†ð±]í&®(A**ˆ¥ªªª'6ÄÚÄŽlo�þú:$ìÒ
ú!QN'ofÞg<‰ÃWñt°ø<¢÷‹û1š}|7ö >u ñ"®týV 0œ`„3kË[€õzí¯;¾Ò+XÌ!³EÞcµH­Ï,Ãä*¬î‘+fœ2wE¡6çd$ß µD ž¬Œ·÷0U0\²¥È¹Ïx¡üÌø©R¥+ÀTj<cò½º¹H`FXî¹PS½¾)Èx^†PWsõ¡1&Šm+_ÁÅ<¸R;pšì¬T¡&¡ÍÐ7š‹•Œ°U%&¡(VÈè4Â R%$9•�þJ,1¢¹�ð—Ñ`úÕéÀfÕÛ$¤(Ó|á·ƒhr7�bL&´à!Ðãš{§éc2¦Æ¢B1±œ�?ÔâñýtÂ¸ÖÄÜ¤Z”V(ù"‡xˆšªÜ”Ô±öA¦Uµ„ý©4ý©³þ|8YÄ£yÝV¥xq÷ßv“Õ\ZÍS«ô¶a®¼–‰)ïöKÔi±Ê,&yè¨òÚð$¥ZËÃN¸.íA_î“&84ú‹³v«ÝñZ�\£ sÛk!tJ—Dpy¨áÆ×Ì`r°8JpÓ»$J5=Îz‚É>:ê^¸<Äæâ,CÕ¹ysÉ�(©Íš‘zÏómüã/ÿ…Ðœ!ìÎ©êóósù¢    E   ˆl–ß=¿Jå2ÛR‚ œPA¸&n41hß�þDÉYYÆÚzB§�²?Åƒ|/;Ä_�uÐb&=LyZËcõÉÈ #ã    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'External Credentials', type: :request do
  let(:admin) { create(:admin) }

  context 'without authentication' do
    describe '#index' do
      it 'returns 403 Forbidden' do
        get '/api/v1/external_credentials', as: :json

        expect(response).to have_http_status(:forbidden)
        expect(json_response).to include('error' => 'Authentication required')
      end
    end

    describe '#app_verify' do
      it 'returns 403 Forbidden' do
        post '/api/v1/external_credentials/facebook/app_verify', as: :json

        expect(response).to have_http_status(:forbidden)
        expect(json_response).to include('error' => 'Authentication required')
      end
    end

    describe '#link_account' do
      it 'returns 403 Forbidden' do
        get '/api/v1/external_credentials/facebook/link_account', as: :json

        expect(response).to have_http_status(:forbidden)
        expect(json_response).to include('error' => 'Authentication required')
      end
    end

    describe '#callback' do
      it 'returns 403 Forbidden' do
        get '/api/v1/external_credentials/facebook/callback', as: :json

        expect(response).to have_http_status(:forbidden)
        expect(json_response).to include('error' => 'Authentication required')
      end
    end
  end

  context 'authenticated as admin' do
    before { authenticated_as(admin, via: :browser) }

    describe '#index' do
      it 'responds with an array of ExternalCredential records' do
        get '/api/v1/external_credentials', as: :json

        expect(response).to have_http_status(:ok)
        expect(json_response).to eq([])
      end

      context 'with expand=true URL parameters' do
        it 'responds with an array of ExternalCredential records and their association data' do
          get '/api/v1/external_credentials?expand=true', as: :json

          expect(response).to have_http_status(:ok)
          expect(json_response).to eq([])
        end
      end

      context 'with some content' do
        let(:credential) { create(:facebook_credential) }

        before { credential }

        it 'responds with sanitized data' do
          get '/api/v1/external_credentials?expand=true', as: :json

          expect(response).to have_http_status(:ok)
          expect(json_response).to include(
            include(
              'id'          => credential.id,
              'name'        => credential.name,
              'credentials' => {
                'application_id'     => credential[:credentials][:application_id],
                'application_secret' => SensitiveParamsHelper::SENSITIVE_MASK,
              }
            )
          )
        end
      end
    end

    context 'for Microsoft 365 in online service mode with a multi_tenant_app credential (PKCE)' do
      let(:client_id)     { 'pkce_client' }
      let(:client_secret) { 'pkce_secret' }

      before do
        Setting.set('system_online_service', true)
        create(:external_credential, name: 'microsoft365', credentials: { client_id: client_id, client_secret: client_secret, multi_tenant_app: true })
      end

      describe '#link_account' do
        it 'redirects with code_challenge and stores the code_verifier in the session', :aggregate_failures do
          get '/api/v1/external_credentials/microsoft365/link_account'

     