Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 5a6e630

Browse files
committed
Test that a client_credentials token with the admin scope can add a user
1 parent 86c425e commit 5a6e630

File tree

1 file changed

+79
-2
lines changed

1 file changed

+79
-2
lines changed

crates/handlers/src/graphql/tests.rs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ use oauth2_types::{
2525
};
2626
use sqlx::PgPool;
2727

28-
use crate::test_utils::{init_tracing, RequestBuilderExt, ResponseExt, TestState};
28+
use crate::{
29+
test_utils,
30+
test_utils::{init_tracing, RequestBuilderExt, ResponseExt, TestState},
31+
};
2932

3033
async fn create_test_client(state: &TestState) -> Client {
3134
let mut repo = state.repository().await.unwrap();
@@ -378,7 +381,7 @@ async fn test_oauth2_client_credentials(pool: PgPool) {
378381
let client_id = response.client_id;
379382
let client_secret = response.client_secret.expect("to have a client secret");
380383

381-
// Call the token endpoint with an empty scope
384+
// Call the token endpoint with the graphql scope
382385
let request = Request::post(mas_router::OAuth2TokenEndpoint::PATH).form(serde_json::json!({
383386
"grant_type": "client_credentials",
384387
"client_id": client_id,
@@ -424,6 +427,28 @@ async fn test_oauth2_client_credentials(pool: PgPool) {
424427
})
425428
);
426429

430+
// We shouldn't be able to call the addUser mutation
431+
let request = Request::post("/graphql")
432+
.bearer(&access_token)
433+
.json(serde_json::json!({
434+
"query": r#"
435+
mutation {
436+
addUser(input: {username: "alice"}) {
437+
user {
438+
id
439+
username
440+
}
441+
}
442+
}
443+
"#,
444+
}));
445+
let response = state.request(request).await;
446+
response.assert_status(StatusCode::OK);
447+
let response: GraphQLResponse = response.json();
448+
// There should be an error
449+
assert_eq!(response.errors.len(), 1);
450+
assert!(response.data.is_null());
451+
427452
// Check that we can't do a query once the token is revoked
428453
let request = Request::post(mas_router::OAuth2Revocation::PATH).form(serde_json::json!({
429454
"token": access_token,
@@ -453,4 +478,56 @@ async fn test_oauth2_client_credentials(pool: PgPool) {
453478

454479
let response = state.request(request).await;
455480
response.assert_status(StatusCode::UNAUTHORIZED);
481+
482+
// Now make the client admin and try again
483+
let state = {
484+
let mut state = state;
485+
state.policy_factory = test_utils::policy_factory(serde_json::json!({
486+
"admin_clients": [client_id],
487+
}))
488+
.await
489+
.unwrap();
490+
state
491+
};
492+
493+
// Ask for a token again, with the admin scope
494+
let request = Request::post(mas_router::OAuth2TokenEndpoint::PATH).form(serde_json::json!({
495+
"grant_type": "client_credentials",
496+
"client_id": client_id,
497+
"client_secret": client_secret,
498+
"scope": "urn:mas:graphql:* urn:mas:admin",
499+
}));
500+
501+
let response = state.request(request).await;
502+
response.assert_status(StatusCode::OK);
503+
let AccessTokenResponse { access_token, .. } = response.json();
504+
505+
// We should now be able to call the addUser mutation
506+
let request = Request::post("/graphql")
507+
.bearer(&access_token)
508+
.json(serde_json::json!({
509+
"query": r#"
510+
mutation {
511+
addUser(input: {username: "alice"}) {
512+
user {
513+
username
514+
}
515+
}
516+
}
517+
"#,
518+
}));
519+
let response = state.request(request).await;
520+
response.assert_status(StatusCode::OK);
521+
let response: GraphQLResponse = response.json();
522+
assert!(response.errors.is_empty());
523+
assert_eq!(
524+
response.data,
525+
serde_json::json!({
526+
"addUser": {
527+
"user": {
528+
"username": "alice"
529+
}
530+
}
531+
})
532+
);
456533
}

0 commit comments

Comments
 (0)