1
use bonsaidb_core::{
2
    actionable::{Permissions, Statement},
3
    connection::AsyncStorageConnection,
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
15
    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
15
    let server = initialize_basic_server(test_dir.as_ref()).await?;
21
    // initialize_basic_server already installs a cert, so this should fail.
22
2
    assert!(server.install_self_signed_certificate(false).await.is_err());
23
1
    let old_certificate = server
24
2
        .certificate_chain()
25
2
        .await
26
1
        .unwrap()
27
1
        .into_end_entity_certificate();
28
11
    server.install_self_signed_certificate(true).await.unwrap();
29
1
    let new_certificate = server
30
2
        .certificate_chain()
31
2
        .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
31
    pub async fn new(test: HarnessTest) -> anyhow::Result<Self> {
45
31
        let directory = TestDirectory::new(format!("server-{}", test));
46
465
        let server = initialize_basic_server(directory.as_ref()).await?;
47
31
        Ok(Self {
48
31
            _directory: directory,
49
31
            server,
50
31
        })
51
31
    }
52

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

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

            
61
30
    pub async fn connect(&self) -> anyhow::Result<ServerDatabase> {
62
30
        let db = self.server.database::<BasicSchema>("tests").await?;
63
30
        Ok(db)
64
30
    }
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
            .unwrap();
77
        Ok(db)
78
    }
79

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

            
86
38
bonsaidb_core::define_async_connection_test_suite!(TestHarness);
87
10
bonsaidb_core::define_async_pubsub_test_suite!(TestHarness);
88
259
bonsaidb_core::define_async_kv_test_suite!(TestHarness);