mail_test_account/
lib.rs

1//! A library for creating/storing/loading ethereal.mail accounts.
2//!
3//! **Disclaimer: This source code/library/crate is unrelated to the original ethereal.mail service**, through it
4//! calls their api.
5//!
6//!
7
8use log::{warn, debug};
9use failure::Error;
10
11const APP_NAME: &str = "mail-test-account";
12const DEFAULT_TAG: &str = "test_account";
13
14mod types;
15mod new_account;
16mod load_store_account;
17
18
19pub use crate::{
20    load_store_account::*,
21    types::*,
22    new_account::*,
23};
24
25
26/// Get the "test_account" tagged test account, if there is non create one and store it.
27///
28/// Take a look at [`test_account_info_with_tag()`] for more details about where the
29/// account info is stored.
30pub fn test_account_info() -> Result<AccountAndServiceInfo, Error> {
31    test_account_info_with_tag(DEFAULT_TAG)
32}
33
34/// Get the test account for the given tag, if there is non create one and store it.
35///
36/// This uses the XDG directory spec for storing the account (as json). By default
37/// it will store the account in `~/.config/mail-test-account/<tag>.json`.
38///
39/// If no account is available for given tag it will create a `ethereal.mail` account
40/// and store it. The created accounts have random username/mail-address and password.
41/// No mail send to them get ever delivered, or semantically validated. So while you can
42/// use it for testing, it will _not_ fail if you e.g. switch up from and to address. But
43/// you can use IMAP/POP3 to pull mails from there and validate them.
44pub fn test_account_info_with_tag(tag: &str) -> Result<AccountAndServiceInfo, Error> {
45    if let Some(info) = load_account_info(tag)? {
46        Ok(info)
47    } else {
48        let info = create_new_ethereal_account()?;
49
50        let res = store_account_info(tag, &info);
51        if let Err(err) = res {
52            warn!("could not store new test account: {err}\ndbg: {err:?}", err=err);
53            debug!("non-stored ethereal.mail credentials: {:?}", &info.account);
54        }
55
56        Ok(info)
57    }
58}