Skip to content

Commit

Permalink
Fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Mar 18, 2024
1 parent e3b84e3 commit 21ab9cb
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 42 deletions.
18 changes: 12 additions & 6 deletions v-api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,9 +1563,9 @@ where
mod tests {
use chrono::{Duration, Utc};
use mockall::predicate::eq;
use v_api_permissions::Permissions;
use std::{collections::BTreeSet, ops::Add, sync::Arc};
use uuid::Uuid;
use v_api_permissions::Permissions;
use v_model::{
storage::{AccessGroupFilter, ListPagination, MockAccessGroupStore, MockApiUserStore},
AccessGroup, ApiUser, ApiUserProvider,
Expand All @@ -1576,7 +1576,8 @@ mod tests {
jwt::{Claims, Jwt},
AuthToken,
},
context::UNLIMITED, permissions::VPermission,
context::UNLIMITED,
permissions::VPermission,
};

use super::{
Expand Down Expand Up @@ -1695,7 +1696,10 @@ mod tests {
.await
.unwrap();
assert_eq!(
Permissions::<VPermission>::from(vec![VPermission::CreateGroup, VPermission::ListMappers,]),
Permissions::<VPermission>::from(vec![
VPermission::CreateGroup,
VPermission::ListMappers,
]),
permissions.permissions
);
}
Expand All @@ -1720,7 +1724,10 @@ pub(crate) mod test_mocks {
};

use crate::{
config::JwtConfig, endpoints::login::oauth::{google::GoogleOAuthProvider, OAuthProviderName}, permissions::VPermission, util::tests::mock_key
config::JwtConfig,
endpoints::login::oauth::{google::GoogleOAuthProvider, OAuthProviderName},
permissions::VPermission,
util::tests::mock_key,
};

use super::VContext;
Expand Down Expand Up @@ -2117,8 +2124,7 @@ pub(crate) mod test_mocks {
&self,
filter: v_model::storage::AccessGroupFilter,
pagination: &ListPagination,
) -> Result<Vec<v_model::AccessGroup<VPermission>>, v_model::storage::StoreError>
{
) -> Result<Vec<v_model::AccessGroup<VPermission>>, v_model::storage::StoreError> {
self.access_group_store
.as_ref()
.unwrap()
Expand Down
132 changes: 100 additions & 32 deletions v-api/src/endpoints/api_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use crate::{
error::ApiError,
permissions::{PermissionStorage, VAppPermission, VAppPermissionResponse},
secrets::OpenApiSecretString,
util::response::{bad_request, not_found, to_internal_error, unauthorized}, VContext,
util::response::{bad_request, not_found, to_internal_error, unauthorized},
VContext,
};

fn into_user_response<T, U>(user: ApiUser<T>) -> ApiUser<U>
Expand Down Expand Up @@ -565,17 +566,22 @@ mod tests {
use chrono::{Duration, Utc};
use http::StatusCode;
use mockall::predicate::eq;
use uuid::Uuid;
use v_api_permissions::{Caller, Permissions};
use v_model::{
storage::{ApiKeyFilter, ListPagination, MockApiKeyStore, MockApiUserStore, StoreError},
ApiKey, ApiUser, NewApiUser,
};
use uuid::Uuid;

use crate::{
context::test_mocks::{mock_context, MockStorage}, endpoints::api_user::{
create_api_user_inner, create_api_user_token_inner, delete_api_user_token_inner, get_api_user_token_inner, list_api_user_tokens_inner, update_api_user_inner, ApiKeyCreateParams, ApiUserPath, ApiUserTokenPath
}, permissions::{VPermission, VPermissionResponse}, util::tests::get_status
context::test_mocks::{mock_context, MockStorage},
endpoints::api_user::{
create_api_user_inner, create_api_user_token_inner, delete_api_user_token_inner,
get_api_user_token_inner, list_api_user_tokens_inner, update_api_user_inner,
ApiKeyCreateParams, ApiUserPath, ApiUserTokenPath,
},
permissions::{VPermission, VPermissionResponse},
util::tests::get_status,
};

use super::ApiUserUpdateParams;
Expand Down Expand Up @@ -639,7 +645,12 @@ mod tests {
permissions: Permissions::new(),
};

let resp = create_api_user_inner::<VPermission, VPermissionResponse>(&ctx, no_permissions, successful_update.clone()).await;
let resp = create_api_user_inner::<VPermission, VPermissionResponse>(
&ctx,
no_permissions,
successful_update.clone(),
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::FORBIDDEN);
Expand All @@ -652,13 +663,23 @@ mod tests {
permissions: vec![VPermission::CreateApiUser].into(),
};

let resp = create_api_user_inner::<VPermission, VPermissionResponse>(&ctx, with_permissions.clone(), successful_update.clone()).await;
let resp = create_api_user_inner::<VPermission, VPermissionResponse>(
&ctx,
with_permissions.clone(),
successful_update.clone(),
)
.await;

assert!(resp.is_ok());
assert_eq!(get_status(&resp), StatusCode::CREATED);

// 3. Handle storage failure and return error
let resp = create_api_user_inner::<VPermission, VPermissionResponse>(&ctx, with_permissions, failure_update.clone()).await;
let resp = create_api_user_inner::<VPermission, VPermissionResponse>(
&ctx,
with_permissions,
failure_update.clone(),
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -965,9 +986,13 @@ mod tests {
permissions: Permissions::new(),
};

let resp =
create_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, no_permissions, api_user_path.clone(), new_token.clone())
.await;
let resp = create_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
no_permissions,
api_user_path.clone(),
new_token.clone(),
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::FORBIDDEN);
Expand Down Expand Up @@ -998,7 +1023,7 @@ mod tests {
id: user3.id,
permissions: vec![
VPermission::GetApiUser(unknown_api_user_path.identifier),
VPermission::CreateApiUserToken(unknown_api_user_path.identifier)
VPermission::CreateApiUserToken(unknown_api_user_path.identifier),
]
.into(),
};
Expand Down Expand Up @@ -1119,7 +1144,12 @@ mod tests {
permissions: Permissions::new(),
};

let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, no_permissions, api_user_token_path.clone()).await;
let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
no_permissions,
api_user_token_path.clone(),
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::FORBIDDEN);
Expand All @@ -1132,7 +1162,12 @@ mod tests {
permissions: vec![VPermission::GetApiUserToken(Uuid::new_v4())].into(),
};

let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, incorrect_permissions, api_user_token_path.clone()).await;
let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
incorrect_permissions,
api_user_token_path.clone(),
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::FORBIDDEN);
Expand All @@ -1148,8 +1183,12 @@ mod tests {
.into(),
};

let resp =
get_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, incorrect_permissions, unknown_api_user_token_path).await;
let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
incorrect_permissions,
unknown_api_user_token_path,
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::NOT_FOUND);
Expand All @@ -1159,13 +1198,18 @@ mod tests {
// 4. Succeed in getting token
let success_permissions = Caller {
id: user4.id,
permissions: vec![
VPermission::GetApiUserToken(api_user_token_path.token_identifier),
]
permissions: vec![VPermission::GetApiUserToken(
api_user_token_path.token_identifier,
)]
.into(),
};

let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, success_permissions, api_user_token_path).await;
let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
success_permissions,
api_user_token_path,
)
.await;

assert!(resp.is_ok());
assert_eq!(get_status(&resp), StatusCode::OK);
Expand All @@ -1181,8 +1225,12 @@ mod tests {
.into(),
};

let resp =
get_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, failure_permissions, failure_api_user_token_path).await;
let resp = get_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
failure_permissions,
failure_api_user_token_path,
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -1245,7 +1293,12 @@ mod tests {
permissions: Permissions::new(),
};

let resp = delete_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, no_permissions, api_user_token_path.clone()).await;
let resp = delete_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
no_permissions,
api_user_token_path.clone(),
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::FORBIDDEN);
Expand All @@ -1258,8 +1311,12 @@ mod tests {
permissions: vec![VPermission::DeleteApiUserToken(Uuid::new_v4())].into(),
};

let resp =
delete_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, incorrect_permissions, api_user_token_path.clone()).await;
let resp = delete_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
incorrect_permissions,
api_user_token_path.clone(),
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::FORBIDDEN);
Expand All @@ -1275,9 +1332,12 @@ mod tests {
.into(),
};

let resp =
delete_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, incorrect_permissions, unknown_api_user_token_path)
.await;
let resp = delete_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
incorrect_permissions,
unknown_api_user_token_path,
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::NOT_FOUND);
Expand All @@ -1293,7 +1353,12 @@ mod tests {
.into(),
};

let resp = delete_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, success_permissions, api_user_token_path).await;
let resp = delete_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
success_permissions,
api_user_token_path,
)
.await;

assert!(resp.is_ok());
assert_eq!(get_status(&resp), StatusCode::OK);
Expand All @@ -1309,9 +1374,12 @@ mod tests {
.into(),
};

let resp =
delete_api_user_token_inner::<VPermission, VPermissionResponse>(&ctx, failure_permissions, failure_api_user_token_path)
.await;
let resp = delete_api_user_token_inner::<VPermission, VPermissionResponse>(
&ctx,
failure_permissions,
failure_api_user_token_path,
)
.await;

assert!(resp.is_err());
assert_eq!(get_status(&resp), StatusCode::INTERNAL_SERVER_ERROR);
Expand Down
16 changes: 12 additions & 4 deletions v-api/src/endpoints/login/oauth/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use crate::{
context::{ApiContext, VContextWithCaller},
permissions::{PermissionStorage, VAppPermission, VPermission},
secrets::OpenApiSecretString,
util::response::to_internal_error, VContext,
util::response::to_internal_error,
VContext,
};

#[instrument(skip(rqctx), err(Debug))]
Expand Down Expand Up @@ -228,17 +229,21 @@ mod tests {

use chrono::Utc;
use mockall::predicate::eq;
use uuid::Uuid;
use v_api_permissions::Caller;
use v_model::{
storage::{MockApiUserStore, MockOAuthClientSecretStore, MockOAuthClientStore},
ApiUser, OAuthClient, OAuthClientSecret,
};
use uuid::Uuid;

use crate::{
authn::key::RawApiKey,
context::test_mocks::{mock_context, MockStorage},
endpoints::login::oauth::{client::{create_oauth_client_inner, create_oauth_client_secret_inner}, CheckOAuthClient}, permissions::VPermission,
endpoints::login::oauth::{
client::{create_oauth_client_inner, create_oauth_client_secret_inner},
CheckOAuthClient,
},
permissions::VPermission,
};

fn mock_user() -> ApiUser<VPermission> {
Expand Down Expand Up @@ -320,7 +325,10 @@ mod tests {

let ctx = mock_context(storage).await;

let mut client = create_oauth_client_inner(&ctx, caller.clone()).await.unwrap().0;
let mut client = create_oauth_client_inner(&ctx, caller.clone())
.await
.unwrap()
.0;
caller
.permissions
.insert(VPermission::UpdateOAuthClient(client.id));
Expand Down

0 comments on commit 21ab9cb

Please sign in to comment.