Skip to content

Commit ef19feb

Browse files
committed
refactor: Move transport_tests to their own file
1 parent 528305e commit ef19feb

File tree

2 files changed

+279
-280
lines changed

2 files changed

+279
-280
lines changed

src/transport.rs

Lines changed: 1 addition & 280 deletions
Original file line numberDiff line numberDiff line change
@@ -793,283 +793,4 @@ pub(crate) async fn add_pseudo_transport(context: &Context, addr: &str) -> Resul
793793
}
794794

795795
#[cfg(test)]
796-
mod tests {
797-
use super::*;
798-
use crate::log::LogExt as _;
799-
use crate::provider::get_provider_by_id;
800-
use crate::test_utils::TestContext;
801-
use crate::tools::time;
802-
803-
#[test]
804-
fn test_configured_certificate_checks_display() {
805-
use std::string::ToString;
806-
807-
assert_eq!(
808-
"accept_invalid_certificates".to_string(),
809-
ConfiguredCertificateChecks::AcceptInvalidCertificates.to_string()
810-
);
811-
}
812-
813-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
814-
async fn test_save_load_login_param() -> Result<()> {
815-
let t = TestContext::new().await;
816-
817-
let param = ConfiguredLoginParam {
818-
addr: "alice@example.org".to_string(),
819-
imap: vec![ConfiguredServerLoginParam {
820-
connection: ConnectionCandidate {
821-
host: "imap.example.com".to_string(),
822-
port: 123,
823-
security: ConnectionSecurity::Starttls,
824-
},
825-
user: "alice".to_string(),
826-
}],
827-
imap_user: "".to_string(),
828-
imap_password: "foo".to_string(),
829-
smtp: vec![ConfiguredServerLoginParam {
830-
connection: ConnectionCandidate {
831-
host: "smtp.example.com".to_string(),
832-
port: 456,
833-
security: ConnectionSecurity::Tls,
834-
},
835-
user: "alice@example.org".to_string(),
836-
}],
837-
smtp_user: "".to_string(),
838-
smtp_password: "bar".to_string(),
839-
provider: None,
840-
certificate_checks: ConfiguredCertificateChecks::Strict,
841-
oauth2: false,
842-
};
843-
844-
param
845-
.clone()
846-
.save_to_transports_table(&t, &EnteredLoginParam::default(), time())
847-
.await?;
848-
let expected_param = r#"{"addr":"alice@example.org","imap":[{"connection":{"host":"imap.example.com","port":123,"security":"Starttls"},"user":"alice"}],"imap_user":"","imap_password":"foo","smtp":[{"connection":{"host":"smtp.example.com","port":456,"security":"Tls"},"user":"alice@example.org"}],"smtp_user":"","smtp_password":"bar","provider_id":null,"certificate_checks":"Strict","oauth2":false}"#;
849-
assert_eq!(
850-
t.sql
851-
.query_get_value::<String>("SELECT configured_param FROM transports", ())
852-
.await?
853-
.unwrap(),
854-
expected_param
855-
);
856-
assert_eq!(t.is_configured().await?, true);
857-
let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
858-
assert_eq!(param, loaded);
859-
860-
// Legacy ConfiguredImapCertificateChecks config is ignored
861-
t.set_config(Config::ConfiguredImapCertificateChecks, Some("999"))
862-
.await?;
863-
assert!(ConfiguredLoginParam::load(&t).await.is_ok());
864-
865-
// Test that we don't panic on unknown ConfiguredImapCertificateChecks values.
866-
let wrong_param = expected_param.replace("Strict", "Stricct");
867-
assert_ne!(expected_param, wrong_param);
868-
t.sql
869-
.execute("UPDATE transports SET configured_param=?", (wrong_param,))
870-
.await?;
871-
assert!(ConfiguredLoginParam::load(&t).await.is_err());
872-
873-
Ok(())
874-
}
875-
876-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
877-
async fn test_posteo_alias() -> Result<()> {
878-
let t = TestContext::new().await;
879-
880-
let user = "alice@posteo.de";
881-
882-
// Alice has old config with "alice@posteo.at" address
883-
// and "alice@posteo.de" username.
884-
t.set_config(Config::Configured, Some("1")).await?;
885-
t.set_config(Config::ConfiguredProvider, Some("posteo"))
886-
.await?;
887-
t.sql
888-
.set_raw_config(Config::ConfiguredAddr.as_ref(), Some("alice@posteo.at"))
889-
.await?;
890-
t.set_config(Config::ConfiguredMailServer, Some("posteo.de"))
891-
.await?;
892-
t.set_config(Config::ConfiguredMailPort, Some("993"))
893-
.await?;
894-
t.set_config(Config::ConfiguredMailSecurity, Some("1"))
895-
.await?; // TLS
896-
t.set_config(Config::ConfiguredMailUser, Some(user)).await?;
897-
t.set_config(Config::ConfiguredMailPw, Some("foobarbaz"))
898-
.await?;
899-
t.set_config(Config::ConfiguredImapCertificateChecks, Some("1"))
900-
.await?; // Strict
901-
t.set_config(Config::ConfiguredSendServer, Some("posteo.de"))
902-
.await?;
903-
t.set_config(Config::ConfiguredSendPort, Some("465"))
904-
.await?;
905-
t.set_config(Config::ConfiguredSendSecurity, Some("1"))
906-
.await?; // TLS
907-
t.set_config(Config::ConfiguredSendUser, Some(user)).await?;
908-
t.set_config(Config::ConfiguredSendPw, Some("foobarbaz"))
909-
.await?;
910-
t.set_config(Config::ConfiguredSmtpCertificateChecks, Some("1"))
911-
.await?; // Strict
912-
t.set_config(Config::ConfiguredServerFlags, Some("0"))
913-
.await?;
914-
915-
let param = ConfiguredLoginParam {
916-
addr: "alice@posteo.at".to_string(),
917-
imap: vec![
918-
ConfiguredServerLoginParam {
919-
connection: ConnectionCandidate {
920-
host: "posteo.de".to_string(),
921-
port: 993,
922-
security: ConnectionSecurity::Tls,
923-
},
924-
user: user.to_string(),
925-
},
926-
ConfiguredServerLoginParam {
927-
connection: ConnectionCandidate {
928-
host: "posteo.de".to_string(),
929-
port: 143,
930-
security: ConnectionSecurity::Starttls,
931-
},
932-
user: user.to_string(),
933-
},
934-
],
935-
imap_user: "alice@posteo.de".to_string(),
936-
imap_password: "foobarbaz".to_string(),
937-
smtp: vec![
938-
ConfiguredServerLoginParam {
939-
connection: ConnectionCandidate {
940-
host: "posteo.de".to_string(),
941-
port: 465,
942-
security: ConnectionSecurity::Tls,
943-
},
944-
user: user.to_string(),
945-
},
946-
ConfiguredServerLoginParam {
947-
connection: ConnectionCandidate {
948-
host: "posteo.de".to_string(),
949-
port: 587,
950-
security: ConnectionSecurity::Starttls,
951-
},
952-
user: user.to_string(),
953-
},
954-
],
955-
smtp_user: "alice@posteo.de".to_string(),
956-
smtp_password: "foobarbaz".to_string(),
957-
provider: get_provider_by_id("posteo"),
958-
certificate_checks: ConfiguredCertificateChecks::Strict,
959-
oauth2: false,
960-
};
961-
962-
let loaded = ConfiguredLoginParam::load_legacy(&t).await?.unwrap();
963-
assert_eq!(loaded, param);
964-
965-
migrate_configured_login_param(&t).await;
966-
let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
967-
assert_eq!(loaded, param);
968-
969-
Ok(())
970-
}
971-
972-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
973-
async fn test_empty_server_list_legacy() -> Result<()> {
974-
// Find a provider that does not have server list set.
975-
//
976-
// There is at least one such provider in the provider database.
977-
let (domain, provider) = crate::provider::data::PROVIDER_DATA
978-
.iter()
979-
.find(|(_domain, provider)| provider.server.is_empty())
980-
.unwrap();
981-
982-
let t = TestContext::new().await;
983-
984-
let addr = format!("alice@{domain}");
985-
986-
t.set_config(Config::Configured, Some("1")).await?;
987-
t.set_config(Config::ConfiguredProvider, Some(provider.id))
988-
.await?;
989-
t.sql
990-
.set_raw_config(Config::ConfiguredAddr.as_ref(), Some(&addr))
991-
.await?;
992-
t.set_config(Config::ConfiguredMailPw, Some("foobarbaz"))
993-
.await?;
994-
t.set_config(Config::ConfiguredImapCertificateChecks, Some("1"))
995-
.await?; // Strict
996-
t.set_config(Config::ConfiguredSendPw, Some("foobarbaz"))
997-
.await?;
998-
t.set_config(Config::ConfiguredSmtpCertificateChecks, Some("1"))
999-
.await?; // Strict
1000-
t.set_config(Config::ConfiguredServerFlags, Some("0"))
1001-
.await?;
1002-
1003-
let loaded = ConfiguredLoginParam::load_legacy(&t).await?.unwrap();
1004-
assert_eq!(loaded.provider, Some(*provider));
1005-
assert_eq!(loaded.imap.is_empty(), false);
1006-
assert_eq!(loaded.smtp.is_empty(), false);
1007-
1008-
migrate_configured_login_param(&t).await;
1009-
1010-
let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
1011-
assert_eq!(loaded.provider, Some(*provider));
1012-
assert_eq!(loaded.imap.is_empty(), false);
1013-
assert_eq!(loaded.smtp.is_empty(), false);
1014-
1015-
Ok(())
1016-
}
1017-
1018-
async fn migrate_configured_login_param(t: &TestContext) {
1019-
t.sql.execute("DROP TABLE transports;", ()).await.unwrap();
1020-
t.sql.set_raw_config_int("dbversion", 130).await.unwrap();
1021-
t.sql.run_migrations(t).await.log_err(t).ok();
1022-
}
1023-
1024-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1025-
async fn test_empty_server_list() -> Result<()> {
1026-
// Find a provider that does not have server list set.
1027-
//
1028-
// There is at least one such provider in the provider database.
1029-
let (domain, provider) = crate::provider::data::PROVIDER_DATA
1030-
.iter()
1031-
.find(|(_domain, provider)| provider.server.is_empty())
1032-
.unwrap();
1033-
1034-
let t = TestContext::new().await;
1035-
1036-
let addr = format!("alice@{domain}");
1037-
1038-
ConfiguredLoginParam {
1039-
addr: addr.clone(),
1040-
imap: vec![ConfiguredServerLoginParam {
1041-
connection: ConnectionCandidate {
1042-
host: "example.org".to_string(),
1043-
port: 100,
1044-
security: ConnectionSecurity::Tls,
1045-
},
1046-
user: addr.clone(),
1047-
}],
1048-
imap_user: addr.clone(),
1049-
imap_password: "foobarbaz".to_string(),
1050-
smtp: vec![ConfiguredServerLoginParam {
1051-
connection: ConnectionCandidate {
1052-
host: "example.org".to_string(),
1053-
port: 100,
1054-
security: ConnectionSecurity::Tls,
1055-
},
1056-
user: addr.clone(),
1057-
}],
1058-
smtp_user: addr.clone(),
1059-
smtp_password: "foobarbaz".to_string(),
1060-
provider: Some(provider),
1061-
certificate_checks: ConfiguredCertificateChecks::Automatic,
1062-
oauth2: false,
1063-
}
1064-
.save_to_transports_table(&t, &EnteredLoginParam::default(), time())
1065-
.await?;
1066-
1067-
let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
1068-
assert_eq!(loaded.provider, Some(*provider));
1069-
assert_eq!(loaded.imap.is_empty(), false);
1070-
assert_eq!(loaded.smtp.is_empty(), false);
1071-
assert_eq!(t.get_configured_provider().await?, Some(*provider));
1072-
1073-
Ok(())
1074-
}
1075-
}
796+
mod transport_tests;

0 commit comments

Comments
 (0)