-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.rs
104 lines (85 loc) · 3.33 KB
/
tests.rs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use rocket::async_test;
use uuid::Uuid;
use super::*;
use crate::config::*;
mod insert_credentials {
use super::*;
#[async_test]
async fn it_should_insert_good_credentials() {
let (mut tx, repo) = before_each().await;
let credentials = &new_random_credentials();
let new_id = repo.insert_credentials(&mut tx, credentials).await.unwrap();
assert!(new_id > 0);
}
#[async_test]
async fn it_should_reject_duplicated_username() {
let (mut tx, repo) = before_each().await;
let credentials = &new_random_credentials();
let new_id = repo.insert_credentials(&mut tx, credentials).await.unwrap();
assert!(new_id > 0);
let result = repo.insert_credentials(&mut tx, credentials).await;
match result {
// TODO one day we'll have assert_matches https://github.com/rust-lang/rust/issues/82775
Err(Error::Duplicated(_)) => (/* alright */),
Err(e) => panic!("unexpected error: {:?}", e),
Ok(id) => panic!("unexpected id: {:?}", id),
}
}
#[async_test]
async fn it_should_reject_username_too_big() {
let (mut tx, repo) = before_each().await;
let mut credentials = new_random_credentials();
credentials.username = format!("{0}{0}", credentials.username);
let result = repo.insert_credentials(&mut tx, &credentials).await;
match result {
Err(Error::TooBig(_)) => (/* ❤ boilerplate */),
Err(e) => panic!("unexpected error {:?}", e),
Ok(id) => panic!("unexpected id: {:?}", id),
}
}
// TODO test of MVCC, where a tx tries to read data that has already been changed by another concurrent tx
}
mod check_credentials {
use super::*;
#[async_test]
async fn it_should_find_valid_credentials() {
let (mut tx, repo) = before_each().await;
let credentials = &new_random_credentials();
repo.insert_credentials(&mut tx, credentials).await.unwrap();
let is_valid = repo.check_credentials_tx(&mut tx, credentials).await.unwrap();
assert!(is_valid);
}
#[async_test]
async fn it_should_not_find_when_username_is_wrong() {
let (mut tx, repo) = before_each().await;
let credentials = new_random_credentials();
let is_valid = repo.check_credentials_tx(&mut tx, &credentials).await.unwrap();
assert!(!is_valid);
}
#[async_test]
async fn it_should_not_find_when_password_is_wrong() {
let (mut tx, repo) = before_each().await;
let mut credentials = new_random_credentials();
repo.insert_credentials(&mut tx, &credentials).await.unwrap();
credentials.password = String::from("wrong password");
let is_valid = repo.check_credentials_tx(&mut tx, &credentials).await.unwrap();
assert!(!is_valid);
}
}
// aux ----
async fn before_each() -> (Transaction, PostgresCredentialRepo) {
let db = connect().await;
let tx = db.begin().await.unwrap();
let repo = PostgresCredentialRepo;
(tx, repo)
}
async fn connect() -> Pool<Postgres> {
let cfg = app::test_config();
db::connect(&cfg.db.url).await
}
fn new_random_credentials() -> Credentials {
let uuid = Uuid::new_v4().to_string();
let username = format!("test-{}", uuid);
let password = uuid;
Credentials { username, password }
}