Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions src/graphql/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ impl PolicyCheck {
"Checking authorization against {:?} using {:?} for admin and {:?} for access",
endpoint.policy_host, endpoint.admin_query, endpoint.access_query
);

let host = endpoint.policy_host.trim_end_matches('/');

Self {
client: reqwest::Client::new(),
admin: format!("{}/{}", endpoint.policy_host, endpoint.admin_query),
access: format!("{}/{}", endpoint.policy_host, endpoint.access_query),
admin: format!("{}/{}", host, endpoint.admin_query.trim_start_matches('/')),
access: format!("{}/{}", host, endpoint.access_query.trim_start_matches('/')),
}
}
pub async fn check_access(
Expand Down Expand Up @@ -271,6 +274,68 @@ mod tests {
mock.assert();
}

#[tokio::test]
async fn successful_check_access_with_trailing_slash_on_host() {
let server = MockServer::start();
let mock = server
.mock_async(|when, then| {
when.method("POST")
.path("/demo/access")
.json_body_obj(&json!({
"input": {
"token": "token",
"beamline": "i22",
"visit": 4,
"proposal": 1234,
"audience": "account"
}
}));
then.status(200).json_body_obj(&json!({"result": true}));
})
.await;
let check = PolicyCheck::new(PolicyOptions {
policy_host: server.url("/"),
access_query: "demo/access".into(),
admin_query: "demo/admin".into(),
});
check
.check_access(token("token").as_ref(), "i22", "cm1234-4")
.await
.unwrap();
mock.assert();
}

#[tokio::test]
async fn successful_check_access_with_leading_slashes() {
let server = MockServer::start();
let mock = server
.mock_async(|when, then| {
when.method("POST")
.path("/demo/access")
.json_body_obj(&json!({
"input": {
"token": "token",
"beamline": "i22",
"visit": 4,
"proposal": 1234,
"audience": "account"
}
}));
then.status(200).json_body_obj(&json!({"result": true}));
})
.await;
let check = PolicyCheck::new(PolicyOptions {
policy_host: server.url(""),
access_query: "/demo/access".into(),
admin_query: "/demo/admin".into(),
});
check
.check_access(token("token").as_ref(), "i22", "cm1234-4")
.await
.unwrap();
mock.assert();
}

#[tokio::test]
async fn successful_check_instrument_admin() {
let server = MockServer::start();
Expand Down
Loading