@@ -8,7 +8,14 @@ import * as actionsUtil from "./actions-util";
88import * as api from "./api-client" ;
99import { DO_NOT_RETRY_STATUSES } from "./api-client" ;
1010import { ActionsEnvVars , RegistryProxyVars } from "./environment" ;
11- import { callee , getTestEnv , setupTests } from "./testing-utils" ;
11+ import { GITHUB_DOTCOM_API_URL , GITHUB_DOTCOM_URL } from "./github" ;
12+ import {
13+ callee ,
14+ getTestEnv ,
15+ RecordingLogger ,
16+ SAMPLE_DOTCOM_API_DETAILS ,
17+ setupTests ,
18+ } from "./testing-utils" ;
1219import * as util from "./util" ;
1320
1421setupTests ( test ) ;
@@ -284,3 +291,87 @@ test("makeProxyRequestOptions - returns fetch with custom proxy", async (t) => {
284291 githubUtils . defaults . request ,
285292 ) ;
286293} ) ;
294+
295+ test . serial (
296+ "getApiClientWithExternalAuth constructs clients with or without tokens" ,
297+ ( t ) => {
298+ t . notThrows ( ( ) => {
299+ api . getApiClientWithExternalAuth ( SAMPLE_DOTCOM_API_DETAILS ) ;
300+ api . getApiClientWithExternalAuth ( {
301+ ...SAMPLE_DOTCOM_API_DETAILS ,
302+ auth : undefined ,
303+ } ) ;
304+ } ) ;
305+ } ,
306+ ) ;
307+
308+ test ( "getAuthorizationHeaderFor - returns undefined if there's no token" , ( t ) => {
309+ const logger = new RecordingLogger ( ) ;
310+
311+ t . is (
312+ api . getAuthorizationHeaderFor (
313+ logger ,
314+ { ...SAMPLE_DOTCOM_API_DETAILS , auth : undefined } ,
315+ `${ GITHUB_DOTCOM_URL } /github` ,
316+ ) ,
317+ undefined ,
318+ ) ;
319+ t . is (
320+ api . getAuthorizationHeaderFor (
321+ logger ,
322+ { ...SAMPLE_DOTCOM_API_DETAILS , auth : "" } ,
323+ `${ GITHUB_DOTCOM_URL } /github` ,
324+ ) ,
325+ undefined ,
326+ ) ;
327+ t . is (
328+ api . getAuthorizationHeaderFor (
329+ logger ,
330+ { ...SAMPLE_DOTCOM_API_DETAILS , auth : " " } ,
331+ `${ GITHUB_DOTCOM_URL } /github` ,
332+ ) ,
333+ undefined ,
334+ ) ;
335+ } ) ;
336+
337+ test ( "getAuthorizationHeaderFor - returns header value with token if url is allowed" , ( t ) => {
338+ const logger = new RecordingLogger ( ) ;
339+
340+ t . is (
341+ api . getAuthorizationHeaderFor (
342+ logger ,
343+ SAMPLE_DOTCOM_API_DETAILS ,
344+ `${ GITHUB_DOTCOM_URL } /github` ,
345+ ) ,
346+ `token ${ SAMPLE_DOTCOM_API_DETAILS . auth } ` ,
347+ ) ;
348+ t . is (
349+ api . getAuthorizationHeaderFor (
350+ logger ,
351+ SAMPLE_DOTCOM_API_DETAILS ,
352+ `${ GITHUB_DOTCOM_API_URL } /github` ,
353+ ) ,
354+ `token ${ SAMPLE_DOTCOM_API_DETAILS . auth } ` ,
355+ ) ;
356+ } ) ;
357+
358+ test ( "getAuthorizationHeaderFor - returns undefined if url is not allowed" , ( t ) => {
359+ const logger = new RecordingLogger ( ) ;
360+
361+ t . is (
362+ api . getAuthorizationHeaderFor (
363+ logger ,
364+ SAMPLE_DOTCOM_API_DETAILS ,
365+ `https://localhost/github` ,
366+ ) ,
367+ undefined ,
368+ ) ;
369+ t . is (
370+ api . getAuthorizationHeaderFor (
371+ logger ,
372+ SAMPLE_DOTCOM_API_DETAILS ,
373+ `https://api.localhost/github` ,
374+ ) ,
375+ undefined ,
376+ ) ;
377+ } ) ;
0 commit comments