Lines
100 %
Functions
Branches
//! The guard leaves no database behind, on a normal exit or a panic.
#![cfg(feature = "db")]
use sqlx::{Connection, PgConnection};
use tests_integration::tenant_guard::TestTenant;
use uuid::Uuid;
async fn connect() -> anyhow::Result<PgConnection> {
Ok(PgConnection::connect(&std::env::var("DATABASE_URL")?).await?)
}
async fn create(name: &str) -> anyhow::Result<()> {
sqlx::query(sqlx::AssertSqlSafe(format!("CREATE DATABASE \"{name}\"")))
.execute(&mut connect().await?)
.await?;
Ok(())
async fn exists(name: &str) -> anyhow::Result<bool> {
Ok(
sqlx::query_scalar("SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname = $1)")
.bind(name)
.fetch_one(&mut connect().await?)
.await?,
)
fn unique_name() -> String {
format!("nomi_fleet_{}", Uuid::new_v4().simple())
#[tokio::test]
async fn dropping_the_guard_removes_the_database() -> anyhow::Result<()> {
let name = unique_name();
create(&name).await?;
drop(TestTenant::database(name.clone()));
assert!(!exists(&name).await?);
async fn a_panicking_body_still_removes_the_database() -> anyhow::Result<()> {
let held = name.clone();
let body = std::thread::spawn(move || {
let _tenant = TestTenant::database(held);
panic!("the test body fails after provisioning");
});
assert!(body.join().is_err(), "the body must have panicked");
async fn the_guard_removes_the_directory_row_too() -> anyhow::Result<()> {
let id = Uuid::new_v4();
sqlx::query(
"INSERT INTO users (id, user_name, email, user_password, database_name) \
VALUES ($1, 'guard', $2, 'x', $3)",
.bind(id)
.bind(format!("guard-{id}@example.test"))
.bind(&name)
drop(TestTenant::tenant(id, name.clone()));
let rows: i64 = sqlx::query_scalar("SELECT count(*) FROM users WHERE id = $1")
assert_eq!(rows, 0);
async fn a_name_that_is_not_a_test_database_is_never_dropped() -> anyhow::Result<()> {
let name = format!("throwaway_guard_{}", Uuid::new_v4().simple());
let refused = TestTenant::database(name.clone()).remove();
let kept = exists(&name).await?;
let directory: String = sqlx::query_scalar("SELECT current_database()")
let admin_refused = TestTenant::database(directory.clone()).remove();
let admin_kept = exists(&directory).await?;
sqlx::query(sqlx::AssertSqlSafe(format!("DROP DATABASE \"{name}\"")))
assert!(refused.is_err());
assert!(kept, "a database outside the test prefixes must survive");
assert!(admin_refused.is_err());
assert!(admin_kept);
/// Registers `email` as the server stores it, then removes the tenant by that email.
async fn remove_registered(id: Uuid, email: &str) -> anyhow::Result<()> {
let guard = TestTenant::registered(email);
let name = tests_integration::tenant_guard::tenant_database(id);
.bind(server::command::user::fold_email(email))
guard.remove().map_err(anyhow::Error::msg)?;
async fn a_registered_email_resolves_to_its_tenant_when_dropped() -> anyhow::Result<()> {
remove_registered(id, &format!("guard-{id}@example.test")).await
async fn a_registered_email_is_folded_by_ascii_case_only_like_the_server() -> anyhow::Result<()> {
remove_registered(id, &format!("GUARD-\u{c4}\u{c9}-{id}@example.test")).await
/// A drop that fails must leave the directory row, which still names the
/// database, so the leftover can be found and removed again.
async fn a_failed_drop_keeps_the_directory_row() -> anyhow::Result<()> {
let outcome = failed_drop_outcome(&name, id).await;
// A template database cannot be dropped, so it is reset before any exit.
set_template(&name, false).await?;
TestTenant::tenant(id, name.clone())
.remove()
.map_err(anyhow::Error::msg)?;
let (removed, rows) = outcome?;
assert!(removed.is_err(), "the drop must have failed");
assert_eq!(rows, 1, "the row must outlive the database it names");
async fn set_template(name: &str, template: bool) -> anyhow::Result<()> {
sqlx::query(sqlx::AssertSqlSafe(format!(
"ALTER DATABASE \"{name}\" IS_TEMPLATE {template}"
)))
/// Removes a tenant whose database refuses the drop; reports the removal's
/// result and how many directory rows are left.
async fn failed_drop_outcome(name: &str, id: Uuid) -> anyhow::Result<(Result<(), String>, i64)> {
set_template(name, true).await?;
let removed = TestTenant::tenant(id, name.to_owned()).remove();
let rows = sqlx::query_scalar("SELECT count(*) FROM users WHERE id = $1")
Ok((removed, rows))