-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth_expire_tests.rs
82 lines (65 loc) · 2.18 KB
/
auth_expire_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
use reqwest::StatusCode;
use serial_test::serial;
pub mod common;
use common::{
auth,
constants::{TEST_ADMIN_PASSWORD_HASH, TEST_ADMIN_USERNAME},
helpers, root, test_app,
};
#[tokio::test]
#[serial]
async fn access_token_expire_test() {
// Start API server.
let test_db = test_app::run().await;
let config = helpers::config();
// Assert that revoked options are enabled.
assert!(config.jwt_enable_revoked_tokens);
// Login as an admin.
let tokens = auth::login(TEST_ADMIN_USERNAME, TEST_ADMIN_PASSWORD_HASH)
.await
.expect("Login error.");
// Wait to expire access token.
tokio::time::sleep(tokio::time::Duration::from_secs(
(config.jwt_expire_access_token_seconds + config.jwt_validation_leeway_seconds + 1) as u64,
))
.await;
// Check the access to the root handler with expired token.
assert_eq!(
root::fetch_root(&tokens.access_token).await.unwrap(),
StatusCode::UNAUTHORIZED
);
// Refresh tokens.
let tokens = auth::login(TEST_ADMIN_USERNAME, TEST_ADMIN_PASSWORD_HASH)
.await
.expect("Login error.");
// Try access to the root handler with new token.
assert_eq!(
root::fetch_root(&tokens.access_token).await.unwrap(),
StatusCode::OK
);
// Drop test database.
test_db.drop().await.unwrap();
}
#[tokio::test]
#[serial]
async fn refresh_token_expire_test() {
// Start API server.
let test_db = test_app::run().await;
let config = helpers::config();
// Assert that revoked options are enabled.
assert!(config.jwt_enable_revoked_tokens);
// Login as an admin.
let tokens = auth::login(TEST_ADMIN_USERNAME, TEST_ADMIN_PASSWORD_HASH)
.await
.expect("Login error.");
// Wait to expire refresh token.
tokio::time::sleep(tokio::time::Duration::from_secs(
(config.jwt_expire_refresh_token_seconds + config.jwt_validation_leeway_seconds + 1) as u64,
))
.await;
// Try to refresh with expired token
let result = auth::refresh(&tokens.refresh_token).await;
assert_api_error_status!(result, StatusCode::UNAUTHORIZED);
// Drop test database.
test_db.drop().await.unwrap();
}