@@ -263,6 +263,13 @@ fn get_query_param(url: &Url, name: &str) -> String {
263263 . to_string ( )
264264}
265265
266+ async fn settle_background_refreshes ( ) {
267+ for _ in 0 ..10 {
268+ tokio:: task:: yield_now ( ) . await ;
269+ tokio:: time:: sleep ( Duration :: from_millis ( 10 ) ) . await ;
270+ }
271+ }
272+
266273const REDIRECT_COUNT_COOKIE : & str = "sqlpage_oidc_redirect_count" ;
267274const MAX_OIDC_REDIRECTS : u8 = 3 ;
268275
@@ -741,9 +748,21 @@ async fn test_slow_discovery_does_not_block_authenticated_requests() {
741748 request_with_cookies ! ( app, test:: TestRequest :: get( ) . uri( & callback_uri) , cookies) ;
742749 assert_eq ! ( callback_resp. status( ) , StatusCode :: SEE_OTHER ) ;
743750
751+ settle_background_refreshes ( ) . await ;
752+ let count_before = provider. discovery_count ( ) ;
753+
754+ let resp = request_with_cookies ! ( app, test:: TestRequest :: get( ) . uri( "/" ) , cookies) ;
755+ assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
756+ settle_background_refreshes ( ) . await ;
757+ assert_eq ! (
758+ provider. discovery_count( ) ,
759+ count_before,
760+ "a fresh OIDC snapshot must not trigger a refresh, \
761+ otherwise every request hammers the identity provider"
762+ ) ;
763+
744764 // Advance time so the OIDC snapshot appears stale.
745765 // The next request triggers a background refresh.
746- let count_before = provider. discovery_count ( ) ;
747766 tokio:: time:: pause ( ) ;
748767 tokio:: time:: advance ( Duration :: from_secs ( 3601 ) ) . await ;
749768 // Resume real time so the DB pool and background refresh work normally.
@@ -755,7 +774,7 @@ async fn test_slow_discovery_does_not_block_authenticated_requests() {
755774 assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
756775
757776 // Let the background refresh task complete.
758- tokio :: task :: yield_now ( ) . await ;
777+ settle_background_refreshes ( ) . await ;
759778 assert ! (
760779 provider. discovery_count( ) > count_before,
761780 "OIDC provider metadata was not refreshed"
0 commit comments