use glacier::middleware::{Pipeline, VerifyAuthorized};
use glacier::prelude::*;
use glacier::testing::TestClient;
fn client() -> TestClient {
TestClient::new(App::new().routes(|r| {
r.pipe_through(&Pipeline::api().with(VerifyAuthorized), |r| {
r.get("/allowed", |_ctx: Context| async {
glacier::authorize(true)?;
Ok::<_, Error>(Response::text("ok"))
});
r.get("/denied", |_ctx: Context| async {
glacier::authorize(false)?;
Ok::<_, Error>(Response::text("unreachable"))
});
r.get("/public", |_ctx: Context| async {
glacier::skip_authorization();
Response::text("public")
});
r.get("/forgot", |_ctx: Context| async {
Response::text("open to all")
});
r.get("/missing", |_ctx: Context| async {
Err::<Response, _>(Error::not_found())
});
});
r.get("/outside", |_ctx: Context| async {
Response::text("unchecked")
});
}))
}
#[tokio::test]
async fn actions_must_authorize() {
let c = client();
assert_eq!(c.get("/allowed").await.body_text(), "ok");
assert_eq!(c.get("/denied").await.status(), StatusCode::FORBIDDEN);
assert_eq!(c.get("/public").await.body_text(), "public");
let forgot = c.get("/forgot").await;
assert_eq!(
forgot.status(),
StatusCode::INTERNAL_SERVER_ERROR,
"the miss is an error outside production"
);
assert_eq!(
c.get("/missing").await.status(),
StatusCode::NOT_FOUND,
"errors pass through"
);
assert_eq!(
c.get("/outside").await.body_text(),
"unchecked",
"other pipelines aren't checked"
);
}