use sova_auth::CurrentUser;
use sova_core::TestClient;
#[cfg(feature = "notifications")]
use sova_notifications::NotificationUser;
pub trait ActingAs {
fn acting_as(&self, user: CurrentUser);
fn acting_as_id(&self, user_id: i64);
fn logout(&self);
}
impl ActingAs for TestClient {
fn acting_as(&self, user: CurrentUser) {
self.clear_request_hooks();
#[cfg(feature = "notifications")]
let uid = user.id;
self.on_request(move |req| {
req.set(user.clone());
});
#[cfg(feature = "notifications")]
self.on_request(move |req| {
req.set(NotificationUser(uid));
});
}
fn acting_as_id(&self, user_id: i64) {
#[cfg(feature = "notifications")]
{
self.clear_request_hooks();
self.on_request(move |req| {
req.set(NotificationUser(user_id));
});
}
#[cfg(not(feature = "notifications"))]
{
let _ = user_id;
panic!("acting_as_id requires sova-testing feature `notifications`");
}
}
fn logout(&self) {
self.clear_request_hooks();
}
}