@@ -117,6 +117,7 @@ import {
117
117
Configuration ,
118
118
} from "../../src/config/Configuration.js" ;
119
119
import { buildAccountFromIdTokenClaims , buildIdToken } from "msal-test-utils" ;
120
+ import { nativeConnectionNotEstablished } from "../../src/error/BrowserAuthErrorCodes.js" ;
120
121
121
122
const cacheConfig = {
122
123
temporaryCacheLocation : BrowserCacheLocation . SessionStorage ,
@@ -5773,10 +5774,52 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
5773
5774
expect ( silentIframeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5774
5775
} ) ;
5775
5776
5776
- it ( "Calls SilentCacheClient.acquireToken and SilentRefreshClient.acquireToken , and does not call SilentIframeClient .acquireToken if cache lookup throws and refresh token is expired when CacheLookupPolicy is set to AccessTokenAndRefreshToken " , async ( ) => {
5777
+ it ( "Calls SilentCacheClient.acquireToken, and calls NativeInteractionClient .acquireToken when CacheLookupPolicy is set to AccessToken " , async ( ) => {
5777
5778
const silentCacheSpy : jest . SpyInstance = jest
5778
5779
. spyOn ( SilentCacheClient . prototype , "acquireToken" )
5779
5780
. mockRejectedValue ( refreshRequiredCacheError ) ;
5781
+ const silentRefreshSpy = jest
5782
+ . spyOn ( SilentRefreshClient . prototype , "acquireToken" )
5783
+ . mockImplementation ( ) ;
5784
+ const silentIframeSpy = jest
5785
+ . spyOn ( SilentIframeClient . prototype , "acquireToken" )
5786
+ . mockImplementation ( ) ;
5787
+
5788
+ const isPlatformBrokerAvailableSpy = jest
5789
+ . spyOn ( NativeMessageHandler , "isPlatformBrokerAvailable" )
5790
+ . mockReturnValue ( true ) ;
5791
+ const nativeAcquireTokenSpy : jest . SpyInstance = jest
5792
+ . spyOn ( NativeInteractionClient . prototype , "acquireToken" )
5793
+ . mockImplementation ( ) ;
5794
+ const cacheAccount = testAccount ;
5795
+ cacheAccount . nativeAccountId = "nativeAccountId" ;
5796
+
5797
+ await expect (
5798
+ pca . acquireTokenSilent ( {
5799
+ scopes : [ "openid" ] ,
5800
+ account : cacheAccount ,
5801
+ cacheLookupPolicy : CacheLookupPolicy . AccessToken ,
5802
+ } )
5803
+ )
5804
+ . rejects . toThrow ( BrowserAuthError )
5805
+ . catch ( ( error ) => {
5806
+ expect ( error . errorCode ) . toBe (
5807
+ BrowserAuthErrorCodes . nativeConnectionNotEstablished
5808
+ ) ;
5809
+ } ) ;
5810
+ expect ( silentCacheSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5811
+ expect ( silentRefreshSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5812
+ expect ( silentIframeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5813
+ expect ( nativeAcquireTokenSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5814
+
5815
+ nativeAcquireTokenSpy . mockRestore ( ) ;
5816
+ isPlatformBrokerAvailableSpy . mockRestore ( ) ;
5817
+ } ) ;
5818
+
5819
+ it ( "Calls SilentRefreshClient.acquireToken, and does not call SilentCacheClient.acquireToken or SilentIframeClient.acquireToken if refresh token is expired when CacheLookupPolicy is set to RefreshToken" , async ( ) => {
5820
+ const silentCacheSpy = jest
5821
+ . spyOn ( SilentCacheClient . prototype , "acquireToken" )
5822
+ . mockImplementation ( ) ;
5780
5823
const silentRefreshSpy : jest . SpyInstance = jest
5781
5824
. spyOn ( SilentRefreshClient . prototype , "acquireToken" )
5782
5825
. mockRejectedValue ( refreshRequiredServerError ) ;
@@ -5788,15 +5831,57 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
5788
5831
pca . acquireTokenSilent ( {
5789
5832
scopes : [ "openid" ] ,
5790
5833
account : testAccount ,
5791
- cacheLookupPolicy :
5792
- CacheLookupPolicy . AccessTokenAndRefreshToken ,
5834
+ cacheLookupPolicy : CacheLookupPolicy . RefreshToken ,
5793
5835
} )
5794
5836
) . rejects . toThrow ( refreshRequiredServerError ) ;
5795
- expect ( silentCacheSpy ) . toHaveBeenCalledTimes ( 1 ) ;
5837
+ expect ( silentCacheSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5796
5838
expect ( silentRefreshSpy ) . toHaveBeenCalledTimes ( 1 ) ;
5797
5839
expect ( silentIframeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5798
5840
} ) ;
5799
5841
5842
+ it ( "Calls NativeInteractionClient.acquireToken when CacheLookupPolicy is set to AccessTokenAndRefreshToken" , async ( ) => {
5843
+ const silentCacheSpy : jest . SpyInstance = jest
5844
+ . spyOn ( SilentCacheClient . prototype , "acquireToken" )
5845
+ . mockRejectedValue ( refreshRequiredCacheError ) ;
5846
+ const silentRefreshSpy : jest . SpyInstance = jest
5847
+ . spyOn ( SilentRefreshClient . prototype , "acquireToken" )
5848
+ . mockRejectedValue ( refreshRequiredServerError ) ;
5849
+ const silentIframeSpy = jest
5850
+ . spyOn ( SilentIframeClient . prototype , "acquireToken" )
5851
+ . mockImplementation ( ) ;
5852
+ const nativeAcquireTokenSpy : jest . SpyInstance = jest
5853
+ . spyOn ( NativeInteractionClient . prototype , "acquireToken" )
5854
+ . mockImplementation ( ) ;
5855
+
5856
+ const cacheAccount = testAccount ;
5857
+ cacheAccount . nativeAccountId = "nativeAccountId" ;
5858
+ const isPlatformBrokerAvailableSpy = jest
5859
+ . spyOn ( NativeMessageHandler , "isPlatformBrokerAvailable" )
5860
+ . mockReturnValue ( true ) ;
5861
+ testAccount . nativeAccountId = "nativeAccountId" ;
5862
+
5863
+ await expect (
5864
+ pca . acquireTokenSilent ( {
5865
+ scopes : [ "openid" ] ,
5866
+ account : cacheAccount ,
5867
+ cacheLookupPolicy :
5868
+ CacheLookupPolicy . AccessTokenAndRefreshToken ,
5869
+ } )
5870
+ )
5871
+ . rejects . toThrow ( BrowserAuthError )
5872
+ . catch ( ( error ) => {
5873
+ expect ( error . errorCode ) . toBe (
5874
+ BrowserAuthErrorCodes . nativeConnectionNotEstablished
5875
+ ) ;
5876
+ } ) ;
5877
+ expect ( silentCacheSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5878
+ expect ( silentRefreshSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5879
+ expect ( silentIframeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5880
+ expect ( nativeAcquireTokenSpy ) . toHaveBeenCalledTimes ( 0 ) ;
5881
+ nativeAcquireTokenSpy . mockRestore ( ) ;
5882
+ isPlatformBrokerAvailableSpy . mockRestore ( ) ;
5883
+ } ) ;
5884
+
5800
5885
it ( "Calls SilentRefreshClient.acquireToken, and does not call SilentCacheClient.acquireToken or SilentIframeClient.acquireToken if refresh token is expired when CacheLookupPolicy is set to RefreshToken" , async ( ) => {
5801
5886
const silentCacheSpy = jest
5802
5887
. spyOn ( SilentCacheClient . prototype , "acquireToken" )
0 commit comments