Skip to content

Commit

Permalink
Add test for abacus server start
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonbaeten committed Mar 4, 2025
1 parent 262a42f commit b3877c9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
8 changes: 0 additions & 8 deletions backend/src/audit_log/audit_log_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ pub struct AuditLogEvent {

#[cfg(test)]
impl AuditLogEvent {
pub fn time(&self) -> DateTime<Utc> {
self.time
}

pub fn event(&self) -> &AuditEvent {
&self.event
}
Expand All @@ -96,10 +92,6 @@ impl AuditLogEvent {
self.message.as_ref()
}

pub fn workstation(&self) -> Option<u32> {
self.workstation
}

pub fn user_id(&self) -> u32 {
self.user_id
}
Expand Down
72 changes: 56 additions & 16 deletions backend/src/bin/abacus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,48 @@ struct Args {
reset_database: bool,
}

/// Main entry point for the application. Sets up the database, and starts the
/// API server and in-memory file router on port 8080.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env()?,
)
.init();

let args = Args::parse();
let pool = create_sqlite_pool(&args).await?;

/// Start the API server on the given port, using the given database pool.
async fn start_server(pool: SqlitePool, port: u16) -> Result<(), Box<dyn Error>> {
let app = router(pool)?;

let address = SocketAddr::from((Ipv4Addr::UNSPECIFIED, args.port));
let address = SocketAddr::from((Ipv4Addr::UNSPECIFIED, port));
let listener = TcpListener::bind(&address).await?;

info!("Starting API server on http://{}", listener.local_addr()?);
let listener = listener.tap_io(|tcp_stream| {
if let Err(err) = tcp_stream.set_nodelay(true) {
trace!("failed to set TCP_NODELAY on incoming connection: {err:#}");

Check warning on line 48 in backend/src/bin/abacus.rs

View check run for this annotation

Codecov / codecov/patch

backend/src/bin/abacus.rs#L48

Added line #L48 was not covered by tests
}
});

axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.with_graceful_shutdown(shutdown_signal())
.await?;

Ok(())
}

Check warning on line 60 in backend/src/bin/abacus.rs

View check run for this annotation

Codecov / codecov/patch

backend/src/bin/abacus.rs#L59-L60

Added lines #L59 - L60 were not covered by tests

/// Main entry point for the application. Sets up the database, and starts the
/// API server and in-memory file router on port 8080.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env()?,
)
.init();

let args = Args::parse();
let pool = create_sqlite_pool(&args).await?;

start_server(pool, args.port).await

Check warning on line 77 in backend/src/bin/abacus.rs

View check run for this annotation

Codecov / codecov/patch

backend/src/bin/abacus.rs#L77

Added line #L77 was not covered by tests
}

/// Create a SQLite database if needed, then connect to it and run migrations.
/// Return a connection pool.
async fn create_sqlite_pool(
Expand Down Expand Up @@ -123,3 +131,35 @@ async fn shutdown_signal() {
_ = terminate => {},
}
}

#[cfg(test)]
mod test {
use rand::Rng;
use sqlx::SqlitePool;
use test_log::test;

use super::start_server;

pub fn random_port() -> u16 {
let mut rng = rand::rng();

rng.random_range(10_000..30_000)
}

#[test(sqlx::test)]
async fn test_abacus_starts(pool: SqlitePool) {
let random_port = random_port();
let task = tokio::spawn(async move {
start_server(pool, random_port).await.unwrap();
});

Check warning on line 154 in backend/src/bin/abacus.rs

View check run for this annotation

Codecov / codecov/patch

backend/src/bin/abacus.rs#L154

Added line #L154 was not covered by tests

let result = reqwest::get(format!("http://localhost:{random_port}/api/user/whoami"))
.await
.unwrap();

assert_eq!(result.status(), 401);

task.abort();
let _ = task.await;
}
}

0 comments on commit b3877c9

Please sign in to comment.