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

Commit

Permalink
simplify test code
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Aug 20, 2024
1 parent 2eb7f8d commit af4414e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 35 deletions.
5 changes: 1 addition & 4 deletions openadr-client/src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use openadr_wire::program::ProgramContent;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = openadr_client::Client::with_url(
"http://localhost:3000/".try_into()?,
Some(ClientCredentials::new(
"admin".to_string(),
"admin".to_string(),
)),
Some(ClientCredentials::admin()),
);
let _created_program = client.create_program(ProgramContent::new("name")).await?;
// let created_program_1 = client.create_program(ProgramContent::new("name1")).await?;
Expand Down
4 changes: 4 additions & 0 deletions openadr-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl ClientCredentials {
default_credential_expires_in: Duration::from_secs(3600),
}
}

pub fn admin() -> Self {
Self::new("admin".to_string(), "admin".to_string())
}
}

struct AuthToken {
Expand Down
18 changes: 6 additions & 12 deletions openadr-client/tests/basic-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@ mod helper {
pub fn setup_mock_client() -> Client {
use openadr_vtn::{data_source::InMemoryStorage, jwt::JwtManager, state::AppState};

let auth_info = AuthInfo::bl_admin();
let client_credentials = ClientCredentials::admin();

let storage = InMemoryStorage::default();
storage.auth.try_write().unwrap().push(AuthInfo {
client_id: "admin".into(),
client_secret: "admin".into(),
role: openadr_vtn::jwt::AuthRole::BL,
ven: None,
});
storage.auth.try_write().unwrap().push(auth_info);

let app_state = AppState::new(storage, JwtManager::from_secret(b"test"));

MockClientRef::new(app_state.into_router())
.into_client(Some(ClientCredentials::new("admin".into(), "admin".into())))
MockClientRef::new(app_state.into_router()).into_client(Some(client_credentials))
}

pub fn setup_url_client(url: Url) -> Client {
Client::with_url(
url,
Some(ClientCredentials::new("admin".into(), "admin".into())),
)
Client::with_url(url, Some(ClientCredentials::admin()))
}

pub fn setup_client() -> Client {
Expand Down
7 changes: 1 addition & 6 deletions openadr-vtn/src/api/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,7 @@ mod test {
fn state_with_events(events: Vec<Event>) -> AppState {
let store = InMemoryStorage::default();

store.auth.try_write().unwrap().push(AuthInfo {
client_id: "admin".to_string(),
client_secret: "admin".to_string(),
role: AuthRole::BL,
ven: None,
});
store.auth.try_write().unwrap().push(AuthInfo::bl_admin());

{
let mut writer = store.events.try_write().unwrap();
Expand Down
7 changes: 1 addition & 6 deletions openadr-vtn/src/api/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,7 @@ mod test {
fn state_with_programs(programs: Vec<Program>) -> AppState {
let store = InMemoryStorage::default();

store.auth.try_write().unwrap().push(AuthInfo {
client_id: "admin".to_string(),
client_secret: "admin".to_string(),
role: AuthRole::BL,
ven: None,
});
store.auth.try_write().unwrap().push(AuthInfo::bl_admin());

{
let mut writer = store.programs.try_write().unwrap();
Expand Down
11 changes: 11 additions & 0 deletions openadr-vtn/src/data_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ pub struct AuthInfo {
pub ven: Option<String>,
}

impl AuthInfo {
pub fn bl_admin() -> Self {
Self {
client_id: "admin".to_string(),
client_secret: "admin".to_string(),
role: AuthRole::BL,
ven: None,
}
}
}

#[derive(Default, Clone)]
pub struct InMemoryStorage {
pub programs: Arc<RwLock<HashMap<ProgramId, Program>>>,
Expand Down
9 changes: 2 additions & 7 deletions openadr-vtn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, EnvFilter};

use openadr_vtn::data_source::{AuthInfo, InMemoryStorage};
use openadr_vtn::jwt::{AuthRole, JwtManager};
use openadr_vtn::jwt::JwtManager;
use openadr_vtn::state::AppState;

#[tokio::main]
Expand All @@ -21,12 +21,7 @@ async fn main() {
info!("listening on http://{}", listener.local_addr().unwrap());

let storage = InMemoryStorage::default();
storage.auth.write().await.push(AuthInfo {
client_id: "admin".to_string(),
client_secret: "admin".to_string(),
role: AuthRole::BL,
ven: None,
});
storage.auth.write().await.push(AuthInfo::bl_admin());
let state = AppState::new(storage, JwtManager::from_base64_secret("test").unwrap());

if let Err(e) = axum::serve(listener, state.into_router())
Expand Down

0 comments on commit af4414e

Please sign in to comment.