1
use bonsaidb_core::{
2
    actionable::{Permissions, Statement},
3
    connection::StorageConnection,
4
    test_util::{self, BasicSchema, HarnessTest, TestDirectory},
5
};
6

            
7
use crate::{server::ServerDatabase, test_util::initialize_basic_server, Server};
8

            
9
1
#[tokio::test]
10
1
async fn simple_test() -> anyhow::Result<()> {
11
1
    let test_dir = TestDirectory::new("simple-test");
12
26
    let server = initialize_basic_server(test_dir.as_ref()).await?;
13
1
    let db = server.database::<BasicSchema>("tests").await?;
14
15
    test_util::store_retrieve_update_delete_tests(&db).await
15
1
}
16

            
17
1
#[tokio::test]
18
1
async fn install_self_signed_certificate_tests() -> anyhow::Result<()> {
19
1
    let test_dir = TestDirectory::new("cert-install-test");
20
29
    let server = initialize_basic_server(test_dir.as_ref()).await?;
21
    // initialize_basic_server already installs a cert, so this should fail.
22
1
    assert!(server.install_self_signed_certificate(false).await.is_err());
23
1
    let old_certificate = server
24
1
        .certificate_chain()
25
        .await
26
1
        .unwrap()
27
1
        .into_end_entity_certificate();
28
7
    server.install_self_signed_certificate(true).await.unwrap();
29
1
    let new_certificate = server
30
1
        .certificate_chain()
31
1
        .await
32
1
        .unwrap()
33
1
        .into_end_entity_certificate();
34
1
    assert_ne!(new_certificate, old_certificate);
35
1
    Ok(())
36
1
}
37

            
38
struct TestHarness {
39
    _directory: TestDirectory,
40
    server: Server,
41
}
42

            
43
impl TestHarness {
44
29
    pub async fn new(test: HarnessTest) -> anyhow::Result<Self> {
45
29
        let directory = TestDirectory::new(format!("server-{}", test));
46
725
        let server = initialize_basic_server(directory.as_ref()).await?;
47
29
        Ok(Self {
48
29
            _directory: directory,
49
29
            server,
50
29
        })
51
29
    }
52

            
53
1
    pub const fn server_name() -> &'static str {
54
1
        "server"
55
1
    }
56

            
57
1
    pub const fn server(&self) -> &'_ Server {
58
1
        &self.server
59
1
    }
60

            
61
28
    pub async fn connect(&self) -> anyhow::Result<ServerDatabase> {
62
28
        let db = self.server.database::<BasicSchema>("tests").await?;
63
28
        Ok(db)
64
28
    }
65

            
66
    #[allow(dead_code)]
67
    async fn connect_with_permissions(
68
        &self,
69
        permissions: Vec<Statement>,
70
        _label: &str,
71
    ) -> anyhow::Result<ServerDatabase> {
72
        let mut db = self.connect().await?;
73
        db.db = db
74
            .db
75
            .with_effective_permissions(Permissions::from(permissions));
76
        Ok(db)
77
    }
78

            
79
25
    pub async fn shutdown(&self) -> anyhow::Result<()> {
80
25
        self.server.shutdown(None).await?;
81
25
        Ok(())
82
25
    }
83
}
84

            
85
36
bonsaidb_core::define_connection_test_suite!(TestHarness);
86
9
bonsaidb_core::define_pubsub_test_suite!(TestHarness);
87
260
bonsaidb_core::define_kv_test_suite!(TestHarness);