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}
8
9impl Default for TestConfiguration {
10    fn default() -> Self {
11        TestConfiguration {
12            domain_level: DOMAIN_TGT_LEVEL,
13        }
14    }
15}
16
17#[allow(clippy::expect_used)]
18pub async fn setup_test(config: TestConfiguration) -> QueryServer {
19    sketching::test_init();
20
21    // Create an in memory BE
22    let schema_outer = Schema::new().expect("Failed to init schema");
23    let idxmeta = {
24        let schema_txn = schema_outer.write();
25        schema_txn.reload_idxmeta()
26    };
27    let be =
28        Backend::new(BackendConfig::new_test("main"), idxmeta, false).expect("Failed to init BE");
29
30    let test_server = QueryServer::new(be, schema_outer, "example.com".to_string(), Duration::ZERO)
31        .expect("Failed to setup Query Server");
32
33    test_server
34        .initialise_helper(duration_from_epoch_now(), config.domain_level)
35        .await
36        .expect("init failed!");
37
38    test_server
39}
40
41#[allow(clippy::expect_used)]
42pub async fn setup_pair_test(config: TestConfiguration) -> (QueryServer, QueryServer) {
43    sketching::test_init();
44
45    let qs_a = {
46        // Create an in memory BE
47        let schema_outer = Schema::new().expect("Failed to init schema");
48        let idxmeta = {
49            let schema_txn = schema_outer.write();
50            schema_txn.reload_idxmeta()
51        };
52        let be = Backend::new(BackendConfig::new_test("db_a"), idxmeta, false)
53            .expect("Failed to init BE");
54
55        // Init is called via the proc macro
56        QueryServer::new(be, schema_outer, "example.com".to_string(), Duration::ZERO)
57            .expect("Failed to setup Query Server")
58    };
59
60    let qs_b = {
61        // Create an in memory BE
62        let schema_outer = Schema::new().expect("Failed to init schema");
63        let idxmeta = {
64            let schema_txn = schema_outer.write();
65            schema_txn.reload_idxmeta()
66        };
67        let be = Backend::new(BackendConfig::new_test("db_b"), idxmeta, false)
68            .expect("Failed to init BE");
69
70        // Init is called via the proc macro
71        QueryServer::new(be, schema_outer, "example.com".to_string(), Duration::ZERO)
72            .expect("Failed to setup Query Server")
73    };
74
75    qs_a.initialise_helper(duration_from_epoch_now(), config.domain_level)
76        .await
77        .expect("init failed!");
78
79    qs_b.initialise_helper(duration_from_epoch_now(), config.domain_level)
80        .await
81        .expect("init failed!");
82
83    (qs_a, qs_b)
84}
85
86#[allow(clippy::expect_used)]
87pub async fn setup_idm_test(
88    config: TestConfiguration,
89) -> (IdmServer, IdmServerDelayed, IdmServerAudit) {
90    let qs = setup_test(config).await;
91
92    IdmServer::new(qs, "https://idm.example.com", true)
93        .await
94        .expect("Failed to setup idms")
95}