Skip to main content

kanidmd_lib/
testkit.rs

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