Skip to content

Commit df69e5f

Browse files
committed
update tests
1 parent 92a19be commit df69e5f

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

nexus/tests/fixtures/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html></html>

nexus/tests/test_console_api.rs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use dropshot::test_util::ClientTestContext;
12
use http::header::HeaderName;
23
use http::{header, method::Method, StatusCode};
34

@@ -27,18 +28,7 @@ async fn test_sessions() {
2728
.expect("failed to clear cookie and 204 on logout");
2829

2930
// log in and pull the token out of the header so we can use it for authed requests
30-
let login = RequestBuilder::new(&testctx, Method::POST, "/login")
31-
.body(Some(LoginParams { username: "privileged".to_string() }))
32-
.expect_status(Some(StatusCode::OK))
33-
.execute()
34-
.await
35-
.expect("failed to log in");
36-
37-
let session_cookie = get_header_value(login, header::SET_COOKIE);
38-
let (session_token, rest) = session_cookie.split_once("; ").unwrap();
39-
40-
assert!(session_token.starts_with("session="));
41-
assert_eq!(rest, "Secure; HttpOnly; SameSite=Lax; Max-Age=3600");
31+
let session_token = log_in_and_extract_token(&testctx).await;
4232

4333
let org_params = OrganizationCreate {
4434
identity: IdentityMetadataCreateParams {
@@ -64,7 +54,7 @@ async fn test_sessions() {
6454

6555
// now make same requests with cookie
6656
RequestBuilder::new(&testctx, Method::POST, "/organizations")
67-
.header(header::COOKIE, session_token)
57+
.header(header::COOKIE, &session_token)
6858
.body(Some(org_params.clone()))
6959
// TODO: explicit expect_status not needed. decide whether to keep it anyway
7060
.expect_status(Some(StatusCode::CREATED))
@@ -73,7 +63,7 @@ async fn test_sessions() {
7363
.expect("failed to create org with session cookie");
7464

7565
RequestBuilder::new(&testctx, Method::GET, "/c/whatever")
76-
.header(header::COOKIE, session_token)
66+
.header(header::COOKIE, &session_token)
7767
.expect_status(Some(StatusCode::NOT_FOUND))
7868
// TODO: this will stop 404ing once we handle rendering the template better
7969
// .expect_status(Some(StatusCode::OK))
@@ -83,12 +73,12 @@ async fn test_sessions() {
8373

8474
// logout with an actual session should delete the session in the db
8575
RequestBuilder::new(&testctx, Method::POST, "/logout")
86-
.header(header::COOKIE, session_token)
76+
.header(header::COOKIE, &session_token)
8777
.expect_status(Some(StatusCode::NO_CONTENT))
8878
// logout also clears the cookie client-side
8979
.expect_response_header(
9080
header::SET_COOKIE,
91-
"session=\"\"; Secure; HttpOnly; SameSite=Lax; Max-Age=0",
81+
"session=; Secure; HttpOnly; SameSite=Lax; Max-Age=0",
9282
)
9383
.execute()
9484
.await
@@ -97,15 +87,15 @@ async fn test_sessions() {
9787
// now the same requests with the same session cookie should 401/302 because
9888
// logout also deletes the session server-side
9989
RequestBuilder::new(&testctx, Method::POST, "/organizations")
100-
.header(header::COOKIE, session_token)
90+
.header(header::COOKIE, &session_token)
10191
.body(Some(org_params))
10292
.expect_status(Some(StatusCode::UNAUTHORIZED))
10393
.execute()
10494
.await
10595
.expect("failed to get 401 for unauthed API request");
10696

10797
RequestBuilder::new(&testctx, Method::GET, "/c/whatever")
108-
.header(header::COOKIE, session_token)
98+
.header(header::COOKIE, &session_token)
10999
.expect_status(Some(StatusCode::FOUND))
110100
.execute()
111101
.await
@@ -127,9 +117,36 @@ async fn test_console_pages() {
127117
.await
128118
.expect("failed to redirect to IdP on auth failure");
129119

130-
// get session
120+
let session_token = log_in_and_extract_token(&testctx).await;
131121

132122
// hit console page with session, should get back HTML response
123+
let console_page = RequestBuilder::new(&testctx, Method::GET, "/c/irrelevant-path")
124+
.header(http::header::COOKIE, session_token)
125+
.expect_status(Some(StatusCode::OK))
126+
.expect_response_header(http::header::CONTENT_TYPE, "text/html; charset=UTF-8")
127+
.execute()
128+
.await
129+
.expect("failed to get console index");
130+
131+
assert_eq!(console_page.body, "<html></html>".as_bytes());
132+
133+
cptestctx.teardown().await;
134+
}
135+
136+
#[tokio::test]
137+
async fn text_login_form() {
138+
let cptestctx = test_setup("test_login_form").await;
139+
let testctx = &cptestctx.external_client;
140+
141+
// login route returns bundle too, but is not auth gated
142+
let console_page = RequestBuilder::new(&testctx, Method::GET, "/login")
143+
.expect_status(Some(StatusCode::OK))
144+
.expect_response_header(http::header::CONTENT_TYPE, "text/html; charset=UTF-8")
145+
.execute()
146+
.await
147+
.expect("failed to get login form");
148+
149+
assert_eq!(console_page.body, "<html></html>".as_bytes());
133150

134151
cptestctx.teardown().await;
135152
}
@@ -168,3 +185,20 @@ async fn test_assets() {
168185
fn get_header_value(resp: TestResponse, header_name: HeaderName) -> String {
169186
resp.headers.get(header_name).unwrap().to_str().unwrap().to_string()
170187
}
188+
189+
async fn log_in_and_extract_token(testctx: &ClientTestContext) -> String {
190+
let login = RequestBuilder::new(&testctx, Method::POST, "/login")
191+
.body(Some(LoginParams { username: "privileged".to_string() }))
192+
.expect_status(Some(StatusCode::OK))
193+
.execute()
194+
.await
195+
.expect("failed to log in");
196+
197+
let session_cookie = get_header_value(login, header::SET_COOKIE);
198+
let (session_token, rest) = session_cookie.split_once("; ").unwrap();
199+
200+
assert!(session_token.starts_with("session="));
201+
assert_eq!(rest, "Secure; HttpOnly; SameSite=Lax; Max-Age=3600");
202+
203+
session_token.to_string()
204+
}

0 commit comments

Comments
 (0)