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

            
5
use crate::server::ServerDatabase;
6
use crate::test_util::initialize_basic_server;
7
use crate::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
}
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
1
        .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
1
        .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
}
37

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

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

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

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

            
61
34
    pub async fn connect(&self) -> anyhow::Result<ServerDatabase> {
62
34
        let db = self.server.database::<BasicSchema>("tests").await?;
63
34
        Ok(db)
64
34
    }
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
30
    pub async fn shutdown(&self) -> anyhow::Result<()> {
81
30
        self.server.shutdown(None).await?;
82
30
        Ok(())
83
30
    }
84
}
85

            
86
46
bonsaidb_core::define_async_connection_test_suite!(TestHarness);
87
10
bonsaidb_core::define_async_pubsub_test_suite!(TestHarness);
88
255
bonsaidb_core::define_async_kv_test_suite!(TestHarness);