88using System . Threading ;
99using System . Threading . Tasks ;
1010using Azure . Core ;
11+ using Azure . Developer . MicrosoftPlaywrightTesting . TestLogger . Implementation ;
12+ using Azure . Developer . MicrosoftPlaywrightTesting . TestLogger . Interface ;
1113using Azure . Identity ;
1214using Microsoft . IdentityModel . JsonWebTokens ;
1315using Microsoft . IdentityModel . Tokens ;
@@ -43,6 +45,7 @@ public void Setup()
4345 Environment . SetEnvironmentVariable ( Constants . s_playwright_service_workspace_id_environment_variable , null ) ;
4446 Environment . SetEnvironmentVariable ( Constants . s_playwright_service_disable_scalable_execution_environment_variable , null ) ;
4547 Environment . SetEnvironmentVariable ( Constants . s_playwright_service_auth_type_environment_variable , null ) ;
48+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_one_time_operation_flag_environment_variable , null ) ;
4649 }
4750 [ TearDown ]
4851 public void TearDown ( )
@@ -57,6 +60,7 @@ public void TearDown()
5760 Environment . SetEnvironmentVariable ( Constants . s_playwright_service_workspace_id_environment_variable , null ) ;
5861 Environment . SetEnvironmentVariable ( Constants . s_playwright_service_disable_scalable_execution_environment_variable , null ) ;
5962 Environment . SetEnvironmentVariable ( Constants . s_playwright_service_auth_type_environment_variable , null ) ;
63+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_one_time_operation_flag_environment_variable , null ) ;
6064 }
6165
6266 [ Test ]
@@ -814,4 +818,103 @@ public void SetReportingUrlAndWorkspaceId_WhenReportingServiceEndpointAndWorkspa
814818 Assert . That ( Environment . GetEnvironmentVariable ( Constants . s_playwright_service_workspace_id_environment_variable ) , Is . EqualTo ( "sample-id" ) ) ;
815819 } ) ;
816820 }
821+
822+ [ Test ]
823+ public void ShouldNotCallWarnIfAccessTokenCloseToExpiry_WhenOneTimeOperationFlagIsSet_True ( )
824+ {
825+ Environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceAccessToken , "access_token" ) ;
826+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_reporting_url_environment_variable , "https://playwright.microsoft.com" ) ;
827+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_one_time_operation_flag_environment_variable , "true" ) ;
828+ var defaultAzureCredentialMock = new Mock < DefaultAzureCredential > ( ) ;
829+ defaultAzureCredentialMock
830+ . Setup ( x => x . GetTokenAsync ( It . IsAny < TokenRequestContext > ( ) , It . IsAny < CancellationToken > ( ) ) )
831+ . ThrowsAsync ( new Exception ( ) ) ;
832+ var entraLifecycleMock = new Mock < EntraLifecycle > ( defaultAzureCredentialMock . Object , new JsonWebTokenHandler ( ) , null ) ;
833+ var service = new Mock < PlaywrightService > ( new PlaywrightServiceOptions ( ) , null , null ) ;
834+ service . Object . PerformOneTimeOperation ( ) ;
835+ service . Verify ( x => x . WarnIfAccessTokenCloseToExpiry ( ) , Times . Never ) ;
836+ }
837+
838+ [ Test ]
839+ public void ShouldCallWarnIfAccessTokenCloseToExpiry_WhenOneTimeOperationFlagIsSet_True ( )
840+ {
841+ Environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceAccessToken , "access_token" ) ;
842+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_reporting_url_environment_variable , "https://playwright.microsoft.com" ) ;
843+ var defaultAzureCredentialMock = new Mock < DefaultAzureCredential > ( ) ;
844+ defaultAzureCredentialMock
845+ . Setup ( x => x . GetTokenAsync ( It . IsAny < TokenRequestContext > ( ) , It . IsAny < CancellationToken > ( ) ) )
846+ . ThrowsAsync ( new Exception ( ) ) ;
847+ var entraLifecycleMock = new Mock < EntraLifecycle > ( defaultAzureCredentialMock . Object , new JsonWebTokenHandler ( ) , null ) ;
848+ var serviceMock = new Mock < PlaywrightService > ( new PlaywrightServiceOptions ( serviceAuth : ServiceAuthType . AccessToken ) , null , null ) ;
849+ serviceMock . Object . PerformOneTimeOperation ( ) ;
850+ serviceMock . Verify ( x => x . WarnIfAccessTokenCloseToExpiry ( ) , Times . Once ) ;
851+ }
852+
853+ [ Test ]
854+ public void ShouldReturnTrue_IfAccessTokenIs_CloseToExpiry ( )
855+ {
856+ Environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceAccessToken , "access_token" ) ;
857+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_reporting_url_environment_variable , "https://playwright.microsoft.com" ) ;
858+ var defaultAzureCredentialMock = new Mock < DefaultAzureCredential > ( ) ;
859+ defaultAzureCredentialMock
860+ . Setup ( x => x . GetTokenAsync ( It . IsAny < TokenRequestContext > ( ) , It . IsAny < CancellationToken > ( ) ) )
861+ . ThrowsAsync ( new Exception ( ) ) ;
862+ var entraLifecycleMock = new Mock < EntraLifecycle > ( defaultAzureCredentialMock . Object , new JsonWebTokenHandler ( ) , null ) ;
863+ var serviceMock = new Mock < PlaywrightService > ( new PlaywrightServiceOptions ( ) , null , null ) ;
864+ long currentTime = DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( ) ;
865+ long expirationTime = DateTimeOffset . UtcNow . AddDays ( 4 ) . ToUnixTimeMilliseconds ( ) ;
866+ bool isExpiringSoon = PlaywrightService . IsTokenExpiringSoon ( expirationTime , currentTime ) ;
867+ Assert . IsTrue ( isExpiringSoon ) ;
868+ }
869+
870+ [ Test ]
871+ public void ShouldReturnFalse_IfAccessTokenIs_NotCloseToExpiry ( )
872+ {
873+ Environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceAccessToken , "access_token" ) ;
874+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_reporting_url_environment_variable , "https://playwright.microsoft.com" ) ;
875+ var defaultAzureCredentialMock = new Mock < DefaultAzureCredential > ( ) ;
876+ defaultAzureCredentialMock
877+ . Setup ( x => x . GetTokenAsync ( It . IsAny < TokenRequestContext > ( ) , It . IsAny < CancellationToken > ( ) ) )
878+ . ThrowsAsync ( new Exception ( ) ) ;
879+ var entraLifecycleMock = new Mock < EntraLifecycle > ( defaultAzureCredentialMock . Object , new JsonWebTokenHandler ( ) , null ) ;
880+ var serviceMock = new Mock < PlaywrightService > ( new PlaywrightServiceOptions ( ) , null , null ) ;
881+ long currentTime = DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( ) ;
882+ long expirationTime = DateTimeOffset . UtcNow . AddDays ( 20 ) . ToUnixTimeMilliseconds ( ) ;
883+ bool isExpiringSoon = PlaywrightService . IsTokenExpiringSoon ( expirationTime , currentTime ) ;
884+ Assert . IsFalse ( isExpiringSoon ) ;
885+ }
886+
887+ [ Test ]
888+ public void ShouldLogWarning_IfWarnAboutTokenExpiry_IsCalled ( )
889+ {
890+ Environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceAccessToken , "access_token" ) ;
891+ Environment . SetEnvironmentVariable ( Constants . s_playwright_service_reporting_url_environment_variable , "https://playwright.microsoft.com" ) ;
892+ var defaultAzureCredentialMock = new Mock < DefaultAzureCredential > ( ) ;
893+ defaultAzureCredentialMock
894+ . Setup ( x => x . GetTokenAsync ( It . IsAny < TokenRequestContext > ( ) , It . IsAny < CancellationToken > ( ) ) )
895+ . ThrowsAsync ( new Exception ( ) ) ;
896+
897+ var entraLifecycleMock = new Mock < EntraLifecycle > ( defaultAzureCredentialMock . Object , new JsonWebTokenHandler ( ) , null ) ;
898+ var consoleWriterMock = new Mock < IConsoleWriter > ( ) ;
899+ var serviceMock = new Mock < PlaywrightService > (
900+ null ,
901+ null ,
902+ null ,
903+ null ,
904+ null ,
905+ null ,
906+ null ,
907+ null ,
908+ null ,
909+ consoleWriterMock . Object
910+ ) ;
911+ serviceMock . CallBase = true ;
912+ long currentTime = DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( ) ;
913+ long expirationTime = DateTimeOffset . UtcNow . AddDays ( 4 ) . ToUnixTimeMilliseconds ( ) ;
914+ int daysToExpiration = ( int ) Math . Ceiling ( ( expirationTime - currentTime ) / ( double ) Constants . s_oneDayInMs ) ;
915+ string expirationDate = DateTimeOffset . FromUnixTimeMilliseconds ( expirationTime ) . UtcDateTime . ToString ( "d" ) ;
916+ string expectedWarning = string . Format ( Constants . s_token_expiry_warning_template , daysToExpiration , expirationDate ) ;
917+ serviceMock . Object . WarnAboutTokenExpiry ( expirationTime , currentTime ) ;
918+ consoleWriterMock . Verify ( c => c . WriteLine ( expectedWarning ) , Times . Once ) ;
919+ }
817920}
0 commit comments