mailtrap 0.3.1

An unofficial library for interacting with the Mailtrap API
Documentation
#[cfg(test)]
use crate::accounts::*;
use serde_json::json;
use wiremock::{
    Mock, MockServer, ResponseTemplate,
    matchers::{header, method, path},
};

#[test]
fn account_access_level_new() {
    let account_access_level = AccountAccessLevel::new(1000).unwrap();
    assert_eq!(account_access_level, AccountAccessLevel::AccountOwner);
}

#[test]
fn account_access_level_to_int() {
    let account_access_level = AccountAccessLevel::AccountOwner;
    assert_eq!(account_access_level.to_int(), 1000);
}

#[test]
fn account_access_level_from_int() {
    let account_access_level = AccountAccessLevel::from_int(1000).unwrap();
    assert_eq!(account_access_level, AccountAccessLevel::AccountOwner);
}

#[test]
fn account_access_level_to_string() {
    let account_access_level = AccountAccessLevel::AccountOwner;
    assert_eq!(account_access_level.to_string(), "account_owner");
}

#[test]
fn account_access_level_from_string() {
    let account_access_level = AccountAccessLevel::from_string("1000".to_string()).unwrap();
    assert_eq!(account_access_level, AccountAccessLevel::AccountOwner);
}

#[test]
fn account_access_level_from_string_invalid() {
    let account_access_level = AccountAccessLevel::from_string("invalid".to_string());
    assert!(account_access_level.is_err());
}

#[test]
fn account_access_level_from_string_with_invalid_access_level() {
    let account_access_level = AccountAccessLevel::from_string("1001".to_string());
    assert!(account_access_level.is_err());
}

#[test]
fn account_access_level_from_str() {
    let account_access_level = AccountAccessLevel::from_str("1000").unwrap();
    assert_eq!(account_access_level, AccountAccessLevel::AccountOwner);
}

#[test]
fn account_access_level_from_str_with_invalid() {
    let account_access_level = AccountAccessLevel::from_str("invalid");
    assert!(account_access_level.is_err());
}

#[test]
fn account_access_level_from_str_with_invalid_access_level() {
    let account_access_level = AccountAccessLevel::from_str("1001");
    assert!(account_access_level.is_err());
}

#[test]
fn account_access_level_serialize() {
    let account_access_level = AccountAccessLevel::AccountOwner;
    let serialized = serde_json::to_string(&account_access_level).unwrap();
    assert_eq!(serialized, "1000");
}

#[test]
fn account_access_level_deserialize() {
    let account_access_level = 1000;
    let deserialized: AccountAccessLevel =
        serde_json::from_value(json!(account_access_level)).unwrap();
    assert_eq!(deserialized, AccountAccessLevel::AccountOwner);
}

#[tokio::test]
async fn accounts_list_fails_with_no_api_key_or_bearer_token() {
    let accounts = Accounts::new();
    let result = accounts.list(None, None, None).await;
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err().to_string(),
        "API key or bearer token is required"
    );
}

#[tokio::test]
async fn accounts_list_fails_with_empty_api_key() {
    let accounts = Accounts::new();
    let result = accounts.list(None, Some(""), None).await;
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "API key is empty");
}

#[tokio::test]
async fn accounts_list_fails_with_empty_bearer_token() {
    let accounts = Accounts::new();
    let result = accounts.list(None, None, Some("")).await;
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "Bearer token is empty");
}

#[tokio::test]
async fn accounts_list_fails_with_empty_url() {
    let accounts = Accounts::new();
    let result = accounts.list(Some(""), Some("1234"), None).await;
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "URL is empty");
}

#[tokio::test]
async fn accounts_list_fails_with_empty_bad_url() {
    let accounts = Accounts::new();
    let result = accounts.list(Some("invalid"), Some("1234"), None).await;
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err().to_string(),
        "Failed to parse URL: relative URL without a base"
    );
}

#[tokio::test]
async fn accounts_list_succeeds_with_200() {
    let mock_server = MockServer::start().await;
    let url = mock_server.uri();
    let accounts = Accounts::new();
    let response_body = json!(vec![Account {
        id: 1,
        name: "test".to_string(),
        access_levels: vec![AccountAccessLevel::AccountOwner],
    }])
    .to_string();
    let response_template =
        ResponseTemplate::new(200).set_body_bytes(response_body.as_bytes().to_vec());
    Mock::given(method("GET"))
        .and(path("/api/accounts"))
        .and(header("Api-Token", "1234"))
        .respond_with(response_template)
        .expect(1)
        .mount(&mock_server)
        .await;
    let result = accounts.list(Some(url.as_str()), Some("1234"), None).await;
    assert!(result.is_ok());
    let records = result.unwrap();
    assert_eq!(records.len(), 1);
    assert_eq!(records[0].id, 1);
    assert_eq!(records[0].name, "test");
    assert_eq!(
        records[0].access_levels,
        vec![AccountAccessLevel::AccountOwner]
    );
}

#[tokio::test]
async fn accounts_list_fails_with_401() {
    let mock_server = MockServer::start().await;
    let url = mock_server.uri();
    let accounts = Accounts::new();
    let response_body = AccountErrorResponse {
        error: "Unauthorized".to_string(),
    };
    let response_template = ResponseTemplate::new(401).set_body_json(response_body);
    Mock::given(method("GET"))
        .and(path("/api/accounts"))
        .and(header("Api-Token", "1234"))
        .respond_with(response_template)
        .expect(1)
        .mount(&mock_server)
        .await;
    let result = accounts.list(Some(url.as_str()), Some("1234"), None).await;
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err().to_string(),
        "Failed to list accounts: Unauthorized"
    );
}