Skip to content

Commit 85cd383

Browse files
authored
refactor: Move even even more tests into their own files (#6559)
With this, all the tests of the "big" files are in their own files, so this is likely the last PR like this.
1 parent bbb2673 commit 85cd383

File tree

6 files changed

+1820
-1825
lines changed

6 files changed

+1820
-1825
lines changed

src/config.rs

Lines changed: 1 addition & 394 deletions
Original file line numberDiff line numberDiff line change
@@ -963,397 +963,4 @@ fn get_config_keys_string() -> String {
963963
}
964964

965965
#[cfg(test)]
966-
mod tests {
967-
use num_traits::FromPrimitive;
968-
969-
use super::*;
970-
use crate::test_utils::{sync, TestContext, TestContextManager};
971-
972-
#[test]
973-
fn test_to_string() {
974-
assert_eq!(Config::MailServer.to_string(), "mail_server");
975-
assert_eq!(Config::from_str("mail_server"), Ok(Config::MailServer));
976-
977-
assert_eq!(Config::SysConfigKeys.to_string(), "sys.config_keys");
978-
assert_eq!(
979-
Config::from_str("sys.config_keys"),
980-
Ok(Config::SysConfigKeys)
981-
);
982-
}
983-
984-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
985-
async fn test_set_config_addr() {
986-
let t = TestContext::new().await;
987-
988-
// Test that uppercase address get lowercased.
989-
assert!(t
990-
.set_config(Config::Addr, Some("Foobar@eXample.oRg"))
991-
.await
992-
.is_ok());
993-
assert_eq!(
994-
t.get_config(Config::Addr).await.unwrap().unwrap(),
995-
"foobar@example.org"
996-
);
997-
}
998-
999-
/// Tests that "bot" config can only be set to "0" or "1".
1000-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1001-
async fn test_set_config_bot() {
1002-
let t = TestContext::new().await;
1003-
1004-
assert!(t.set_config(Config::Bot, None).await.is_ok());
1005-
assert!(t.set_config(Config::Bot, Some("0")).await.is_ok());
1006-
assert!(t.set_config(Config::Bot, Some("1")).await.is_ok());
1007-
assert!(t.set_config(Config::Bot, Some("2")).await.is_err());
1008-
assert!(t.set_config(Config::Bot, Some("Foobar")).await.is_err());
1009-
}
1010-
1011-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1012-
async fn test_media_quality_config_option() {
1013-
let t = TestContext::new().await;
1014-
let media_quality = t.get_config_int(Config::MediaQuality).await.unwrap();
1015-
assert_eq!(media_quality, 0);
1016-
let media_quality = constants::MediaQuality::from_i32(media_quality).unwrap_or_default();
1017-
assert_eq!(media_quality, constants::MediaQuality::Balanced);
1018-
1019-
t.set_config(Config::MediaQuality, Some("1")).await.unwrap();
1020-
1021-
let media_quality = t.get_config_int(Config::MediaQuality).await.unwrap();
1022-
assert_eq!(media_quality, 1);
1023-
assert_eq!(constants::MediaQuality::Worse as i32, 1);
1024-
let media_quality = constants::MediaQuality::from_i32(media_quality).unwrap_or_default();
1025-
assert_eq!(media_quality, constants::MediaQuality::Worse);
1026-
}
1027-
1028-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1029-
async fn test_ui_config() -> Result<()> {
1030-
let t = TestContext::new().await;
1031-
1032-
assert_eq!(t.get_ui_config("ui.desktop.linux.systray").await?, None);
1033-
1034-
t.set_ui_config("ui.android.screen_security", Some("safe"))
1035-
.await?;
1036-
assert_eq!(
1037-
t.get_ui_config("ui.android.screen_security").await?,
1038-
Some("safe".to_string())
1039-
);
1040-
1041-
t.set_ui_config("ui.android.screen_security", None).await?;
1042-
assert_eq!(t.get_ui_config("ui.android.screen_security").await?, None);
1043-
1044-
assert!(t.set_ui_config("configured", Some("bar")).await.is_err());
1045-
1046-
Ok(())
1047-
}
1048-
1049-
/// Regression test for https://github.com/deltachat/deltachat-core-rust/issues/3012
1050-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1051-
async fn test_set_config_bool() -> Result<()> {
1052-
let t = TestContext::new().await;
1053-
1054-
// We need some config that defaults to true
1055-
let c = Config::E2eeEnabled;
1056-
assert_eq!(t.get_config_bool(c).await?, true);
1057-
t.set_config_bool(c, false).await?;
1058-
assert_eq!(t.get_config_bool(c).await?, false);
1059-
Ok(())
1060-
}
1061-
1062-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1063-
async fn test_self_addrs() -> Result<()> {
1064-
let alice = TestContext::new_alice().await;
1065-
1066-
assert!(alice.is_self_addr("alice@example.org").await?);
1067-
assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]);
1068-
assert!(!alice.is_self_addr("alice@alice.com").await?);
1069-
1070-
// Test adding the same primary address
1071-
alice.set_primary_self_addr("alice@example.org").await?;
1072-
alice.set_primary_self_addr("Alice@Example.Org").await?;
1073-
assert_eq!(alice.get_all_self_addrs().await?, vec!["Alice@Example.Org"]);
1074-
1075-
// Test adding a new (primary) self address
1076-
// The address is trimmed during configure by `LoginParam::from_database()`,
1077-
// so `set_primary_self_addr()` doesn't have to trim it.
1078-
alice.set_primary_self_addr("Alice@alice.com").await?;
1079-
assert!(alice.is_self_addr("aliCe@example.org").await?);
1080-
assert!(alice.is_self_addr("alice@alice.com").await?);
1081-
assert_eq!(
1082-
alice.get_all_self_addrs().await?,
1083-
vec!["Alice@alice.com", "Alice@Example.Org"]
1084-
);
1085-
1086-
// Check that the entry is not duplicated
1087-
alice.set_primary_self_addr("alice@alice.com").await?;
1088-
alice.set_primary_self_addr("alice@alice.com").await?;
1089-
assert_eq!(
1090-
alice.get_all_self_addrs().await?,
1091-
vec!["alice@alice.com", "Alice@Example.Org"]
1092-
);
1093-
1094-
// Test switching back
1095-
alice.set_primary_self_addr("alice@example.org").await?;
1096-
assert_eq!(
1097-
alice.get_all_self_addrs().await?,
1098-
vec!["alice@example.org", "alice@alice.com"]
1099-
);
1100-
1101-
// Test setting a new primary self address, the previous self address
1102-
// should be kept as a secondary self address
1103-
alice.set_primary_self_addr("alice@alice.xyz").await?;
1104-
assert_eq!(
1105-
alice.get_all_self_addrs().await?,
1106-
vec!["alice@alice.xyz", "alice@example.org", "alice@alice.com"]
1107-
);
1108-
assert!(alice.is_self_addr("alice@example.org").await?);
1109-
assert!(alice.is_self_addr("alice@alice.com").await?);
1110-
assert!(alice.is_self_addr("Alice@alice.xyz").await?);
1111-
1112-
Ok(())
1113-
}
1114-
1115-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1116-
async fn test_mdns_default_behaviour() -> Result<()> {
1117-
let t = &TestContext::new_alice().await;
1118-
assert!(t.should_request_mdns().await?);
1119-
assert!(t.should_send_mdns().await?);
1120-
assert!(t.get_config_bool_opt(Config::MdnsEnabled).await?.is_none());
1121-
// The setting should be displayed correctly.
1122-
assert!(t.get_config_bool(Config::MdnsEnabled).await?);
1123-
1124-
t.set_config_bool(Config::Bot, true).await?;
1125-
assert!(!t.should_request_mdns().await?);
1126-
assert!(t.should_send_mdns().await?);
1127-
assert!(t.get_config_bool_opt(Config::MdnsEnabled).await?.is_none());
1128-
assert!(t.get_config_bool(Config::MdnsEnabled).await?);
1129-
Ok(())
1130-
}
1131-
1132-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1133-
async fn test_delete_server_after_default() -> Result<()> {
1134-
let t = &TestContext::new_alice().await;
1135-
1136-
// Check that the settings are displayed correctly.
1137-
assert_eq!(t.get_config(Config::BccSelf).await?, Some("1".to_string()));
1138-
assert_eq!(
1139-
t.get_config(Config::DeleteServerAfter).await?,
1140-
Some("0".to_string())
1141-
);
1142-
1143-
// Leaving emails on the server even w/o `BccSelf` is a good default at least because other
1144-
// MUAs do so even if the server doesn't save sent messages to some sentbox (like Gmail
1145-
// does).
1146-
t.set_config_bool(Config::BccSelf, false).await?;
1147-
assert_eq!(
1148-
t.get_config(Config::DeleteServerAfter).await?,
1149-
Some("0".to_string())
1150-
);
1151-
Ok(())
1152-
}
1153-
1154-
const SAVED_MESSAGES_DEDUPLICATED_FILE: &str = "969142cb84015bc135767bc2370934a.png";
1155-
1156-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1157-
async fn test_sync() -> Result<()> {
1158-
let alice0 = TestContext::new_alice().await;
1159-
let alice1 = TestContext::new_alice().await;
1160-
for a in [&alice0, &alice1] {
1161-
a.set_config_bool(Config::SyncMsgs, true).await?;
1162-
}
1163-
1164-
let mdns_enabled = alice0.get_config_bool(Config::MdnsEnabled).await?;
1165-
// Alice1 has a different config value.
1166-
alice1
1167-
.set_config_bool(Config::MdnsEnabled, !mdns_enabled)
1168-
.await?;
1169-
// This changes nothing, but still sends a sync message.
1170-
alice0
1171-
.set_config_bool(Config::MdnsEnabled, mdns_enabled)
1172-
.await?;
1173-
sync(&alice0, &alice1).await;
1174-
assert_eq!(
1175-
alice1.get_config_bool(Config::MdnsEnabled).await?,
1176-
mdns_enabled
1177-
);
1178-
1179-
// Reset to default. Test that it's not synced because defaults may differ across client
1180-
// versions.
1181-
alice0.set_config(Config::MdnsEnabled, None).await?;
1182-
alice0.set_config_bool(Config::MdnsEnabled, false).await?;
1183-
sync(&alice0, &alice1).await;
1184-
assert_eq!(alice1.get_config_bool(Config::MdnsEnabled).await?, false);
1185-
1186-
for key in [Config::ShowEmails, Config::MvboxMove] {
1187-
let val = alice0.get_config_bool(key).await?;
1188-
alice0.set_config_bool(key, !val).await?;
1189-
sync(&alice0, &alice1).await;
1190-
assert_eq!(alice1.get_config_bool(key).await?, !val);
1191-
}
1192-
1193-
// `Config::SyncMsgs` mustn't be synced.
1194-
alice0.set_config_bool(Config::SyncMsgs, false).await?;
1195-
alice0.set_config_bool(Config::SyncMsgs, true).await?;
1196-
alice0.set_config_bool(Config::MdnsEnabled, true).await?;
1197-
sync(&alice0, &alice1).await;
1198-
assert!(alice1.get_config_bool(Config::MdnsEnabled).await?);
1199-
1200-
// Usual sync scenario.
1201-
async fn test_config_str(
1202-
alice0: &TestContext,
1203-
alice1: &TestContext,
1204-
key: Config,
1205-
val: &str,
1206-
) -> Result<()> {
1207-
alice0.set_config(key, Some(val)).await?;
1208-
sync(alice0, alice1).await;
1209-
assert_eq!(alice1.get_config(key).await?, Some(val.to_string()));
1210-
Ok(())
1211-
}
1212-
test_config_str(&alice0, &alice1, Config::Displayname, "Alice Sync").await?;
1213-
test_config_str(&alice0, &alice1, Config::Selfstatus, "My status").await?;
1214-
1215-
assert!(alice0.get_config(Config::Selfavatar).await?.is_none());
1216-
let file = alice0.dir.path().join("avatar.png");
1217-
let bytes = include_bytes!("../test-data/image/avatar64x64.png");
1218-
tokio::fs::write(&file, bytes).await?;
1219-
alice0
1220-
.set_config(Config::Selfavatar, Some(file.to_str().unwrap()))
1221-
.await?;
1222-
sync(&alice0, &alice1).await;
1223-
// There was a bug that a sync message creates the self-chat with the user avatar instead of
1224-
// the special icon and that remains so when the self-chat becomes user-visible. Let's check
1225-
// this.
1226-
let self_chat = alice0.get_self_chat().await;
1227-
let self_chat_avatar_path = self_chat.get_profile_image(&alice0).await?.unwrap();
1228-
assert_eq!(
1229-
self_chat_avatar_path,
1230-
alice0.get_blobdir().join(SAVED_MESSAGES_DEDUPLICATED_FILE)
1231-
);
1232-
assert!(alice1
1233-
.get_config(Config::Selfavatar)
1234-
.await?
1235-
.filter(|path| path.ends_with(".png"))
1236-
.is_some());
1237-
alice0.set_config(Config::Selfavatar, None).await?;
1238-
sync(&alice0, &alice1).await;
1239-
assert!(alice1.get_config(Config::Selfavatar).await?.is_none());
1240-
1241-
Ok(())
1242-
}
1243-
1244-
/// Sync message mustn't be sent if self-{status,avatar} is changed by a self-sent message.
1245-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1246-
async fn test_no_sync_on_self_sent_msg() -> Result<()> {
1247-
let mut tcm = TestContextManager::new();
1248-
let alice0 = &tcm.alice().await;
1249-
let alice1 = &tcm.alice().await;
1250-
for a in [alice0, alice1] {
1251-
a.set_config_bool(Config::SyncMsgs, true).await?;
1252-
}
1253-
1254-
let status = "Synced via usual message";
1255-
alice0.set_config(Config::Selfstatus, Some(status)).await?;
1256-
alice0.send_sync_msg().await?;
1257-
alice0.pop_sent_sync_msg().await;
1258-
let status1 = "Synced via sync message";
1259-
alice1.set_config(Config::Selfstatus, Some(status1)).await?;
1260-
tcm.send_recv(alice0, alice1, "hi Alice!").await;
1261-
assert_eq!(
1262-
alice1.get_config(Config::Selfstatus).await?,
1263-
Some(status.to_string())
1264-
);
1265-
sync(alice1, alice0).await;
1266-
assert_eq!(
1267-
alice0.get_config(Config::Selfstatus).await?,
1268-
Some(status1.to_string())
1269-
);
1270-
1271-
// Need a chat with another contact to send self-avatar.
1272-
let bob = &tcm.bob().await;
1273-
let a0b_chat_id = tcm.send_recv_accept(bob, alice0, "hi").await.chat_id;
1274-
let file = alice0.dir.path().join("avatar.png");
1275-
let bytes = include_bytes!("../test-data/image/avatar64x64.png");
1276-
tokio::fs::write(&file, bytes).await?;
1277-
alice0
1278-
.set_config(Config::Selfavatar, Some(file.to_str().unwrap()))
1279-
.await?;
1280-
alice0.send_sync_msg().await?;
1281-
alice0.pop_sent_sync_msg().await;
1282-
let file = alice1.dir.path().join("avatar.jpg");
1283-
let bytes = include_bytes!("../test-data/image/avatar1000x1000.jpg");
1284-
tokio::fs::write(&file, bytes).await?;
1285-
alice1
1286-
.set_config(Config::Selfavatar, Some(file.to_str().unwrap()))
1287-
.await?;
1288-
let sent_msg = alice0.send_text(a0b_chat_id, "hi").await;
1289-
alice1.recv_msg(&sent_msg).await;
1290-
assert!(alice1
1291-
.get_config(Config::Selfavatar)
1292-
.await?
1293-
.filter(|path| path.ends_with(".png"))
1294-
.is_some());
1295-
sync(alice1, alice0).await;
1296-
assert!(alice0
1297-
.get_config(Config::Selfavatar)
1298-
.await?
1299-
.filter(|path| path.ends_with(".jpg"))
1300-
.is_some());
1301-
1302-
Ok(())
1303-
}
1304-
1305-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1306-
async fn test_event_config_synced() -> Result<()> {
1307-
let alice0 = TestContext::new_alice().await;
1308-
let alice1 = TestContext::new_alice().await;
1309-
for a in [&alice0, &alice1] {
1310-
a.set_config_bool(Config::SyncMsgs, true).await?;
1311-
}
1312-
1313-
alice0
1314-
.set_config(Config::Displayname, Some("Alice Sync"))
1315-
.await?;
1316-
alice0
1317-
.evtracker
1318-
.get_matching(|e| {
1319-
matches!(
1320-
e,
1321-
EventType::ConfigSynced {
1322-
key: Config::Displayname
1323-
}
1324-
)
1325-
})
1326-
.await;
1327-
sync(&alice0, &alice1).await;
1328-
assert_eq!(
1329-
alice1.get_config(Config::Displayname).await?,
1330-
Some("Alice Sync".to_string())
1331-
);
1332-
alice1
1333-
.evtracker
1334-
.get_matching(|e| {
1335-
matches!(
1336-
e,
1337-
EventType::ConfigSynced {
1338-
key: Config::Displayname
1339-
}
1340-
)
1341-
})
1342-
.await;
1343-
1344-
alice0.set_config(Config::Displayname, None).await?;
1345-
alice0
1346-
.evtracker
1347-
.get_matching(|e| {
1348-
matches!(
1349-
e,
1350-
EventType::ConfigSynced {
1351-
key: Config::Displayname
1352-
}
1353-
)
1354-
})
1355-
.await;
1356-
1357-
Ok(())
1358-
}
1359-
}
966+
mod config_tests;

0 commit comments

Comments
 (0)