emailit 2.0.3

The official Rust SDK for the Emailit Email API
Documentation
mod common;

use emailit::types::{CreateContactParams, UpdateContactParams};
use serde_json::json;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};

#[tokio::test]
async fn test_create_contact() {
    let (client, server) = common::setup().await;

    Mock::given(method("POST"))
        .and(path("/v2/contacts"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "con_123",
            "object": "contact",
            "email": "user@example.com",
            "first_name": "John",
            "last_name": "Doe"
        })))
        .mount(&server)
        .await;

    let result = client
        .contacts
        .create(
            CreateContactParams::new("user@example.com")
                .with_first_name("John")
                .with_last_name("Doe"),
        )
        .await
        .unwrap();

    assert_eq!(result.id.unwrap(), "con_123");
    assert_eq!(result.email.unwrap(), "user@example.com");
}

#[tokio::test]
async fn test_get_contact() {
    let (client, server) = common::setup().await;

    Mock::given(method("GET"))
        .and(path("/v2/contacts/con_123"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "con_123",
            "object": "contact",
            "email": "user@example.com"
        })))
        .mount(&server)
        .await;

    let result = client.contacts.get("con_123").await.unwrap();
    assert_eq!(result.email.unwrap(), "user@example.com");
}

#[tokio::test]
async fn test_update_contact() {
    let (client, server) = common::setup().await;

    Mock::given(method("POST"))
        .and(path("/v2/contacts/con_123"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "con_123",
            "object": "contact",
            "first_name": "Jane"
        })))
        .mount(&server)
        .await;

    let result = client
        .contacts
        .update(
            "con_123",
            UpdateContactParams {
                email: None,
                first_name: Some("Jane".into()),
                last_name: None,
                custom_fields: None,
                unsubscribed: None,
            },
        )
        .await
        .unwrap();

    assert_eq!(result.first_name.unwrap(), "Jane");
}

#[tokio::test]
async fn test_list_contacts() {
    let (client, server) = common::setup().await;

    Mock::given(method("GET"))
        .and(path("/v2/contacts"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [
                {"id": "con_1", "object": "contact"},
                {"id": "con_2", "object": "contact"}
            ]
        })))
        .mount(&server)
        .await;

    let result = client.contacts.list(None).await.unwrap();
    assert_eq!(result.data.len(), 2);
}

#[tokio::test]
async fn test_delete_contact() {
    let (client, server) = common::setup().await;

    Mock::given(method("DELETE"))
        .and(path("/v2/contacts/con_123"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "con_123",
            "object": "contact",
            "deleted": true
        })))
        .mount(&server)
        .await;

    let result = client.contacts.delete("con_123").await.unwrap();
    assert_eq!(result.deleted, Some(true));
}