emailit 2.0.3

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

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

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

    Mock::given(method("POST"))
        .and(path("/v2/api-keys"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "ak_123",
            "object": "api_key",
            "name": "Production Key",
            "scope": "full",
            "key": "secret_abc123"
        })))
        .mount(&server)
        .await;

    let result = client
        .api_keys
        .create(CreateApiKeyParams::new("Production Key").with_scope("full"))
        .await
        .unwrap();

    assert_eq!(result.id.unwrap(), "ak_123");
    assert_eq!(result.key.unwrap(), "secret_abc123");
}

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

    Mock::given(method("GET"))
        .and(path("/v2/api-keys/ak_123"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "ak_123",
            "object": "api_key",
            "name": "Production Key"
        })))
        .mount(&server)
        .await;

    let result = client.api_keys.get("ak_123").await.unwrap();
    assert_eq!(result.name.unwrap(), "Production Key");
}

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

    Mock::given(method("GET"))
        .and(path("/v2/api-keys"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [
                {"id": "ak_1", "object": "api_key"},
                {"id": "ak_2", "object": "api_key"}
            ]
        })))
        .mount(&server)
        .await;

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

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

    Mock::given(method("POST"))
        .and(path("/v2/api-keys/ak_123"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "ak_123",
            "object": "api_key",
            "name": "Renamed Key"
        })))
        .mount(&server)
        .await;

    let result = client
        .api_keys
        .update(
            "ak_123",
            UpdateApiKeyParams {
                name: Some("Renamed Key".into()),
                scope: None,
                sending_domain_id: None,
            },
        )
        .await
        .unwrap();

    assert_eq!(result.name.unwrap(), "Renamed Key");
}

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

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

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

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

    Mock::given(method("GET"))
        .and(path("/v2/api-keys/ak_123"))
        .respond_with(ResponseTemplate::new(401).set_body_json(json!({
            "error": "Unauthorized"
        })))
        .mount(&server)
        .await;

    let err = client.api_keys.get("ak_123").await.unwrap_err();
    assert!(err.is_authentication());
}