Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

VEN and Resource tests #129

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
47 changes: 35 additions & 12 deletions openadr-vtn/src/api/ven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ pub async fn get(
Path(id): Path<VenId>,
User(user): User,
) -> AppResponse<Ven> {
if user.is_ven() {
if !user.ven_ids().iter().any(|vid| *vid == id) {
return Err(AppError::Forbidden("User does not have access to this VEN"));
}
} else if !user.is_ven_manager() {
return Err(AppError::Forbidden("User is not a VEN or VEN Manager"));
}

let ven = ven_source.retrieve(&id, &user.try_into()?).await?;

Ok(Json(ven))
Expand Down Expand Up @@ -132,11 +124,11 @@ mod tests {
state::AppState,
};

async fn request_all(app: Router, token: &str) -> Response<Body> {
async fn request_all(app: Router, token: &str, query_params: &str) -> Response<Body> {
app.oneshot(
Request::builder()
.method(http::Method::GET)
.uri("/vens")
.uri(format!("/vens{query_params}"))
.header(http::header::AUTHORIZATION, format!("Bearer {}", token))
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::empty())
Expand Down Expand Up @@ -165,7 +157,7 @@ mod tests {
let token = jwt_test_token(&state, vec![AuthRole::VenManager]);
let app = state.into_router();

let resp = request_all(app, &token).await;
let resp = request_all(app, &token, "").await;

assert_eq!(resp.status(), http::StatusCode::OK);
let mut vens: Vec<Ven> = get_response_json(resp).await;
Expand All @@ -176,13 +168,44 @@ mod tests {
assert_eq!(vens[1].id.as_str(), "ven-2");
}

#[sqlx::test(fixtures("users", "vens"))]
async fn get_all_filetred(db: PgPool) {
let state = test_state(db);
let token = jwt_test_token(&state, vec![AuthRole::VenManager]);
let app = state.clone().into_router();

let resp = request_all(app.clone(), &token, "?skip=1").await;
assert_eq!(resp.status(), http::StatusCode::OK);
let vens: Vec<Ven> = get_response_json(resp).await;
assert_eq!(vens.len(), 1);

let resp = request_all(app.clone(), &token, "?limit=1").await;
assert_eq!(resp.status(), http::StatusCode::OK);
let vens: Vec<Ven> = get_response_json(resp).await;
assert_eq!(vens.len(), 1);

let resp = request_all(app.clone(), &token, "?targetType=VEN_NAME&targetValues=ven-2-name").await;
assert_eq!(resp.status(), http::StatusCode::OK);
let vens: Vec<Ven> = get_response_json(resp).await;
assert_eq!(vens.len(), 1);
assert_eq!(vens[0].id.as_str(), "ven-2");

let token = jwt_test_token(&state, vec![AuthRole::VEN("ven-1".parse().unwrap())]);
let app = state.into_router();

let resp = request_all(app.clone(), &token, "?targetType=VEN_NAME&targetValues=ven-1-name").await;
assert_eq!(resp.status(), http::StatusCode::OK);
let vens: Vec<Ven> = get_response_json(resp).await;
assert_eq!(vens.len(), 0);
}

#[sqlx::test(fixtures("users", "vens"))]
async fn get_all_ven_user(db: PgPool) {
let state = test_state(db);
let token = jwt_test_token(&state, vec![AuthRole::VEN("ven-1".parse().unwrap())]);
let app = state.into_router();

let resp = request_all(app, &token).await;
let resp = request_all(app, &token, "").await;

assert_eq!(resp.status(), http::StatusCode::OK);
let vens: Vec<Ven> = get_response_json(resp).await;
Expand Down
2 changes: 2 additions & 0 deletions openadr-vtn/src/data_source/postgres/ven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ impl Crud for PgVenStorage {
let pg_filter: PostgresFilter = filter.into();
trace!(?pg_filter);

dbg!(&pg_filter);

let ids = permissions.as_value();

let mut vens: Vec<Ven> = sqlx::query_as!(
Expand Down
Loading