use std::collections::HashMap;
use std::sync::Arc;
use sqlx::PgPool;
use uuid::Uuid;
use super::super::mock_dispatch::{MockJobDispatch, MockWorkflowDispatch};
use super::super::mock_http::{MockHttp, MockRequest, MockResponse};
use crate::function::{AuthContext, RequestMetadata};
use crate::Result;
pub struct TestActionContext {
pub auth: AuthContext,
pub request: RequestMetadata,
pool: Option<PgPool>,
http: Arc<MockHttp>,
job_dispatch: Arc<MockJobDispatch>,
workflow_dispatch: Arc<MockWorkflowDispatch>,
}
impl TestActionContext {
pub fn builder() -> TestActionContextBuilder {
TestActionContextBuilder::default()
}
pub fn minimal() -> Self {
Self::builder().build()
}
pub fn authenticated(user_id: Uuid) -> Self {
Self::builder().as_user(user_id).build()
}
pub fn db(&self) -> Option<&PgPool> {
self.pool.as_ref()
}
pub fn http(&self) -> &MockHttp {
&self.http
}
pub fn job_dispatch(&self) -> &MockJobDispatch {
&self.job_dispatch
}
pub fn workflow_dispatch(&self) -> &MockWorkflowDispatch {
&self.workflow_dispatch
}
pub fn require_user_id(&self) -> Result<Uuid> {
self.auth.require_user_id()
}
pub async fn dispatch_job<T: serde::Serialize>(&self, job_type: &str, args: T) -> Result<Uuid> {
self.job_dispatch.dispatch(job_type, args).await
}
pub async fn start_workflow<T: serde::Serialize>(&self, name: &str, input: T) -> Result<Uuid> {
self.workflow_dispatch.start(name, input).await
}
}
pub struct TestActionContextBuilder {
user_id: Option<Uuid>,
roles: Vec<String>,
claims: HashMap<String, serde_json::Value>,
pool: Option<PgPool>,
http: MockHttp,
job_dispatch: Arc<MockJobDispatch>,
workflow_dispatch: Arc<MockWorkflowDispatch>,
}
impl Default for TestActionContextBuilder {
fn default() -> Self {
Self {
user_id: None,
roles: Vec::new(),
claims: HashMap::new(),
pool: None,
http: MockHttp::new(),
job_dispatch: Arc::new(MockJobDispatch::new()),
workflow_dispatch: Arc::new(MockWorkflowDispatch::new()),
}
}
}
impl TestActionContextBuilder {
pub fn as_user(mut self, id: Uuid) -> Self {
self.user_id = Some(id);
self
}
pub fn with_role(mut self, role: impl Into<String>) -> Self {
self.roles.push(role.into());
self
}
pub fn with_roles(mut self, roles: Vec<String>) -> Self {
self.roles.extend(roles);
self
}
pub fn with_claim(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
self.claims.insert(key.into(), value);
self
}
pub fn with_pool(mut self, pool: PgPool) -> Self {
self.pool = Some(pool);
self
}
pub fn mock_http<F>(self, pattern: &str, handler: F) -> Self
where
F: Fn(&MockRequest) -> MockResponse + Send + Sync + 'static,
{
self.http.add_mock_sync(pattern, handler);
self
}
pub fn mock_http_json<T: serde::Serialize>(self, pattern: &str, response: T) -> Self {
let json = serde_json::to_value(response).unwrap_or(serde_json::Value::Null);
self.mock_http(pattern, move |_| MockResponse::json(json.clone()))
}
pub fn build(self) -> TestActionContext {
let auth = if let Some(user_id) = self.user_id {
AuthContext::authenticated(user_id, self.roles, self.claims)
} else {
AuthContext::unauthenticated()
};
TestActionContext {
auth,
request: RequestMetadata::default(),
pool: self.pool,
http: Arc::new(self.http),
job_dispatch: self.job_dispatch,
workflow_dispatch: self.workflow_dispatch,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_minimal_context() {
let ctx = TestActionContext::minimal();
assert!(!ctx.auth.is_authenticated());
}
#[test]
fn test_authenticated_context() {
let user_id = Uuid::new_v4();
let ctx = TestActionContext::authenticated(user_id);
assert!(ctx.auth.is_authenticated());
assert_eq!(ctx.require_user_id().unwrap(), user_id);
}
}