mod common;
use emailit::types::{CreateTemplateParams, UpdateTemplateParams};
use serde_json::json;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_create_template() {
let (client, server) = common::setup().await;
Mock::given(method("POST"))
.and(path("/v2/templates"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "tem_123",
"object": "template",
"name": "Welcome",
"subject": "Welcome!"
})))
.mount(&server)
.await;
let result = client
.templates
.create(
CreateTemplateParams::new("Welcome")
.with_subject("Welcome!")
.with_html("<h1>Hi {{name}}</h1>"),
)
.await
.unwrap();
assert_eq!(result.id.unwrap(), "tem_123");
assert_eq!(result.name.unwrap(), "Welcome");
}
#[tokio::test]
async fn test_get_template() {
let (client, server) = common::setup().await;
Mock::given(method("GET"))
.and(path("/v2/templates/tem_123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "tem_123",
"object": "template",
"name": "Welcome"
})))
.mount(&server)
.await;
let result = client.templates.get("tem_123").await.unwrap();
assert_eq!(result.id.unwrap(), "tem_123");
}
#[tokio::test]
async fn test_update_template() {
let (client, server) = common::setup().await;
Mock::given(method("POST"))
.and(path("/v2/templates/tem_123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "tem_123",
"object": "template",
"subject": "New Subject"
})))
.mount(&server)
.await;
let result = client
.templates
.update(
"tem_123",
UpdateTemplateParams {
name: None,
subject: Some("New Subject".into()),
from: None,
reply_to: None,
html: None,
text: None,
},
)
.await
.unwrap();
assert_eq!(result.subject.unwrap(), "New Subject");
}
#[tokio::test]
async fn test_list_templates() {
let (client, server) = common::setup().await;
Mock::given(method("GET"))
.and(path("/v2/templates"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": [
{"id": "tem_1", "object": "template"},
{"id": "tem_2", "object": "template"}
]
})))
.mount(&server)
.await;
let result = client.templates.list(None).await.unwrap();
assert_eq!(result.data.len(), 2);
}
#[tokio::test]
async fn test_delete_template() {
let (client, server) = common::setup().await;
Mock::given(method("DELETE"))
.and(path("/v2/templates/tem_123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"message": "Template deleted"
})))
.mount(&server)
.await;
let result = client.templates.delete("tem_123").await.unwrap();
assert_eq!(result["message"], "Template deleted");
}
#[tokio::test]
async fn test_publish_template() {
let (client, server) = common::setup().await;
Mock::given(method("POST"))
.and(path("/v2/templates/tem_123/publish"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "tem_123",
"object": "template",
"published_at": "2026-01-01T00:00:00Z"
})))
.mount(&server)
.await;
let result = client.templates.publish("tem_123").await.unwrap();
assert_eq!(result.id.unwrap(), "tem_123");
assert!(result.published_at.is_some());
}