mod common;
use emailit::types::{CreateWebhookParams, UpdateWebhookParams};
use serde_json::json;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_create_webhook() {
let (client, server) = common::setup().await;
Mock::given(method("POST"))
.and(path("/v2/webhooks"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "wh_123",
"object": "webhook",
"name": "My Webhook",
"url": "https://example.com/hook",
"all_events": true,
"enabled": true
})))
.mount(&server)
.await;
let result = client
.webhooks
.create(
CreateWebhookParams::new("My Webhook", "https://example.com/hook")
.with_all_events(true)
.with_enabled(true),
)
.await
.unwrap();
assert_eq!(result.id.unwrap(), "wh_123");
assert_eq!(result.all_events, Some(true));
}
#[tokio::test]
async fn test_get_webhook() {
let (client, server) = common::setup().await;
Mock::given(method("GET"))
.and(path("/v2/webhooks/wh_123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "wh_123",
"object": "webhook",
"name": "My Webhook"
})))
.mount(&server)
.await;
let result = client.webhooks.get("wh_123").await.unwrap();
assert_eq!(result.name.unwrap(), "My Webhook");
}
#[tokio::test]
async fn test_update_webhook() {
let (client, server) = common::setup().await;
Mock::given(method("POST"))
.and(path("/v2/webhooks/wh_123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "wh_123",
"object": "webhook",
"enabled": false
})))
.mount(&server)
.await;
let result = client
.webhooks
.update(
"wh_123",
UpdateWebhookParams {
name: None,
url: None,
all_events: None,
enabled: Some(false),
events: None,
},
)
.await
.unwrap();
assert_eq!(result.enabled, Some(false));
}
#[tokio::test]
async fn test_list_webhooks() {
let (client, server) = common::setup().await;
Mock::given(method("GET"))
.and(path("/v2/webhooks"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": [
{"id": "wh_1", "object": "webhook"},
{"id": "wh_2", "object": "webhook"}
]
})))
.mount(&server)
.await;
let result = client.webhooks.list(None).await.unwrap();
assert_eq!(result.data.len(), 2);
}
#[tokio::test]
async fn test_delete_webhook() {
let (client, server) = common::setup().await;
Mock::given(method("DELETE"))
.and(path("/v2/webhooks/wh_123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": "wh_123",
"object": "webhook",
"deleted": true
})))
.mount(&server)
.await;
let result = client.webhooks.delete("wh_123").await.unwrap();
assert_eq!(result.deleted, Some(true));
}