#[cfg(test)]
use crate::email::emails::{
Address, Attachment, BatchEmail, BatchEmailInnerResponse, BatchEmailRequest,
BatchEmailResponse, ContentType, Disposition, Email, EmailResponse, Header,
};
use wiremock::{
Mock, MockServer, ResponseTemplate,
matchers::{header, method, path},
};
#[test]
fn address_new() {
let address = Address::new();
assert_eq!(address.name, String::new());
assert_eq!(address.email, String::new());
}
#[test]
fn address_new_with_empty_string() {
let address = Address::from_string("".to_string()).unwrap_err();
assert_eq!(address.to_string(), "Empty address");
}
#[test]
fn address_from_string_with_quoted_name() {
let address = Address::from_string("\"test\" <test@example.com>".to_string()).unwrap();
assert_eq!(address.name, "test");
assert_eq!(address.email, "test@example.com");
}
#[test]
fn address_from_string_without_quoted_name() {
let address = Address::from_string("test <test@example.com>".to_string()).unwrap();
assert_eq!(address.name, "test");
assert_eq!(address.email, "test@example.com");
}
#[test]
fn address_from_str_with_only_email() {
let address = Address::from_str("test@example.com").unwrap();
assert_eq!(address.name, String::new());
assert_eq!(address.email, "test@example.com");
}
#[test]
fn address_from_str_with_wrapped_email() {
let address = Address::from_str("<test@example.com>").unwrap();
assert_eq!(address.name, String::new());
assert_eq!(address.email, "test@example.com");
}
#[test]
fn address_to_string_with_name() {
let address = Address::new().name("test").email("test@example.com");
assert_eq!(address.to_string(), "\"test\" <test@example.com>");
}
#[test]
fn address_to_string_without_name() {
let address = Address::new().email("test@example.com");
assert_eq!(address.to_string(), "test@example.com");
}
#[test]
fn content_type_new() {
let content_type = ContentType::new("text/plain").unwrap();
assert_eq!(content_type.to_string(), "text/plain");
}
#[test]
fn content_type_new_with_invalid_content_type() {
let result = ContentType::new("invalid");
assert!(result.is_err());
}
#[test]
fn content_type_from_string() {
let content_type = ContentType::from_string("text/plain".to_string()).unwrap();
assert_eq!(content_type.to_string(), "text/plain");
}
#[test]
fn content_type_from_str() {
let content_type = ContentType::from_str("text/plain").unwrap();
assert_eq!(content_type.to_string(), "text/plain");
}
#[test]
fn content_type_to_string() {
let content_type = ContentType::Plain;
assert_eq!(content_type.to_string(), "text/plain");
}
#[test]
fn disposition_new() {
let disposition = Disposition::new("attachment").unwrap();
assert_eq!(disposition.to_string(), "attachment");
}
#[test]
fn disposition_new_with_invalid_disposition() {
let result = Disposition::new("invalid");
assert!(result.is_err());
}
#[test]
fn disposition_from_string() {
let disposition = Disposition::from_string("attachment".to_string()).unwrap();
assert_eq!(disposition.to_string(), "attachment");
}
#[test]
fn disposition_from_str() {
let disposition = Disposition::from_str("attachment").unwrap();
assert_eq!(disposition.to_string(), "attachment");
}
#[test]
fn attachment_new() {
let attachment = Attachment::new();
assert_eq!(attachment.content, Vec::<u8>::new());
assert_eq!(attachment.content_type.to_string(), "text/plain");
assert_eq!(attachment.filename, String::new());
assert_eq!(attachment.disposition.to_string(), "attachment");
assert_eq!(attachment.content_id, None);
}
#[test]
fn attachment_set_content() {
let attachment = Attachment::new().content(vec![1, 2, 3]);
assert_eq!(attachment.content, vec![1, 2, 3]);
}
#[test]
fn attachment_set_content_type() {
let attachment = Attachment::new().content_type(ContentType::Html);
assert_eq!(attachment.content_type.to_string(), "text/html");
}
#[test]
fn attachment_set_filename() {
let attachment = Attachment::new().filename("test.txt");
assert_eq!(attachment.filename, "test.txt");
}
#[test]
fn attachment_set_disposition() {
let attachment = Attachment::new().disposition(Disposition::Inline);
assert_eq!(attachment.disposition.to_string(), "inline");
}
#[test]
fn attachment_set_content_id() {
let attachment = Attachment::new().content_id(Some(vec![1, 2, 3]));
assert_eq!(attachment.content_id, Some(vec![1, 2, 3]));
}
#[test]
fn header_new() {
let header = Header::new();
assert_eq!(header.key, String::new());
assert_eq!(header.value, String::new());
}
#[test]
fn header_from_string() {
let header = Header::from_string("test: test".to_string()).unwrap();
assert_eq!(header.key, "test");
assert_eq!(header.value, "test");
}
#[test]
fn header_from_str() {
let header = Header::from_str("test: test").unwrap();
assert_eq!(header.key, "test");
assert_eq!(header.value, "test");
}
#[test]
fn header_to_string() {
let header = Header::new().key("test").value("test");
assert_eq!(header.to_string(), "test: test");
}
#[test]
fn header_from_string_with_invalid_header() {
let result = Header::from_string("test".to_string());
assert!(result.is_err());
}
#[test]
fn header_from_str_with_invalid_header() {
let result = Header::from_str("test");
assert!(result.is_err());
}
#[test]
fn header_to_string_with_key_and_value() {
let header = Header::new().key("test").value("test");
assert_eq!(header.to_string(), "test: test");
}
#[test]
fn header_to_string_without_key() {
let header = Header::new().value("test");
assert_eq!(header.to_string(), ": test");
}
#[test]
fn header_from_string_with_only_value() {
let result = Header::from_string("test".to_string());
assert!(result.is_err());
}
#[test]
fn email_new_with_empty_email() {
let email = Email::new();
assert_eq!(email.from.to_string(), "");
assert_eq!(email.to.to_string(), "");
assert_eq!(email.cc.len(), 0);
assert_eq!(email.bcc.len(), 0);
assert_eq!(email.reply_to.to_string(), "");
assert_eq!(email.attachments.len(), 0);
assert_eq!(email.headers.len(), 0);
assert_eq!(email.subject, String::new());
assert_eq!(email.html, String::new());
let email = Email::new();
assert_eq!(email.from.to_string(), "");
assert_eq!(email.to.to_string(), "");
assert_eq!(email.cc.len(), 0);
assert_eq!(email.bcc.len(), 0);
assert_eq!(email.reply_to.to_string(), "");
assert_eq!(email.attachments.len(), 0);
assert_eq!(email.headers.len(), 0);
assert_eq!(email.subject, String::new());
assert_eq!(email.html, String::new());
assert_eq!(email.text, String::new());
assert_eq!(email.category, String::new());
}
#[test]
fn email_set_from() {
let email = Email::new().from("test@example.com");
assert_eq!(email.from.to_string(), "test@example.com");
}
#[test]
fn email_set_to() {
let email = Email::new().to("test@example.com");
assert_eq!(email.to.to_string(), "test@example.com");
}
#[test]
fn email_set_ccs() {
let email = Email::new().ccs(vec!["test@example.com"]);
assert_eq!(email.cc.len(), 1);
assert_eq!(email.cc[0].to_string(), "test@example.com");
}
#[test]
fn email_set_cc() {
let email = Email::new().cc("test@example.com");
assert_eq!(email.cc.len(), 1);
assert_eq!(email.cc[0].to_string(), "test@example.com");
}
#[test]
fn email_set_bccs() {
let email = Email::new().bccs(vec!["test@example.com"]);
assert_eq!(email.bcc.len(), 1);
assert_eq!(email.bcc[0].to_string(), "test@example.com");
}
#[test]
fn email_set_bcc() {
let email = Email::new().bcc("test@example.com");
assert_eq!(email.bcc.len(), 1);
assert_eq!(email.bcc[0].to_string(), "test@example.com");
}
#[test]
fn email_set_reply_to() {
let email = Email::new().reply_to("test@example.com");
assert_eq!(email.reply_to.to_string(), "test@example.com");
}
#[test]
fn email_set_attachments() {
let email = Email::new().attachments(vec![Attachment::new()]);
assert_eq!(email.attachments.len(), 1);
assert_eq!(email.attachments[0].content, Vec::<u8>::new());
assert_eq!(email.attachments[0].content_type.to_string(), "text/plain");
assert_eq!(email.attachments[0].filename, String::new());
assert_eq!(email.attachments[0].disposition.to_string(), "attachment");
assert_eq!(email.attachments[0].content_id, None);
}
#[test]
fn email_set_headers() {
let email = Email::new().headers(vec![Header::new().key("test").value("test")]);
assert_eq!(email.headers.len(), 1);
assert_eq!(email.headers.get("test").unwrap().value, "test");
}
#[test]
fn email_set_header() {
let email = Email::new().header("test", "test");
assert_eq!(email.headers.len(), 1);
let header = email.headers.get("test");
assert!(header.is_some());
let header = header.unwrap();
assert_eq!(header.to_string(), "test: test");
assert_eq!(header.key, "test");
assert_eq!(header.value, "test");
}
#[test]
fn email_set_text() {
let email = Email::new().text("test text");
assert_eq!(email.text, "test text");
}
#[test]
fn email_set_category() {
let email = Email::new().category("test category");
assert_eq!(email.category, "test category");
}
#[tokio::test]
async fn email_send_fails_with_empty_url() {
let email = Email::new();
let result = email.send(Some(""), None, None).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "URL is empty");
}
#[tokio::test]
async fn email_send_fails_with_empty_to_email() {
let url = "https://example.com";
let email = Email::new();
let result = email.send(Some(url), Some("1234"), None).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "To email is empty");
}
#[tokio::test]
async fn email_send_fails_with_empty_from_email() {
let url = "https://example.com";
let email = Email::new().to("test@example.com");
let result = email.send(Some(url), Some("1234"), None).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "From email is empty");
}
#[tokio::test]
async fn email_send_fails_with_empty_subject() {
let url = "https://example.com";
let email = Email::new().from("test@example.com").to("test@example.com");
let result = email.send(Some(url), Some("1234"), None).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Subject is empty");
}
#[tokio::test]
async fn email_send_fails_with_empty_text_and_html() {
let url = "https://example.com";
let email = Email::new()
.from("test@example.com")
.to("test@example.com")
.subject("test subject");
let result = email.send(Some(url), Some("1234"), None).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Text or HTML is empty");
}
#[tokio::test]
async fn email_send_fails_with_invalid_url() {
let url = "invalid";
let email = Email::new();
let result = email.send(Some(url), None, None).await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Invalid URL: relative URL without a base"
);
}
#[tokio::test]
async fn email_send_fails_with_no_api_key_or_bearer_token() {
let url = "https://example.com";
let email = Email::new()
.from("test@example.com")
.to("test@example.com")
.subject("test subject")
.text("test text");
let result = email.send(Some(url), 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 email_send_fails_with_empty_api_key() {
let url = "https://example.com";
let email = Email::new()
.from("test@example.com")
.to("test@example.com")
.subject("test subject")
.text("test text");
let result = email.send(Some(url), Some(""), None).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "API key is empty");
}
#[tokio::test]
async fn email_send_fails_with_empty_bearer_token() {
let url = "https://example.com";
let email = Email::new()
.from("test@example.com")
.to("test@example.com")
.subject("test subject")
.text("test text");
let result = email.send(Some(url), None, Some("")).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Bearer token is empty");
}
#[cfg(test)]
fn setup_email() -> Email {
Email::new()
.from("test@example.com")
.to("test@example.com")
.subject("test subject")
.text("test text")
}
#[tokio::test]
async fn email_send_succeeds_with_200() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let email = setup_email();
let response_body = EmailResponse {
success: true,
errors: Vec::new(),
};
let response_template = ResponseTemplate::new(200).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/send"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = email.send(Some(url.as_str()), Some("1234"), None).await;
assert_eq!(result.is_ok(), true);
assert_eq!(result.unwrap(), true);
}
#[tokio::test]
async fn email_send_fails_with_400() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let email = setup_email();
let error_message = "Bad request. Fix errors listed in response before retrying.";
let response_body = EmailResponse {
success: false,
errors: vec![error_message.to_string()],
};
let response_template = ResponseTemplate::new(400).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/send"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = email.send(Some(url.as_str()), Some("1234"), None).await;
assert_eq!(result.is_err(), true);
assert_eq!(
result.unwrap_err().to_string(),
format!("Failed to send email: {}", error_message)
);
}
#[tokio::test]
async fn email_send_fails_with_401() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let email = setup_email();
let error_message = "Unauthorized. Make sure you are sending correct credentials with the request before retrying.";
let response_body = EmailResponse {
success: false,
errors: vec![error_message.to_string()],
};
let response_template = ResponseTemplate::new(401).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/send"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = email.send(Some(url.as_str()), Some("1234"), None).await;
assert_eq!(result.is_err(), true);
assert_eq!(
result.unwrap_err().to_string(),
format!("Failed to send email: {}", error_message)
);
}
#[tokio::test]
async fn email_send_fails_with_403() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let email = setup_email();
let error_message = "Forbidden. Make sure domain verification process is completed.";
let response_body = EmailResponse {
success: false,
errors: vec![error_message.to_string()],
};
let response_template = ResponseTemplate::new(403).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/send"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = email.send(Some(url.as_str()), Some("1234"), None).await;
assert_eq!(result.is_err(), true);
assert_eq!(
result.unwrap_err().to_string(),
format!("Failed to send email: {}", error_message)
);
}
#[tokio::test]
async fn email_send_fails_with_500() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let email = setup_email();
let error_message = "Internal error. Mail was not delivered. Retry later or contact support.";
let response_body = EmailResponse {
success: false,
errors: vec![error_message.to_string()],
};
let response_template = ResponseTemplate::new(500).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/send"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = email.send(Some(url.as_str()), Some("1234"), None).await;
assert_eq!(result.is_err(), true);
assert_eq!(
result.unwrap_err().to_string(),
format!("Failed to send email: {}", error_message)
);
}
#[test]
fn batch_email_new() {
let batch_email = BatchEmail::new();
assert_eq!(batch_email.from.to_string(), "");
assert_eq!(batch_email.reply_to.to_string(), "");
assert_eq!(batch_email.subject, String::new());
assert_eq!(batch_email.html, String::new());
assert_eq!(batch_email.text, String::new());
assert_eq!(batch_email.attachments.len(), 0);
assert_eq!(batch_email.headers.len(), 0);
assert_eq!(batch_email.category, String::new());
assert_eq!(batch_email.custom_variables.len(), 0);
assert_eq!(batch_email.template_uuid, String::new());
assert_eq!(batch_email.template_variables.len(), 0);
assert_eq!(batch_email.requests.len(), 0);
}
#[test]
fn batch_email_set_from() {
let batch_email = BatchEmail::new().from("test@example.com");
assert_eq!(batch_email.from.to_string(), "test@example.com");
}
#[test]
fn batch_email_set_reply_to() {
let batch_email = BatchEmail::new().reply_to("test@example.com");
assert_eq!(batch_email.reply_to.to_string(), "test@example.com");
}
#[test]
fn batch_email_set_subject() {
let batch_email = BatchEmail::new().subject("test subject");
assert_eq!(batch_email.subject, "test subject");
}
#[test]
fn batch_email_set_html() {
let batch_email = BatchEmail::new().html("test html");
assert_eq!(batch_email.html, "test html");
}
#[test]
fn batch_email_set_text() {
let batch_email = BatchEmail::new().text("test text");
assert_eq!(batch_email.text, "test text");
}
#[test]
fn batch_email_set_attachments() {
let batch_email = BatchEmail::new().attachments(vec![Attachment::new()]);
assert_eq!(batch_email.attachments.len(), 1);
}
#[test]
fn batch_email_set_attachment() {
let batch_email = BatchEmail::new().attachment(Attachment::new());
assert_eq!(batch_email.attachments.len(), 1);
}
#[test]
fn batch_email_set_headers() {
let batch_email = BatchEmail::new().headers(vec![Header::new().key("test").value("test")]);
assert_eq!(batch_email.headers.len(), 1);
assert_eq!(batch_email.headers.get("test").unwrap().value, "test");
}
#[test]
fn batch_email_set_header() {
let batch_email = BatchEmail::new().header("test", "test");
assert_eq!(batch_email.headers.len(), 1);
}
#[test]
fn batch_email_set_custom_variables() {
let batch_email = BatchEmail::new().custom_variables(vec![("test", "test")]);
assert_eq!(batch_email.custom_variables.len(), 1);
}
#[test]
fn batch_email_set_custom_variable() {
let batch_email = BatchEmail::new().custom_variable("test", "test");
assert_eq!(batch_email.custom_variables.len(), 1);
}
#[test]
fn batch_email_set_template_uuid() {
let batch_email = BatchEmail::new().template_uuid("test");
assert_eq!(batch_email.template_uuid, "test");
}
#[test]
fn batch_email_set_template_variables() {
let batch_email = BatchEmail::new().template_variables(vec![("test", "test")]);
assert_eq!(batch_email.template_variables.len(), 1);
}
#[test]
fn batch_email_set_template_variable() {
let batch_email = BatchEmail::new().template_variable("test", "test");
assert_eq!(batch_email.template_variables.len(), 1);
}
#[test]
fn batch_email_set_requests() {
let batch_email = BatchEmail::new().requests(vec![BatchEmailRequest::new()]);
assert_eq!(batch_email.requests.len(), 1);
}
#[test]
fn batch_email_set_request() {
let batch_email = BatchEmail::new().request(BatchEmailRequest::new());
assert_eq!(batch_email.requests.len(), 1);
}
#[test]
fn batch_request_new() {
let batch_request = BatchEmailRequest::new();
assert_eq!(batch_request.from.to_string(), "");
assert_eq!(batch_request.to.to_string(), "");
assert_eq!(batch_request.cc.len(), 0);
assert_eq!(batch_request.bcc.len(), 0);
assert_eq!(batch_request.reply_to.to_string(), "");
assert_eq!(batch_request.subject, String::new());
}
#[test]
fn batch_request_set_from() {
let batch_request = BatchEmailRequest::new().from("test@example.com");
assert_eq!(batch_request.from.to_string(), "test@example.com");
}
#[test]
fn batch_request_set_to() {
let batch_request = BatchEmailRequest::new().to("test@example.com");
assert_eq!(batch_request.to.to_string(), "test@example.com");
}
#[test]
fn batch_request_set_ccs() {
let batch_request = BatchEmailRequest::new().ccs(vec!["test@example.com"]);
assert_eq!(batch_request.cc.len(), 1);
}
#[test]
fn batch_request_set_cc() {
let batch_request = BatchEmailRequest::new().cc("test@example.com");
assert_eq!(batch_request.cc.len(), 1);
}
#[test]
fn batch_request_set_bccs() {
let batch_request = BatchEmailRequest::new().bccs(vec!["test@example.com"]);
assert_eq!(batch_request.bcc.len(), 1);
}
#[test]
fn batch_request_set_bcc() {
let batch_request = BatchEmailRequest::new().bcc("test@example.com");
assert_eq!(batch_request.bcc.len(), 1);
}
#[test]
fn batch_request_set_reply_to() {
let batch_request = BatchEmailRequest::new().reply_to("test@example.com");
assert_eq!(batch_request.reply_to.to_string(), "test@example.com");
}
#[test]
fn batch_request_set_subject() {
let batch_request = BatchEmailRequest::new().subject("test subject");
assert_eq!(batch_request.subject, "test subject");
}
#[test]
fn batch_request_set_html() {
let batch_request = BatchEmailRequest::new().html("test html");
assert_eq!(batch_request.html, "test html");
}
#[test]
fn batch_request_set_text() {
let batch_request = BatchEmailRequest::new().text("test text");
assert_eq!(batch_request.text, "test text");
}
#[test]
fn batch_request_set_attachments() {
let batch_request = BatchEmailRequest::new().attachments(vec![Attachment::new()]);
assert_eq!(batch_request.attachments.len(), 1);
}
#[test]
fn batch_request_set_attachment() {
let batch_request = BatchEmailRequest::new().attachment(Attachment::new());
assert_eq!(batch_request.attachments.len(), 1);
}
#[test]
fn batch_request_set_headers() {
let batch_request =
BatchEmailRequest::new().headers(vec![Header::new().key("test").value("test")]);
assert_eq!(batch_request.headers.len(), 1);
assert_eq!(batch_request.headers.get("test").unwrap().value, "test");
}
#[test]
fn batch_request_set_header() {
let batch_request = BatchEmailRequest::new().header("test", "test");
assert_eq!(batch_request.headers.len(), 1);
}
#[test]
fn batch_request_set_category() {
let batch_request = BatchEmailRequest::new().category("test category");
assert_eq!(batch_request.category, "test category");
}
#[test]
fn batch_request_set_custom_variables() {
let batch_request = BatchEmailRequest::new().custom_variables(vec![("test", "test")]);
assert_eq!(batch_request.custom_variables.len(), 1);
}
#[test]
fn batch_request_set_custom_variable() {
let batch_request = BatchEmailRequest::new().custom_variable("test", "test");
assert_eq!(batch_request.custom_variables.len(), 1);
}
#[test]
fn batch_request_set_template_uuid() {
let batch_request = BatchEmailRequest::new().template_uuid("test");
assert_eq!(batch_request.template_uuid, "test");
}
#[test]
fn batch_request_set_template_variables() {
let batch_request = BatchEmailRequest::new().template_variables(vec![("test", "test")]);
assert_eq!(batch_request.template_variables.len(), 1);
}
#[test]
fn batch_request_set_template_variable() {
let batch_request = BatchEmailRequest::new().template_variable("test", "test");
assert_eq!(batch_request.template_variables.len(), 1);
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_url() {
let batch_email = BatchEmail::new();
let result = batch_email.send(Some(""), None, None).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "URL is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_no_token_and_bearer_token() {
let batch_email = BatchEmail::new();
let result = batch_email
.send(Some("https://example.com"), 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 batch_email_send_fails_with_empty_token() {
let batch_email = BatchEmail::new();
let result = batch_email
.send(Some("https://example.com"), Some(""), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "API key is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_bearer_token() {
let batch_email = BatchEmail::new();
let result = batch_email
.send(Some("https://example.com"), None, Some(""))
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Bearer token is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_from_email() {
let batch_email = BatchEmail::new();
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "From email is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_reply_to_email() {
let batch_email = BatchEmail::new().from("test@example.com");
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Reply-To email is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_subject() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com");
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Subject is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_text_and_html() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject");
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Text or HTML is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_category() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text");
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Category is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_template_uuid() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category");
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Template UUID is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_requests() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid");
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Requests are empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_request_from_email() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.to("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid"),
]);
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Request from email is empty"
);
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_request_to_email() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.html("test html")
.category("test category")
.template_uuid("test template uuid")
.template_variables(vec![("test", "test")]),
]);
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Request to email is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_request_reply_to_email() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.from("test@example.com")
.to("test@example.com")
.subject("test subject")
.text("test text")
.html("test html")
.category("test category")
.template_uuid("test template uuid")
.template_variables(vec![("test", "test")]),
]);
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Request reply-to email is empty"
);
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_request_subject() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.from("test@example.com")
.to("test@example.com")
.reply_to("test@example.com")
.text("test text")
.html("test html")
.category("test category")
.template_uuid("test template uuid")
.template_variables(vec![("test", "test")]),
]);
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Request subject is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_request_text_and_html() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.from("test@example.com")
.to("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.category("test category")
.template_uuid("test template uuid")
.template_variables(vec![("test", "test")]),
]);
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Request text or HTML is empty"
);
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_request_category() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.from("test@example.com")
.to("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.html("test html")
.template_uuid("test template uuid")
.template_variables(vec![("test", "test")]),
]);
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Request category is empty");
}
#[tokio::test]
async fn batch_email_send_fails_with_empty_request_template_uuid() {
let batch_email = BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.from("test@example.com")
.to("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.html("test html")
.category("test category")
.template_variables(vec![("test", "test")]),
]);
let result = batch_email
.send(Some("https://example.com"), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Request template UUID is empty"
);
}
fn setup_batch_email() -> BatchEmail {
BatchEmail::new()
.from("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.category("test category")
.template_uuid("test template uuid")
.requests(vec![
BatchEmailRequest::new()
.from("test@example.com")
.to("test@example.com")
.reply_to("test@example.com")
.subject("test subject")
.text("test text")
.html("test html")
.category("test category")
.template_uuid("test template uuid")
.template_variables(vec![("test", "test")]),
])
}
#[tokio::test]
async fn batch_email_send_succeeds_with_200() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let batch_email = setup_batch_email();
let response_body = BatchEmailResponse {
success: true,
responses: vec![BatchEmailInnerResponse {
success: true,
message_ids: vec!["test templae uuid".to_string()],
errors: Vec::new(),
}],
errors: Vec::new(),
};
let response_template = ResponseTemplate::new(200).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/batch"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = batch_email
.send(Some(url.as_str()), Some("1234"), None)
.await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[tokio::test]
async fn batch_email_send_fails_with_400() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let batch_email = setup_batch_email();
let response_body = BatchEmailResponse {
success: false,
responses: Vec::new(),
errors: vec!["test error".to_string()],
};
let response_template = ResponseTemplate::new(400).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/batch"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = batch_email
.send(Some(url.as_str()), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Failed to send batch email: test error"
);
}
#[tokio::test]
async fn batch_email_send_fails_with_401() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let batch_email = setup_batch_email();
let response_body = BatchEmailResponse {
success: false,
responses: Vec::new(),
errors: vec!["test error".to_string()],
};
let response_template = ResponseTemplate::new(401).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/batch"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = batch_email
.send(Some(url.as_str()), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Failed to send batch email: test error"
);
}
#[tokio::test]
async fn batch_email_send_fails_with_500() {
let mock_server = MockServer::start().await;
let url = mock_server.uri();
let batch_email = setup_batch_email();
let response_body = BatchEmailResponse {
success: false,
responses: Vec::new(),
errors: vec!["test error".to_string()],
};
let response_template = ResponseTemplate::new(500).set_body_json(response_body);
Mock::given(method("POST"))
.and(path("/api/batch"))
.and(header("Api-Token", "1234"))
.respond_with(response_template)
.expect(1)
.mount(&mock_server)
.await;
let result = batch_email
.send(Some(url.as_str()), Some("1234"), None)
.await;
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Failed to send batch email: test error"
);
}