@@ -44,7 +44,7 @@ class ManagedIdentityTests {
4444 private static ManagedIdentityApplication miApp ;
4545
4646 private String getSuccessfulResponse (String resource ) {
47- long expiresOn = Instant . now (). plus ( 1 , ChronoUnit . HOURS ). getEpochSecond ();
47+ long expiresOn = ( System . currentTimeMillis () / 1000 ) + ( 24 * 3600 ); //A long-lived, 24 hour token
4848 return "{\" access_token\" :\" accesstoken\" ,\" expires_on\" :\" " + expiresOn + "\" ,\" resource\" :\" " + resource + "\" ,\" token_type\" :" +
4949 "\" Bearer\" ,\" client_id\" :\" client_id\" }" ;
5050 }
@@ -222,6 +222,34 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
222222 verify (httpClientMock , times (1 )).send (any ());
223223 }
224224
225+ @ Test
226+ void managedIdentityTest_RefreshOnHalfOfExpiresOn () throws Exception {
227+ //All managed identity flows use the same AcquireTokenByManagedIdentitySupplier where refreshOn is set,
228+ // so any of the MI options should let us verify that it's being set correctly
229+ IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (ManagedIdentitySourceType .APP_SERVICE , appServiceEndpoint );
230+ ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
231+ ManagedIdentityClient .resetManagedIdentitySourceType ();
232+ DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
233+
234+ when (httpClientMock .send (expectedRequest (ManagedIdentitySourceType .APP_SERVICE , resource ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
235+
236+ miApp = ManagedIdentityApplication
237+ .builder (ManagedIdentityId .systemAssigned ())
238+ .httpClient (httpClientMock )
239+ .build ();
240+
241+ AuthenticationResult result = (AuthenticationResult ) miApp .acquireTokenForManagedIdentity (
242+ ManagedIdentityParameters .builder (resource )
243+ .build ()).get ();
244+
245+ long timestampSeconds = (System .currentTimeMillis () / 1000 );
246+
247+ assertNotNull (result .accessToken ());
248+ assertEquals ((result .expiresOn () - timestampSeconds )/2 , result .refreshOn () - timestampSeconds );
249+
250+ verify (httpClientMock , times (1 )).send (any ());
251+ }
252+
225253 @ ParameterizedTest
226254 @ MethodSource ("com.microsoft.aad.msal4j.ManagedIdentityTestDataProvider#createDataUserAssignedNotSupported" )
227255 void managedIdentityTest_UserAssigned_NotSupported (ManagedIdentitySourceType source , String endpoint , ManagedIdentityId id ) throws Exception {
0 commit comments