@@ -46,6 +46,7 @@ use mas_router::{SimpleRoute, UrlBuilder};
46
46
use mas_storage:: { clock:: MockClock , BoxClock , BoxRepository , BoxRng } ;
47
47
use mas_storage_pg:: { DatabaseError , PgRepository } ;
48
48
use mas_templates:: { SiteConfigExt , Templates } ;
49
+ use oauth2_types:: { registration:: ClientRegistrationResponse , requests:: AccessTokenResponse } ;
49
50
use rand:: SeedableRng ;
50
51
use rand_chacha:: ChaChaRng ;
51
52
use serde:: { de:: DeserializeOwned , Serialize } ;
@@ -249,6 +250,7 @@ impl TestState {
249
250
. merge ( crate :: compat_router ( ) )
250
251
. merge ( crate :: human_router ( self . templates . clone ( ) ) )
251
252
. merge ( crate :: graphql_router ( false ) )
253
+ . merge ( crate :: admin_api_router ( ) . 1 )
252
254
. with_state ( self . clone ( ) )
253
255
. into_service ( ) ;
254
256
@@ -274,6 +276,49 @@ impl TestState {
274
276
Response :: from_parts ( parts, body)
275
277
}
276
278
279
+ /// Get a token with the given scope
280
+ pub async fn token_with_scope ( & mut self , scope : & str ) -> String {
281
+ // Provision a client
282
+ let request =
283
+ Request :: post ( mas_router:: OAuth2RegistrationEndpoint :: PATH ) . json ( serde_json:: json!( {
284
+ "client_uri" : "https://example.com/" ,
285
+ "contacts" : [ "[email protected] " ] ,
286
+ "token_endpoint_auth_method" : "client_secret_post" ,
287
+ "grant_types" : [ "client_credentials" ] ,
288
+ } ) ) ;
289
+ let response = self . request ( request) . await ;
290
+ response. assert_status ( StatusCode :: CREATED ) ;
291
+ let response: ClientRegistrationResponse = response. json ( ) ;
292
+ let client_id = response. client_id ;
293
+ let client_secret = response. client_secret . expect ( "to have a client secret" ) ;
294
+
295
+ // Make the client admin
296
+ let state = {
297
+ let mut state = self . clone ( ) ;
298
+ state. policy_factory = policy_factory ( serde_json:: json!( {
299
+ "admin_clients" : [ client_id] ,
300
+ } ) )
301
+ . await
302
+ . unwrap ( ) ;
303
+ state
304
+ } ;
305
+
306
+ // Ask for a token with the admin scope
307
+ let request =
308
+ Request :: post ( mas_router:: OAuth2TokenEndpoint :: PATH ) . form ( serde_json:: json!( {
309
+ "grant_type" : "client_credentials" ,
310
+ "client_id" : client_id,
311
+ "client_secret" : client_secret,
312
+ "scope" : scope,
313
+ } ) ) ;
314
+
315
+ let response = state. request ( request) . await ;
316
+ response. assert_status ( StatusCode :: OK ) ;
317
+ let AccessTokenResponse { access_token, .. } = response. json ( ) ;
318
+
319
+ access_token
320
+ }
321
+
277
322
pub async fn repository ( & self ) -> Result < BoxRepository , DatabaseError > {
278
323
let repo = PgRepository :: from_pool ( & self . pool ) . await ?;
279
324
Ok ( repo. boxed ( ) )
0 commit comments