use std::borrow::ToOwned;
use jiff::Timestamp;
use super::{Auth, Result};
use crate::core::password::hash_password_cheaply;
use crate::core::{Actor, new_session_id, session_expiry};
use crate::store::{Credential, Session, User};
use crate::types::NewUser;
impl Auth {
#[cfg_attr(watchword_docsrs, doc(cfg(feature = "test-util")))]
pub async fn test_user(&self, new: &NewUser, password: &str) -> Result<User> {
let user = self.import_user(new).await?;
let password = password.to_owned();
let hash = tokio::task::spawn_blocking(move || hash_password_cheaply(&password)).await??;
self.set_credential(Actor::System, user.id, &Credential::Password(hash), None)
.await
}
#[cfg_attr(watchword_docsrs, doc(cfg(feature = "test-util")))]
pub async fn test_session(&self, user: &User) -> Result<Session> {
let now = Timestamp::now();
let session = Session {
id: new_session_id(),
user: user.id,
impersonator: None,
created_at: now,
expires_at: session_expiry(now),
};
self.store().create_session(&session).await?;
Ok(session)
}
}