@@ -55,12 +55,24 @@ async fn test_qcs_against_mocks() {
5555
5656async fn setup ( ) {
5757 simple_logger:: init_with_env ( ) . unwrap ( ) ;
58- std:: env:: set_var ( SETTINGS_PATH_VAR , "tests/settings.toml" ) ;
59- std:: env:: set_var ( SECRETS_PATH_VAR , "tests/secrets.toml" ) ;
58+
59+ // Create a temporary file to store the secrets generated by the test server,
60+ // which must be a non-expired JWT in order for the client to even attempt to make the request.
61+ let secrets_toml = tempfile:: NamedTempFile :: new ( ) . unwrap ( ) ;
62+
63+ unsafe {
64+ std:: env:: set_var ( SETTINGS_PATH_VAR , "tests/settings.toml" ) ;
65+ std:: env:: set_var ( SECRETS_PATH_VAR , secrets_toml. path ( ) ) ;
66+ }
67+
68+ let ( oauth_ready_tx, oauth_ready_rx) = tokio:: sync:: oneshot:: channel :: < ( ) > ( ) ;
69+
6070 tokio:: spawn ( qpu:: run ( ) ) ;
6171 tokio:: spawn ( translation:: run ( ) ) ;
62- tokio:: spawn ( auth_server :: run ( ) ) ;
72+ tokio:: spawn ( mock_oauth2 :: run ( secrets_toml , oauth_ready_tx ) ) ;
6373 tokio:: spawn ( mock_qcs:: run ( ) ) ;
74+
75+ oauth_ready_rx. await . unwrap ( ) ;
6476}
6577
6678async fn quilc_client ( ) -> rpcq:: Client {
@@ -94,35 +106,60 @@ async fn run_bell_state(connection_strategy: ConnectionStrategy) {
94106 assert_eq ! ( result. duration, Some ( Duration :: from_micros( 8675 ) ) ) ;
95107}
96108
97- #[ allow( dead_code) ]
98- mod auth_server {
99- use serde:: { Deserialize , Serialize } ;
100- use warp:: Filter ;
101-
102- #[ derive( Debug , Deserialize ) ]
103- struct TokenRequest {
104- grant_type : String ,
105- client_id : String ,
106- refresh_token : String ,
107- }
108-
109- #[ derive( Serialize , Debug ) ]
110- struct TokenResponse {
111- refresh_token : & ' static str ,
112- access_token : & ' static str ,
113- }
114-
115- pub ( crate ) async fn run ( ) {
116- let token = warp:: post ( )
117- . and ( warp:: path ( "v1" ) . and ( warp:: path ( "token" ) ) )
118- . and ( warp:: body:: form ( ) )
119- . map ( |_request : TokenRequest | {
120- warp:: reply:: json ( & TokenResponse {
121- refresh_token : "refreshed" ,
122- access_token : "accessed" ,
123- } )
124- } ) ;
125- warp:: serve ( token) . run ( ( [ 127 , 0 , 0 , 1 ] , 8001 ) ) . await ;
109+ mod mock_oauth2 {
110+ use std:: io:: Write as _;
111+
112+ use oauth2_test_server:: { IssuerConfig , JwtOptions , OAuthTestServer } ;
113+ use tokio:: task:: JoinError ;
114+
115+ /// A test harness for serving a valid oauth2 issuer, including the well-known endpoint.
116+ pub ( super ) async fn run (
117+ secrets_toml : tempfile:: NamedTempFile ,
118+ oauth_ready_tx : tokio:: sync:: oneshot:: Sender < ( ) > ,
119+ ) -> Result < ( ) , JoinError > {
120+ const SCHEME : & str = "http" ;
121+ const HOST : & str = "127.0.0.1" ;
122+ const PORT : u16 = 8001 ;
123+
124+ let server = OAuthTestServer :: start_with_config ( IssuerConfig {
125+ scheme : SCHEME . to_string ( ) ,
126+ host : HOST . to_string ( ) ,
127+ port : PORT ,
128+ ..Default :: default ( )
129+ } )
130+ . await ;
131+
132+ let client = server. register_client ( serde_json:: json!( {
133+ "scope" : "openid" ,
134+ "redirect_uris" : [ format!( "{SCHEME}://{HOST}:{PORT}" ) ] ,
135+ "client_name" : "mock_oauth2"
136+ } ) ) ;
137+
138+ // Generate a valid access token and persist it to the credentials,
139+ // otherwise the client won't make a request with an invalid access token.
140+ let token = server. generate_token ( & client, JwtOptions :: default ( ) ) ;
141+ let access_token = token. access_token ;
142+
143+ let contents = format ! (
144+ r#"
145+ [credentials]
146+ [credential.default]
147+ [credentials.default.token_payload]
148+ access_token = "{access_token}"
149+ "#
150+ ) ;
151+ secrets_toml
152+ . as_file ( )
153+ . write_all ( contents. as_bytes ( ) )
154+ . unwrap ( ) ;
155+
156+ oauth_ready_tx. send ( ( ) ) . unwrap ( ) ;
157+
158+ server. wait_for_shutdown ( ) . await ?;
159+
160+ secrets_toml. close ( ) . unwrap ( ) ;
161+
162+ Ok ( ( ) )
126163 }
127164}
128165
0 commit comments