kanidmd_lib/
testkit.rs

1use std::str::FromStr;
2
3use crate::be::{Backend, BackendConfig};
4use crate::prelude::*;
5use crate::schema::Schema;
6
7pub struct TestConfiguration {
8    pub domain_level: DomainVersion,
9    // This is literally here to make clippy happy, just leave it alone!
10    // if you don't believe me then remove it and run 'cargo clippy --all-targets' it'll complain
11    // about "struct update has no effect, all the fields in the struct have already been specified"
12    // because the domain_level was set, then we ..Default::default() the "rest"
13    #[allow(dead_code)]
14    pub ignore_this_field: bool,
15}
16
17impl Default for TestConfiguration {
18    fn default() -> Self {
19        TestConfiguration {
20            domain_level: DOMAIN_TGT_LEVEL,
21            ignore_this_field: false,
22        }
23    }
24}
25
26#[allow(clippy::expect_used)]
27pub async fn setup_test(config: TestConfiguration) -> QueryServer {
28    sketching::test_init();
29
30    // Create an in memory BE
31    let schema_outer = Schema::new().expect("Failed to init schema");
32    let idxmeta = {
33        let schema_txn = schema_outer.write();
34        schema_txn.reload_idxmeta()
35    };
36    let be =
37        Backend::new(BackendConfig::new_test("main"), idxmeta, false).expect("Failed to init BE");
38
39    let test_server = QueryServer::new(be, schema_outer, "example.com".to_string(), Duration::ZERO)
40        .expect("Failed to setup Query Server");
41
42    test_server
43        .initialise_helper(duration_from_epoch_now(), config.domain_level)
44        .await
45        .expect("init failed!");
46
47    test_server
48}
49
50#[allow(clippy::expect_used)]
51pub async fn setup_pair_test(config: TestConfiguration) -> (QueryServer, QueryServer) {
52    sketching::test_init();
53
54    let qs_a = {
55        // Create an in memory BE
56        let schema_outer = Schema::new().expect("Failed to init schema");
57        let idxmeta = {
58            let schema_txn = schema_outer.write();
59            schema_txn.reload_idxmeta()
60        };
61        let be = Backend::new(BackendConfig::new_test("db_a"), idxmeta, false)
62            .expect("Failed to init BE");
63
64        // Init is called via the proc macro
65        QueryServer::new(be, schema_outer, "example.com".to_string(), Duration::ZERO)
66            .expect("Failed to setup Query Server")
67    };
68
69    let qs_b = {
70        // Create an in memory BE
71        let schema_outer = Schema::new().expect("Failed to init schema");
72        let idxmeta = {
73            let schema_txn = schema_outer.write();
74            schema_txn.reload_idxmeta()
75        };
76        let be = Backend::new(BackendConfig::new_test("db_b"), idxmeta, false)
77            .expect("Failed to init BE");
78
79        // Init is called via the proc macro
80        QueryServer::new(be, schema_outer, "example.com".to_string(), Duration::ZERO)
81            .expect("Failed to setup Query Server")
82    };
83
84    qs_a.initialise_helper(duration_from_epoch_now(), config.domain_level)
85        .await
86        .expect("init failed!");
87
88    qs_b.initialise_helper(duration_from_epoch_now(), config.domain_level)
89        .await
90        .expect("init failed!");
91
92    (qs_a, qs_b)
93}
94
95#[allow(clippy::expect_used)]
96pub async fn setup_idm_test(
97    config: TestConfiguration,
98) -> (IdmServer, IdmServerDelayed, IdmServerAudit) {
99    let qs = setup_test(config).await;
100
101    IdmServer::new(
102        qs,
103        &Url::from_str("https://idm.example.com").expect("Failed to parse URL"),
104        true,
105        duration_from_epoch_now(),
106    )
107    .await
108    .expect("Failed to setup idms")
109}