-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.rs
109 lines (89 loc) · 3.34 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
105
106
107
108
109
use rocket::async_test;
use uuid::Variant;
use super::*;
use crate::config::*;
mod register {
use super::*;
use anyhow::anyhow;
#[async_test]
async fn it_should_return_new_id_when_registration_is_ok() {
let mut svc = before_each().await;
svc.credential_repo
.expect_insert_credentials_tx()
.once()
.return_once(|_, _| Ok(1_i64));
let (username, password) = ("a".into(), "b".into());
let credentials = Credentials { username, password };
let actual = svc.register(credentials).await.unwrap();
assert!(actual > 0);
}
#[async_test]
async fn it_should_return_proper_error_when_registration_fails() {
let mut svc = before_each().await;
svc.credential_repo
.expect_insert_credentials_tx()
.once()
.return_once(|_, _| Err(Error::Other(anyhow!("oops!"))));
let (username, password) = ("a".into(), "b".into());
let credentials = Credentials { username, password };
let actual = svc.register(credentials).await;
match actual {
Err(Error::Other(_)) => (/* love my job */),
Err(e) => panic!("unexpected error: {:?}", e),
Ok(id) => panic!("unexpected id: {}", id),
}
}
}
mod login {
use super::*;
#[async_test]
async fn it_should_return_uuid_token_when_login_is_ok() {
let mut svc = before_each().await;
svc.credential_repo
.expect_check_credentials_db()
.once()
.return_once(|_, _| Ok(true));
svc.token_repo.expect_save_token().once().return_once(|_, _| Ok(()));
let (username, password) = ("a".into(), "b".into());
let credentials = Credentials { username, password };
let actual = svc.login(credentials).await.unwrap();
let actual = Uuid::parse_str(&actual).unwrap();
assert_eq!(actual.get_variant(), Variant::RFC4122);
}
#[async_test]
async fn it_should_not_return_uuid_token_when_login_fails() {
let mut svc = before_each().await;
svc.credential_repo
.expect_check_credentials_db()
.once()
.return_once(|_, _| Ok(false));
svc.token_repo.expect_save_token().never();
let (username, password) = ("a".into(), "b".into());
let credentials = Credentials { username, password };
let actual = svc.login(credentials).await;
match actual {
Err(Error::BadCredentials) => (/* life is good */),
Err(e) => panic!("unexpected error: {:?}", e),
Ok(token) => panic!("unexpected token: {}", token),
}
}
}
// aux -----
async fn before_each() -> AuthService<MockCredentialRepoApi, MockTokenRepoApi> {
AuthService::<MockCredentialRepoApi, MockTokenRepoApi>::new().await
}
impl AuthService<MockCredentialRepoApi, MockTokenRepoApi> {
// TODO AuthService unit tests connect to DB and trigger empty TXs, since
// actual queries are mocked out, but ideally they shouldn't need a DB.
async fn new() -> Self {
let cfg = app::test_config();
let db = db::connect(&cfg.db.url).await;
let credential_repo = MockCredentialRepoApi::new();
let token_repo = MockTokenRepoApi::new();
Self {
db,
credential_repo,
token_repo,
}
}
}