kanidmd_lib/
testkit.rs

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