Lines
100 %
Functions
Branches
use std::cell::RefCell;
use std::future::Future;
use server::user::TenantConnection;
use sqlx::types::Uuid;
use tests_integration::tenant_guard::TestTenant;
tokio::task_local! {
static HELD: RefCell<Vec<TestTenant>>;
}
/// Runs a test body so that every tenant it provisions is removed when the body
/// ends, whether it returns, fails an assertion or panics.
pub async fn scoped<F: Future>(body: F) -> F::Output {
let ended = async {
let output = body.await;
remove_held();
output
};
HELD.scope(RefCell::new(Vec::new()), ended).await
/// On a normal end, so that a failed cleanup fails the test.
fn remove_held() {
let held = HELD.with(|held| held.take());
for tenant in held {
drop(tenant);
pub async fn provision_tenant(user_id: Uuid) {
hold(TestTenant::provisioned(user_id));
let database_name = server::provision::create_user_database(user_id)
.await
.expect("provision tenant");
let mut conn = server::db::get_connection().await.expect("connection");
sqlx::query(
"INSERT INTO users (id, user_name, email, user_password, database_name, \
jwt_public_key, verified) VALUES ($1, $2, $3, 'unused', $4, $5, TRUE)",
)
.bind(user_id)
.bind(format!("probe-{}", user_id.simple()))
.bind(format!("tags-{}@example.test", user_id.simple()))
.bind(database_name)
.bind(crate::common::TEST_PUBLIC_KEY_B64)
.execute(&mut *conn)
.expect("seed directory row");
fn hold(tenant: TestTenant) {
HELD.try_with(|held| held.borrow_mut().push(tenant))
.expect("tenants are provisioned inside a shared_runtime_test body");
/// Removes a tenant before the body ends, for tests that need it gone.
pub fn release_tenant(user_id: Uuid) {
let released = HELD
.try_with(|held| {
let mut held = held.borrow_mut();
let index = held
.iter()
.position(|tenant| tenant.user() == Some(user_id));
index.map(|index| held.remove(index))
})
.expect("tenants are released inside a shared_runtime_test body");
drop(released);
pub async fn tenant_connection(user_id: Uuid) -> TenantConnection {
server::user::User { id: user_id }
.get_connection()
.expect("tenant connection")