11import { PublicClientApplication } from './../../src/client/PublicClientApplication' ;
2- import { Configuration , InteractiveRequest } from './../../src/index' ;
3- import { ID_TOKEN_CLAIMS , mockAuthenticationResult , TEST_CONSTANTS } from '../utils/TestConstants' ;
2+ import { Configuration , ILoopbackClient , InteractiveRequest } from './../../src/index' ;
3+ import { ID_TOKEN_CLAIMS , mockAuthenticationResult , TEST_CONSTANTS , TEST_DATA_CLIENT_INFO } from '../utils/TestConstants' ;
44import {
55 ClientConfiguration , AuthenticationResult , AuthorizationCodeClient , RefreshTokenClient , UsernamePasswordClient ,
6- SilentFlowClient , ProtocolMode , Logger , LogLevel , ClientAuthError , AccountInfo
6+ SilentFlowClient , ProtocolMode , Logger , LogLevel , ClientAuthError , AccountInfo , ServerAuthorizationCodeResponse
77} from '@azure/msal-common' ;
88import { CryptoProvider } from '../../src/crypto/CryptoProvider' ;
99import { DeviceCodeRequest } from '../../src/request/DeviceCodeRequest' ;
@@ -197,7 +197,7 @@ describe('PublicClientApplication', () => {
197197 ) ;
198198 } ) ;
199199
200- test ( 'acquireTokenSilent' , async ( ) => {
200+ test ( 'acquireTokenSilent' , async ( ) => {
201201 const account : AccountInfo = {
202202 homeAccountId : "" ,
203203 environment : "" ,
@@ -230,7 +230,7 @@ describe('PublicClientApplication', () => {
230230 const authApp = new PublicClientApplication ( appConfig ) ;
231231
232232 let redirectUri : string ;
233-
233+
234234 const openBrowser = ( url : string ) => {
235235 expect ( url . startsWith ( "https://login.microsoftonline.com" ) ) . toBe ( true ) ;
236236 http . get ( `${ redirectUri } ?code=${ TEST_CONSTANTS . AUTHORIZATION_CODE } ` ) ;
@@ -260,6 +260,62 @@ describe('PublicClientApplication', () => {
260260 expect ( response . account ) . toEqual ( mockAuthenticationResult . account ) ;
261261 } ) ;
262262
263+ test ( "acquireTokenInteractive - with custom loopback client" , async ( ) => {
264+ const authApp = new PublicClientApplication ( appConfig ) ;
265+
266+ const openBrowser = ( url : string ) => {
267+ expect ( url . startsWith ( "https://login.microsoftonline.com" ) ) . toBe ( true ) ;
268+ return Promise . resolve ( ) ;
269+ } ;
270+
271+ const testServerCodeResponse : ServerAuthorizationCodeResponse = {
272+ code : TEST_CONSTANTS . AUTHORIZATION_CODE ,
273+ client_info : TEST_DATA_CLIENT_INFO . TEST_DECODED_CLIENT_INFO ,
274+ state : "123"
275+ } ;
276+
277+ const mockListenForAuthCode = jest . fn ( ( ) => {
278+ return new Promise < ServerAuthorizationCodeResponse > ( ( resolve ) => {
279+ resolve ( testServerCodeResponse ) ;
280+ } ) ;
281+ } ) ;
282+ const mockGetRedirectUri = jest . fn ( ( ) => TEST_CONSTANTS . REDIRECT_URI ) ;
283+ const mockCloseServer = jest . fn ( ( ) => { } ) ;
284+
285+ const customLoopbackClient : ILoopbackClient = {
286+ listenForAuthCode : mockListenForAuthCode ,
287+ getRedirectUri : mockGetRedirectUri ,
288+ closeServer : mockCloseServer
289+ } ;
290+
291+ const request : InteractiveRequest = {
292+ scopes : TEST_CONSTANTS . DEFAULT_GRAPH_SCOPE ,
293+ openBrowser : openBrowser ,
294+ loopbackClient : customLoopbackClient ,
295+ } ;
296+
297+ const MockAuthorizationCodeClient = getMsalCommonAutoMock ( ) . AuthorizationCodeClient ;
298+ jest . spyOn ( msalCommon , 'AuthorizationCodeClient' ) . mockImplementation ( ( config ) => new MockAuthorizationCodeClient ( config ) ) ;
299+
300+ jest . spyOn ( MockAuthorizationCodeClient . prototype , "getAuthCodeUrl" ) . mockImplementation ( ( req ) => {
301+ expect ( req . redirectUri ) . toEqual ( TEST_CONSTANTS . REDIRECT_URI ) ;
302+ return Promise . resolve ( TEST_CONSTANTS . AUTH_CODE_URL ) ;
303+ } ) ;
304+
305+ jest . spyOn ( MockAuthorizationCodeClient . prototype , "acquireToken" ) . mockImplementation ( ( tokenRequest ) => {
306+ expect ( tokenRequest . scopes ) . toEqual ( [ ...TEST_CONSTANTS . DEFAULT_GRAPH_SCOPE , ...TEST_CONSTANTS . DEFAULT_OIDC_SCOPES ] ) ;
307+ return Promise . resolve ( mockAuthenticationResult ) ;
308+ } ) ;
309+
310+ const response = await authApp . acquireTokenInteractive ( request ) ;
311+ expect ( response . idToken ) . toEqual ( mockAuthenticationResult . idToken ) ;
312+ expect ( response . accessToken ) . toEqual ( mockAuthenticationResult . accessToken ) ;
313+ expect ( response . account ) . toEqual ( mockAuthenticationResult . account ) ;
314+ expect ( mockListenForAuthCode ) . toHaveBeenCalledTimes ( 1 ) ;
315+ expect ( mockGetRedirectUri ) . toHaveBeenCalledTimes ( 1 ) ;
316+ expect ( mockCloseServer ) . toHaveBeenCalledTimes ( 1 ) ;
317+ } ) ;
318+
263319 test ( 'initializeBaseRequest passes a claims hash to acquireToken' , async ( ) => {
264320 const account : AccountInfo = {
265321 homeAccountId : "" ,
@@ -345,7 +401,7 @@ describe('PublicClientApplication', () => {
345401 const authorityMock = setupAuthorityFactory_createDiscoveredInstance_mock ( fakeAuthority ) ;
346402
347403 const authApp = new PublicClientApplication ( config ) ;
348- await authApp . acquireTokenByRefreshToken ( request ) ;
404+ await authApp . acquireTokenByRefreshToken ( request ) ;
349405 expect ( authorityMock . mock . calls [ 0 ] [ 0 ] ) . toBe ( TEST_CONSTANTS . DEFAULT_AUTHORITY ) ;
350406 expect ( authorityMock . mock . calls [ 0 ] [ 1 ] ) . toBeInstanceOf ( HttpClient ) ;
351407 expect ( authorityMock . mock . calls [ 0 ] [ 2 ] ) . toBeInstanceOf ( NodeStorage ) ;
@@ -490,7 +546,7 @@ describe('PublicClientApplication', () => {
490546
491547 expect ( authApp . getLogger ( ) ) . toBeDefined ( ) ;
492548 expect ( authApp . getLogger ( ) . info ( "Test logger" ) ) . toEqual ( undefined ) ;
493-
549+
494550 } ) ;
495551
496552 test ( "should throw an error if state is not provided" , async ( ) => {
@@ -530,7 +586,7 @@ describe('PublicClientApplication', () => {
530586
531587 try {
532588 await authApp . acquireTokenByCode ( request , authCodePayLoad ) ;
533- } catch ( e ) {
589+ } catch ( e ) {
534590 expect ( mockInfo ) . toBeCalledWith ( "acquireTokenByCode called" ) ;
535591 expect ( mockInfo ) . toHaveBeenCalledWith (
536592 "acquireTokenByCode - validating state"
@@ -579,4 +635,4 @@ describe('PublicClientApplication', () => {
579635 . rejects . toMatchObject ( ClientAuthError . createStateMismatchError ( ) ) ;
580636 } ) ;
581637
582- } ) ;
638+ } ) ;
0 commit comments