aboutsummaryrefslogtreecommitdiff
path: root/crates/secd/src/util/from.rs
blob: 7401468e2dd3415ee46f5e2e74e36f57513d97eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::AuthStore;

impl From<Option<String>> for AuthStore {
    fn from(s: Option<String>) -> Self {
        let conn = s.clone().unwrap_or("sqlite::memory:".into());
        match conn.split(':').next() {
            Some("postgresql") | Some("postgres") => AuthStore::Postgres { conn },
            Some("sqlite") => AuthStore::Sqlite { conn },
            _ => panic!(
                "AuthStore: Invalid database connection string provided. Found: {:?}",
                s
            ),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn auth_store_from_string() {
        let in_conn = None;
        match AuthStore::from(in_conn.clone()) {
            AuthStore::Sqlite { conn } => assert_eq!(conn, "sqlite::memory:"),
            r @ _ => assert!(
                false,
                "should have parsed None as in-memory sqlite. Found: {:?}",
                r
            ),
        }

        let postgresql_conn = Some("postgresql://testuser:p4ssw0rd@1.2.3.4:5432/test-db".into());
        match AuthStore::from(postgresql_conn.clone()) {
            AuthStore::Postgres { conn } => assert_eq!(conn, postgresql_conn.unwrap()),
            r @ _ => assert!(false, "should have parsed as postgres. Found: {:?}", r),
        }

        let postgres_conn = Some("postgres://testuser:p4ssw0rd@1.2.3.4:5432/test-db".into());
        match AuthStore::from(postgres_conn.clone()) {
            AuthStore::Postgres { conn } => assert_eq!(conn, postgres_conn.unwrap()),
            r @ _ => assert!(false, "should have parsed as postgres. Found: {:?}", r),
        }

        let sqlite_conn = Some("sqlite:///path/to/db.sql".into());
        match AuthStore::from(sqlite_conn.clone()) {
            AuthStore::Sqlite { conn } => assert_eq!(conn, sqlite_conn.unwrap()),
            r @ _ => assert!(false, "should have parsed as sqlite. Found: {:?}", r),
        }

        let sqlite_mem_conn = Some("sqlite:memory:".into());
        match AuthStore::from(sqlite_mem_conn.clone()) {
            AuthStore::Sqlite { conn } => assert_eq!(conn, sqlite_mem_conn.unwrap()),
            r @ _ => assert!(
                false,
                "should have parsed as in-memoy sqlite. Found: {:?}",
                r
            ),
        }
    }
}