Start adding some tests

This commit is contained in:
Hocuri
2026-09-17 18:18:22 +02:00
parent 11a8d42ca7
commit f308e739cd
5 changed files with 87 additions and 3 deletions

View File

@@ -139,6 +139,18 @@ class Account:
"""Add a new transport using a QR code."""
yield self._rpc.add_transport_from_qr.future(self.id, qr)
@futuremethod
def init_transports(self, qr: Optional[str] = None):
"""Automatically adds up to three transports.
If the user just scanned a QR code of type `Account`, `Login`,
`AskVerifyContact`, `AskVerifyGroup`, or `AskJoinBroadcast`,
then UI implementations should pass it as the `qr` parameter.
The host(s) from the QR code will then also be considered
for creating an account there.
"""
yield self._rpc.init_transports.future(self.id, qr)
def delete_transport(self, addr: str):
"""Delete a transport."""
self._rpc.delete_transport(self.id, addr)

View File

@@ -92,9 +92,10 @@ class RPCAccountFactory:
"""Create a new configured account."""
account = self.get_unconfigured_account()
qr = self.get_account_qr()
yield account.add_transport_from_qr.future(qr)
yield account.init_transports.future(qr)
assert account.is_configured()
assert len(account.list_transports()) == 1
return account
def new_configured_bot(self) -> Bot:

View File

@@ -26,12 +26,18 @@ def wait_for_imap_message(imap):
def test_add_second_address(acf) -> None:
account = acf.new_configured_account()
assert len(account.list_transports()) == 1
assert len(account.list_transports()) == 1
qr = acf.get_account_qr()
account.add_transport_from_qr(qr)
assert len(account.list_transports()) == 2
# init_transports() only works on an unconfigured profile:
with pytest.raises(JsonRpcError):
account.init_transports(qr)
with pytest.raises(JsonRpcError):
account.init_transports()
account.add_transport_from_qr(qr)
assert len(account.list_transports()) == 3

View File

@@ -60,7 +60,7 @@ pub(crate) async fn init_transports_inner(
default_relays.shuffle(&mut rng());
let (relays_sender, relays_receiver) = async_channel::unbounded::<String>();
let relays_from_qr: BTreeSet<_> = addrs_from_qr
let relays_from_qr: BTreeSet<String> = addrs_from_qr
.into_iter()
.filter_map(|addr| EmailAddress::new(&addr).ok())
.map(|email| email.domain)

View File

@@ -4,6 +4,71 @@ use super::*;
use crate::test_utils::TestContext;
use crate::tools::SystemTime;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_init_transports_basic() -> Result<()> {
let t = &TestContext::new().await;
assert!(t.list_transports().await?.is_empty());
let skip_network = true;
init_transports_inner(t, vec![], skip_network).await?;
// Wait until the tasks adding transports are finished:
let _ = t.background_task_lock.write().await;
let relays = get_configured_relays(t).await;
assert_eq!(relays.len(), NUM_TRANSPORTS_TARGET);
for relay in &relays {
assert!(DEFAULT_RELAY_CANDIDATES.contains(&relay.as_ref()));
}
assert_eq!(t.get_config_bool(Config::Autorelay).await?, true);
assert_eq!(t.get_config_bool(Config::AutorelayFinished).await?, true);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_init_transports_use_extra_addrs() -> Result<()> {
let t = &TestContext::new().await;
let skip_network = true;
init_transports_inner(
t,
vec![
"alice@example.org".to_string(),
"bob@example.org".to_string(),
"bob@nine.testrun.org".to_string(),
],
skip_network,
)
.await?;
// Wait until the tasks adding transports are finished:
let _ = t.background_task_lock.write().await;
let relays = get_configured_relays(t).await;
assert_eq!(relays.len(), NUM_TRANSPORTS_TARGET);
assert!(relays.contains(&"example.org".to_string()));
assert!(relays.contains(&"nine.testrun.org".to_string()));
Ok(())
}
async fn get_configured_relays(t: &TestContext) -> Vec<String> {
let transports = t.list_transports().await.unwrap();
let mut relays: Vec<_> = transports
.iter()
.map(|t| t.addr.split_once('@').unwrap().1)
.collect();
// Check that every relay is used only once:
relays.sort();
relays.dedup();
assert_eq!(relays.len(), transports.len());
relays.into_iter().map(|s| s.to_string()).collect()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_load_relay_candidates_single() -> Result<()> {
let t = &TestContext::new_alice().await;