Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace error catch AccessDenied with BadRequest #27

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion endpoint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The following endpoints are available:
- `/openapi.json`: OpenAPI description file which is automatically generated from the types defined in [`api_types.rs`](src/api_types.rs) and endpoints defined in [`main.rs`](src/main.rs)
- Swagger UI: `/swagger-ui/` if you fancy a visualization

No further endpoints are supported by this implementation and all return `{"message":"Access Denied", "code":"AccessDenied"}`.
No further endpoints are supported by this implementation and all return `{"message":"Bad Request","code":"BadRequest"`.

## Credentials

Expand Down
4 changes: 2 additions & 2 deletions endpoint/src/api_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub(crate) struct PfListingResponseInner {
pub(crate) enum EventsApiResponse {
#[response(status = 200)]
Ok(()),
#[response(status = 403, content_type = "application/json")]
NoAuth(crate::error::AccessDenied),
#[response(status = 400, content_type = "application/json")]
NoAuth(crate::error::BadRequest),
#[response(status = 501, content_type = "application/json")]
NotImpl(crate::error::NotImplemented),
#[response(status = 400, content_type = "application/json")]
Expand Down
24 changes: 12 additions & 12 deletions endpoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ fn get_list(
offset: usize,
filter: Filter,
host: Host,
) -> Either<PfListingResponse, error::AccessDenied> {
) -> Either<PfListingResponse, error::BadRequest> {
if auth.is_none() {
return Either::Right(Default::default());
}
Expand Down Expand Up @@ -377,7 +377,7 @@ fn get_footprints(
limit: Option<usize>,
filter: Filter,
host: Host,
) -> Either<PfListingResponse, error::AccessDenied> {
) -> Either<PfListingResponse, error::BadRequest> {
let limit = limit.unwrap_or(ACTION_LIST_FOOTPRINTS_MIN_RESULTS);
let offset = 0;
get_list(auth, limit, offset, filter, host)
Expand All @@ -388,7 +388,7 @@ fn get_footprints(
fn get_pcf(
id: PfId,
auth: Option<UserToken>,
) -> Either<Json<ProductFootprintResponse>, error::AccessDenied> {
) -> Either<Json<ProductFootprintResponse>, error::BadRequest> {
if auth.is_some() {
PCF_DEMO_DATA
.iter()
Expand All @@ -401,7 +401,7 @@ fn get_pcf(
}

#[get("/2/footprints/<_id>", format = "json", rank = 2)]
fn get_pcf_unauth(_id: &str) -> error::AccessDenied {
fn get_pcf_unauth(_id: &str) -> error::BadRequest {
Default::default()
}

Expand Down Expand Up @@ -433,7 +433,7 @@ fn post_event(

#[post("/2/events", rank = 2)]
fn post_event_fallback() -> EventsApiResponse {
EventsApiResponse::NoAuth(error::AccessDenied::default())
EventsApiResponse::NoAuth(error::BadRequest::default())
}

#[get("/")]
Expand All @@ -447,7 +447,7 @@ fn bad_request() -> error::BadRequest {
}

#[catch(default)]
fn default_handler() -> error::AccessDenied {
fn default_handler() -> error::BadRequest {
Default::default()
}

Expand Down Expand Up @@ -633,7 +633,7 @@ fn get_list_test() {
.get(get_list_uri)
.header(rocket::http::Header::new("Host", EXAMPLE_HOST))
.dispatch();
assert_eq!(rocket::http::Status::Forbidden, resp.status());
assert_eq!(rocket::http::Status::BadRequest, resp.status());
}
}

Expand Down Expand Up @@ -854,13 +854,13 @@ fn post_events_test() {
bearer_token.clone(),
))
.dispatch();
assert_eq!(rocket::http::Status::Forbidden, resp.status());
assert_eq!(rocket::http::Status::BadRequest, resp.status());
}

// test unauth request
{
let resp = client.post(post_events_uri).dispatch();
assert_eq!(rocket::http::Status::Forbidden, resp.status());
assert_eq!(rocket::http::Status::BadRequest, resp.status());
}

// test authenticated request with OK body
Expand Down Expand Up @@ -928,7 +928,7 @@ fn get_pcf_test() {
{
let get_pcf_uri = format!("/2/footprints/{}", PCF_DEMO_DATA[2].id.0);
let resp = client.get(get_pcf_uri).dispatch();
assert_eq!(rocket::http::Status::Forbidden, resp.status());
assert_eq!(rocket::http::Status::BadRequest, resp.status());
}

// test malformed PCF ID
Expand All @@ -941,7 +941,7 @@ fn get_pcf_test() {
bearer_token.clone(),
))
.dispatch();
assert_eq!(rocket::http::Status::Forbidden, resp.status());
assert_eq!(rocket::http::Status::BadRequest, resp.status());
}
// test unknown PCF ID
{
Expand All @@ -950,6 +950,6 @@ fn get_pcf_test() {
.get(get_pcf_uri)
.header(rocket::http::Header::new("Authorization", bearer_token))
.dispatch();
assert_eq!(rocket::http::Status::Forbidden, resp.status());
assert_eq!(rocket::http::Status::BadRequest, resp.status());
}
}
Loading