Skip to content

Commit b5c2425

Browse files
committed
Wrap auth request input as field in JSON query
The input should be sent to the OPA instance as the 'input' field of the query data instead of as the data itself. The tests have also been updated to reflect this and now use the expected json when mocking the auth server instead of relying on the same request/response types as the implementation so that inconsistencies are easier to catch in future.
1 parent 172cd01 commit b5c2425

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

src/graphql/auth.rs

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ const AUDIENCE: &str = "account";
2626

2727
type Token = Authorization<Bearer>;
2828

29+
#[derive(Debug, Serialize)]
30+
#[cfg_attr(test, derive(Deserialize))]
31+
struct Request<T> {
32+
input: T,
33+
}
34+
2935
#[derive(Debug, Deserialize)]
3036
#[cfg_attr(test, derive(Serialize))]
3137
struct Response {
@@ -136,7 +142,12 @@ impl PolicyCheck {
136142
}
137143

138144
async fn authorise(&self, query: &str, input: impl Serialize) -> Result<(), AuthError> {
139-
let response = self.client.post(query).json(&input).send().await?;
145+
let response = self
146+
.client
147+
.post(query)
148+
.json(&Request { input })
149+
.send()
150+
.await?;
140151
if response.json::<Response>().await?.result {
141152
Ok(())
142153
} else {
@@ -187,11 +198,9 @@ mod tests {
187198
use axum_extra::headers::Authorization;
188199
use httpmock::MockServer;
189200
use rstest::rstest;
201+
use serde_json::json;
190202

191-
use super::{
192-
AccessRequest, AdminRequest, AuthError, InvalidVisit, PolicyCheck, Response, Visit,
193-
AUDIENCE,
194-
};
203+
use super::{AuthError, InvalidVisit, PolicyCheck, Visit};
195204
use crate::cli::PolicyOptions;
196205

197206
fn token(name: &'static str) -> Option<Authorization<Bearer>> {
@@ -224,14 +233,16 @@ mod tests {
224233
.mock_async(|when, then| {
225234
when.method("POST")
226235
.path("/demo/access")
227-
.json_body_obj(&AccessRequest {
228-
token: "token",
229-
beamline: "i22",
230-
visit: 4,
231-
proposal: 1234,
232-
audience: AUDIENCE,
233-
});
234-
then.status(200).json_body_obj(&Response { result: true });
236+
.json_body_obj(&json!({
237+
"input": {
238+
"token": "token",
239+
"beamline": "i22",
240+
"visit": 4,
241+
"proposal": 1234,
242+
"audience": "account"
243+
}
244+
}));
245+
then.status(200).json_body_obj(&json!({"result": true}));
235246
})
236247
.await;
237248
let check = PolicyCheck::new(PolicyOptions {
@@ -253,12 +264,14 @@ mod tests {
253264
.mock_async(|when, then| {
254265
when.method("POST")
255266
.path("/demo/admin")
256-
.json_body_obj(&AdminRequest {
257-
token: "token",
258-
beamline: "i22",
259-
audience: AUDIENCE,
260-
});
261-
then.status(200).json_body_obj(&Response { result: true });
267+
.json_body_obj(&json!({
268+
"input": {
269+
"token": "token",
270+
"beamline": "i22",
271+
"audience": "account"
272+
}
273+
}));
274+
then.status(200).json_body_obj(&json!({"result": true}));
262275
})
263276
.await;
264277
let check = PolicyCheck::new(PolicyOptions {
@@ -280,14 +293,16 @@ mod tests {
280293
.mock_async(|when, then| {
281294
when.method("POST")
282295
.path("/demo/access")
283-
.json_body_obj(&AccessRequest {
284-
token: "token",
285-
beamline: "i22",
286-
proposal: 1234,
287-
visit: 4,
288-
audience: AUDIENCE,
289-
});
290-
then.status(200).json_body_obj(&Response { result: false });
296+
.json_body_obj(&json!({
297+
"input": {
298+
"token": "token",
299+
"beamline": "i22",
300+
"proposal": 1234,
301+
"visit": 4,
302+
"audience": "account"
303+
}
304+
}));
305+
then.status(200).json_body_obj(&json!({"result": false}));
291306
})
292307
.await;
293308
let check = PolicyCheck::new(PolicyOptions {
@@ -312,12 +327,14 @@ mod tests {
312327
.mock_async(|when, then| {
313328
when.method("POST")
314329
.path("/demo/admin")
315-
.json_body_obj(&AdminRequest {
316-
token: "token",
317-
beamline: "i22",
318-
audience: AUDIENCE,
319-
});
320-
then.status(200).json_body_obj(&Response { result: false });
330+
.json_body_obj(&json!({
331+
"input": {
332+
"token": "token",
333+
"beamline": "i22",
334+
"audience": "account"
335+
}
336+
}));
337+
then.status(200).json_body_obj(&json!({"result": false}));
321338
})
322339
.await;
323340
let check = PolicyCheck::new(PolicyOptions {

0 commit comments

Comments
 (0)