-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_tests.rs
69 lines (55 loc) · 2.16 KB
/
http_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
use double_checked_cell_async::DoubleCheckedCell;
use lazy_static::lazy_static;
use rocket::async_test;
use rocket::http::{ContentType, Status};
use rocket::local::asynchronous::Client;
use uuid::{Uuid, Variant::RFC4122};
use rust_auth_demo::{config, model::*};
lazy_static! {
static ref CLIENT: DoubleCheckedCell<Client> = DoubleCheckedCell::new();
}
async fn get_client() -> &'static Client {
// TODO automate the clearing of test database before each execution
std::env::set_var("APP_PROFILE", "test");
CLIENT
.get_or_init(async {
let server = config::app::build_rocket().await;
Client::tracked(server).await.expect("valid rocket instance")
})
.await
}
#[async_test]
async fn it_should_execute_e2e_happy_path() {
let (username, password) = ("foo", "bar12345");
let client = get_client().await;
// registration
let body = format!(r#"{{ "username": "{}", "password": "{}" }}"#, username, password);
let res = client
.post("/register")
.body(&body)
.header(ContentType::JSON)
.dispatch()
.await;
assert_eq!(res.status(), Status::Created);
let location = res.headers().get_one("Location").expect("location header was expected");
assert!(location.starts_with("/profile/"));
let new_id = location.split('/').last().expect("generated id was expected");
let new_id = new_id.parse::<u64>().expect("numeric id was expected");
assert!(new_id > 0);
// login
let res = client.post("/login").body(&body).header(ContentType::JSON).dispatch().await;
assert_eq!(res.status(), Status::Ok);
let actual = res.into_json::<LoginOk>().await.expect("login response was expected");
let token = Uuid::parse_str(&actual.token).expect("uuid token was expected");
assert_eq!(token.get_variant(), RFC4122);
// authentication
let res = client
.post("/authenticate")
.body(token)
.header(ContentType::Plain)
.dispatch()
.await;
assert_eq!(res.status(), Status::Ok);
let actual = res.into_json::<AuthOk>().await.expect("auth response was expected");
assert_eq!(username, actual.username);
}