test(db): add create_test_pool and use in tests
This commit is contained in:
1
server/Cargo.lock
generated
1
server/Cargo.lock
generated
@@ -103,6 +103,7 @@ dependencies = [
|
|||||||
"secrecy",
|
"secrecy",
|
||||||
"smlang",
|
"smlang",
|
||||||
"statig",
|
"statig",
|
||||||
|
"tempfile",
|
||||||
"test-log",
|
"test-log",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|||||||
@@ -22,3 +22,4 @@ prost-build = "0.14.3"
|
|||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
tonic-prost-build = "0.14.3"
|
tonic-prost-build = "0.14.3"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -58,3 +58,4 @@ prost-types.workspace = true
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
test-log = { version = "0.2", default-features = false, features = ["trace"] }
|
test-log = { version = "0.2", default-features = false, features = ["trace"] }
|
||||||
|
tempfile = "3.25.0"
|
||||||
@@ -322,9 +322,7 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[test_log::test]
|
#[test_log::test]
|
||||||
pub async fn test_bootstrap_token_auth() {
|
pub async fn test_bootstrap_token_auth() {
|
||||||
let db = db::create_pool(Some("sqlite://:memory:"))
|
let db = db::create_test_pool().await;
|
||||||
.await
|
|
||||||
.expect("Failed to create database pool");
|
|
||||||
// explicitly not installing any user_agent pubkeys
|
// explicitly not installing any user_agent pubkeys
|
||||||
let bootstrapper = BootstrapActor::new(&db).await.unwrap(); // this will create bootstrap token
|
let bootstrapper = BootstrapActor::new(&db).await.unwrap(); // this will create bootstrap token
|
||||||
let token = bootstrapper.get_token().unwrap();
|
let token = bootstrapper.get_token().unwrap();
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
use arbiter_proto::{BOOTSTRAP_TOKEN_PATH, home_path};
|
use arbiter_proto::{BOOTSTRAP_TOKEN_PATH, home_path};
|
||||||
use diesel::{
|
use diesel::{ExpressionMethods, QueryDsl};
|
||||||
ExpressionMethods, QueryDsl,
|
|
||||||
dsl::{count, exists},
|
|
||||||
select,
|
|
||||||
};
|
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
use kameo::{Actor, messages};
|
use kameo::{Actor, messages};
|
||||||
use memsafe::MemSafe;
|
use memsafe::MemSafe;
|
||||||
@@ -61,16 +57,14 @@ impl BootstrapActor {
|
|||||||
pub async fn new(db: &DatabasePool) -> Result<Self, BootstrapError> {
|
pub async fn new(db: &DatabasePool) -> Result<Self, BootstrapError> {
|
||||||
let mut conn = db.get().await?;
|
let mut conn = db.get().await?;
|
||||||
|
|
||||||
let needs_token: bool = select(exists(
|
let row_count: i64 = schema::useragent_client::table
|
||||||
schema::useragent_client::table
|
.count()
|
||||||
.filter(schema::useragent_client::id.eq(schema::useragent_client::id)), // Just check if the table is empty
|
.get_result(&mut conn)
|
||||||
))
|
.await?;
|
||||||
.first(&mut conn)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
drop(conn);
|
drop(conn);
|
||||||
|
|
||||||
let token = if needs_token {
|
let token = if row_count == 0 {
|
||||||
let token = generate_token().await?;
|
let token = generate_token().await?;
|
||||||
info!(%token, "Generated bootstrap token");
|
info!(%token, "Generated bootstrap token");
|
||||||
tokio::fs::write(home_path()?.join(BOOTSTRAP_TOKEN_PATH), token.as_str()).await?;
|
tokio::fs::write(home_path()?.join(BOOTSTRAP_TOKEN_PATH), token.as_str()).await?;
|
||||||
|
|||||||
@@ -133,3 +133,20 @@ pub async fn create_pool(url: Option<&str>) -> Result<DatabasePool, DatabaseSetu
|
|||||||
|
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub async fn create_test_pool() -> DatabasePool {
|
||||||
|
use rand::distr::{Alphanumeric, SampleString as _};
|
||||||
|
|
||||||
|
let tempfile_name = Alphanumeric.sample_string(&mut rand::rng(), 16);
|
||||||
|
|
||||||
|
let file = std::env::temp_dir().join(tempfile_name);
|
||||||
|
let url = format!(
|
||||||
|
"{}?mode=rwc",
|
||||||
|
file.to_str().expect("temp file path is not valid UTF-8")
|
||||||
|
);
|
||||||
|
|
||||||
|
create_pool(Some(&url))
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test database pool")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user