httpd: Move `test` utility mod from inside `api` to root

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2023-02-17 12:41:33 +00:00 committed by Alexis Sellier
parent c1b6ceb141
commit f290f6b4a5
8 changed files with 19 additions and 17 deletions

View File

@ -1,5 +1,4 @@
#[cfg(test)] pub mod auth;
pub mod test;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
@ -21,7 +20,6 @@ use radicle::identity::Id;
use radicle::storage::{ReadRepository, ReadStorage}; use radicle::storage::{ReadRepository, ReadStorage};
use radicle::Profile; use radicle::Profile;
mod auth;
mod axum_extra; mod axum_extra;
mod error; mod error;
mod json; mod json;
@ -70,6 +68,11 @@ impl Context {
pub fn profile(&self) -> &Arc<Profile> { pub fn profile(&self) -> &Arc<Profile> {
&self.profile &self.profile
} }
#[cfg(test)]
pub fn sessions(&self) -> &Arc<RwLock<HashMap<SessionId, auth::Session>>> {
&self.sessions
}
} }
pub fn router(ctx: Context) -> Router { pub fn router(ctx: Context) -> Router {

View File

@ -74,7 +74,7 @@ mod routes {
use axum::http::StatusCode; use axum::http::StatusCode;
use serde_json::json; use serde_json::json;
use crate::api::test::{self, get, HEAD}; use crate::test::{self, get, HEAD};
#[tokio::test] #[tokio::test]
async fn test_delegates_projects() { async fn test_delegates_projects() {

View File

@ -562,7 +562,7 @@ mod routes {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serde_json::json; use serde_json::json;
use crate::api::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID}; use crate::test::{self, get, patch, post, HEAD, HEAD_1, ISSUE_ID, PATCH_ID};
const CREATED_ISSUE_ID: &str = "b56febfba1e7dd20f4aea43ca2fe9dcf1fd448ba"; const CREATED_ISSUE_ID: &str = "b56febfba1e7dd20f4aea43ca2fe9dcf1fd448ba";

View File

@ -125,10 +125,8 @@ mod routes {
use axum::http::StatusCode; use axum::http::StatusCode;
use radicle_cli::commands::rad_web::{self, SessionInfo}; use radicle_cli::commands::rad_web::{self, SessionInfo};
use crate::api::{ use crate::api::auth::{AuthState, Session};
auth::{AuthState, Session}, use crate::test::{self, get, post, put};
test::{self, get, post, put},
};
#[tokio::test] #[tokio::test]
async fn test_session() { async fn test_session() {

View File

@ -29,7 +29,7 @@ mod routes {
use axum::http::StatusCode; use axum::http::StatusCode;
use serde_json::json; use serde_json::json;
use crate::api::test::{self, get}; use crate::test::{self, get};
#[tokio::test] #[tokio::test]
async fn test_stats() { async fn test_stats() {

View File

@ -203,7 +203,7 @@ mod routes {
use axum::http::{Method, StatusCode}; use axum::http::{Method, StatusCode};
use tower::ServiceExt; use tower::ServiceExt;
use crate::api::test; use crate::test;
#[tokio::test] #[tokio::test]
async fn test_invalid_route_returns_404() { async fn test_invalid_route_returns_404() {

View File

@ -17,6 +17,8 @@ use tracing::Span;
mod api; mod api;
mod git; mod git;
#[cfg(test)]
mod test;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Options { pub struct Options {
@ -30,9 +32,11 @@ pub async fn run(options: Options) -> anyhow::Result<()> {
.output() .output()
.context("'git' command must be available")? .context("'git' command must be available")?
.stdout; .stdout;
tracing::info!("{}", str::from_utf8(&git_version)?.trim()); tracing::info!("{}", str::from_utf8(&git_version)?.trim());
let profile = Arc::new(radicle::Profile::load()?); let profile = Arc::new(radicle::Profile::load()?);
tracing::info!("using radicle home at {}", profile.home().display()); tracing::info!("using radicle home at {}", profile.home().display());
let ctx = api::Context::new(profile.clone()); let ctx = api::Context::new(profile.clone());

View File

@ -136,21 +136,18 @@ pub fn seed(dir: &Path) -> Context {
) )
.unwrap(); .unwrap();
Context { Context::new(Arc::new(profile))
profile: Arc::new(profile),
sessions: Default::default(),
}
} }
/// Adds an authorized session to the Context::sessions HashMap. /// Adds an authorized session to the Context::sessions HashMap.
pub async fn create_session(ctx: Context) { pub async fn create_session(ctx: Context) {
let issued_at = OffsetDateTime::now_utc(); let issued_at = OffsetDateTime::now_utc();
let mut sessions = ctx.sessions.write().await; let mut sessions = ctx.sessions().write().await;
sessions.insert( sessions.insert(
String::from("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"), String::from("u9MGAkkfkMOv0uDDB2WeUHBT7HbsO2Dy"),
auth::Session { auth::Session {
status: auth::AuthState::Authorized, status: auth::AuthState::Authorized,
public_key: ctx.profile.public_key, public_key: ctx.profile().public_key,
issued_at, issued_at,
expires_at: issued_at expires_at: issued_at
.checked_add(auth::AUTHORIZED_SESSIONS_EXPIRATION) .checked_add(auth::AUTHORIZED_SESSIONS_EXPIRATION)