diff options
Diffstat (limited to 'tests/storage.rs')
| -rw-r--r-- | tests/storage.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/storage.rs b/tests/storage.rs new file mode 100644 index 0000000..275d47d --- /dev/null +++ b/tests/storage.rs | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | //! Tests for storage layers that already exist. These must PASS. | ||
| 2 | |||
| 3 | use std::path::PathBuf; | ||
| 4 | |||
| 5 | use zagan_bet::client_database; | ||
| 6 | use zagan_bet::server_database; | ||
| 7 | |||
| 8 | fn tmp_db(name: &str) -> (String, rusqlite::Connection) { | ||
| 9 | let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR")) | ||
| 10 | .join(format!("storage-{name}.db")) | ||
| 11 | .to_string_lossy() | ||
| 12 | .into_owned(); | ||
| 13 | let _ = std::fs::remove_file(&path); | ||
| 14 | let conn = rusqlite::Connection::open(&path).unwrap(); | ||
| 15 | (path, conn) | ||
| 16 | } | ||
| 17 | |||
| 18 | #[test] | ||
| 19 | fn client_credentials_roundtrip() { | ||
| 20 | let (_, conn) = tmp_db("creds"); | ||
| 21 | client_database::init_db(&conn); | ||
| 22 | |||
| 23 | assert_eq!(client_database::get_credentials(&conn, "h1"), None); | ||
| 24 | client_database::save_credentials(&conn, "h1", "alice", "pw").unwrap(); | ||
| 25 | assert_eq!( | ||
| 26 | client_database::get_credentials(&conn, "h1"), | ||
| 27 | Some(("alice".to_string(), "pw".to_string())) | ||
| 28 | ); | ||
| 29 | // Overwrite works. | ||
| 30 | client_database::save_credentials(&conn, "h1", "alice", "pw2").unwrap(); | ||
| 31 | assert_eq!( | ||
| 32 | client_database::get_credentials(&conn, "h1"), | ||
| 33 | Some(("alice".to_string(), "pw2".to_string())) | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | |||
| 37 | #[test] | ||
| 38 | fn client_last_host_tracking() { | ||
| 39 | let (_, conn) = tmp_db("hosts"); | ||
| 40 | client_database::init_db(&conn); | ||
| 41 | |||
| 42 | assert_eq!(client_database::get_last_host(&conn), None); | ||
| 43 | client_database::save_host(&conn, "example.com:9000"); | ||
| 44 | assert_eq!( | ||
| 45 | client_database::get_last_host(&conn), | ||
| 46 | Some("example.com:9000".to_string()) | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | #[test] | ||
| 51 | fn server_user_registration_and_auth_lookup() { | ||
| 52 | let (_, conn) = tmp_db("users"); | ||
| 53 | server_database::init_db(&conn); | ||
| 54 | |||
| 55 | server_database::add_user(&conn, "bob", "cafebabe").unwrap(); | ||
| 56 | assert_eq!( | ||
| 57 | server_database::get_hashed_password(&conn, "bob"), | ||
| 58 | Some("cafebabe".to_string()) | ||
| 59 | ); | ||
| 60 | assert_eq!(server_database::get_hashed_password(&conn, "nobody"), None); | ||
| 61 | } | ||
