Skip to content

Commit c7a34e4

Browse files
authored
trivial: move console session utility to shared utils file (#8614)
Trivial, just making this its own PR to keep noise out of #7339.
1 parent 64febe9 commit c7a34e4

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

nexus/test-utils/src/resource_helpers.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use crate::ControlPlaneTestContext;
6+
use crate::TEST_SUITE_PASSWORD;
67
use crate::http_testing::RequestBuilder;
78

89
use super::http_testing::AuthnMode;
@@ -12,6 +13,7 @@ use dropshot::HttpErrorResponseBody;
1213
use dropshot::Method;
1314
use dropshot::test_util::ClientTestContext;
1415
use http::StatusCode;
16+
use http::header;
1517
use nexus_db_queries::db::fixed_data::silo::DEFAULT_SILO;
1618
use nexus_test_interface::NexusServer;
1719
use nexus_types::deployment::Blueprint;
@@ -1164,6 +1166,35 @@ pub async fn projects_list(
11641166
.collect()
11651167
}
11661168

1169+
/// Log in with test suite password, return session cookie
1170+
pub async fn create_console_session<N: NexusServer>(
1171+
cptestctx: &ControlPlaneTestContext<N>,
1172+
) -> String {
1173+
let testctx = &cptestctx.external_client;
1174+
let url = format!("/v1/login/{}/local", cptestctx.silo_name);
1175+
let credentials = test_params::UsernamePasswordCredentials {
1176+
username: cptestctx.user_name.as_ref().parse().unwrap(),
1177+
password: TEST_SUITE_PASSWORD.to_string(),
1178+
};
1179+
let login = RequestBuilder::new(&testctx, Method::POST, &url)
1180+
.body(Some(&credentials))
1181+
.expect_status(Some(StatusCode::NO_CONTENT))
1182+
.execute()
1183+
.await
1184+
.expect("failed to log in");
1185+
1186+
let session_cookie = {
1187+
let header_name = header::SET_COOKIE;
1188+
login.headers.get(header_name).unwrap().to_str().unwrap().to_string()
1189+
};
1190+
let (session_token, rest) = session_cookie.split_once("; ").unwrap();
1191+
1192+
assert!(session_token.starts_with("session="));
1193+
assert_eq!(rest, "Path=/; HttpOnly; SameSite=Lax; Max-Age=86400");
1194+
1195+
session_token.to_string()
1196+
}
1197+
11671198
#[derive(Debug)]
11681199
pub struct TestDataset {
11691200
pub id: DatasetUuid,

nexus/tests/integration_tests/console_api.rs

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use anyhow::Context;
66
use camino::Utf8PathBuf;
77
use dropshot::ResultsPage;
88
use dropshot::test_util::ClientTestContext;
9-
use http::header::HeaderName;
109
use http::{StatusCode, header, method::Method};
1110
use nexus_auth::context::OpContext;
1211
use std::env::current_dir;
@@ -17,16 +16,12 @@ use internal_dns_types::names::DNS_ZONE_EXTERNAL_TESTING;
1716
use nexus_db_queries::authn::{USER_TEST_PRIVILEGED, USER_TEST_UNPRIVILEGED};
1817
use nexus_db_queries::db::fixed_data::silo::DEFAULT_SILO;
1918
use nexus_db_queries::db::identity::{Asset, Resource};
20-
use nexus_test_utils::http_testing::{
21-
AuthnMode, NexusRequest, RequestBuilder, TestResponse,
22-
};
23-
use nexus_test_utils::resource_helpers::test_params;
19+
use nexus_test_utils::http_testing::{AuthnMode, NexusRequest, RequestBuilder};
20+
use nexus_test_utils::resource_helpers::create_console_session;
2421
use nexus_test_utils::resource_helpers::{
2522
create_silo, grant_iam, object_create,
2623
};
27-
use nexus_test_utils::{
28-
TEST_SUITE_PASSWORD, load_test_config, test_setup_with_config,
29-
};
24+
use nexus_test_utils::{load_test_config, test_setup_with_config};
3025
use nexus_test_utils_macros::nexus_test;
3126
use nexus_types::external_api::params::{self, ProjectCreate};
3227
use nexus_types::external_api::shared::{SiloIdentityMode, SiloRole};
@@ -54,7 +49,7 @@ async fn test_sessions(cptestctx: &ControlPlaneTestContext) {
5449
.expect("failed to clear cookie and 204 on logout");
5550

5651
// log in and pull the token out of the header so we can use it for authed requests
57-
let session_token = log_in_and_extract_token(cptestctx).await;
52+
let session_token = create_console_session(cptestctx).await;
5853

5954
let project_params = ProjectCreate {
6055
identity: IdentityMetadataCreateParams {
@@ -208,7 +203,7 @@ async fn test_console_pages(cptestctx: &ControlPlaneTestContext) {
208203
)
209204
.await;
210205

211-
let session_token = log_in_and_extract_token(cptestctx).await;
206+
let session_token = create_console_session(cptestctx).await;
212207

213208
// hit console pages with session, should get back HTML response
214209
let console_paths = &[
@@ -882,35 +877,6 @@ async fn test_login_redirect_multiple_silos(
882877
)
883878
}
884879

885-
fn get_header_value(resp: TestResponse, header_name: HeaderName) -> String {
886-
resp.headers.get(header_name).unwrap().to_str().unwrap().to_string()
887-
}
888-
889-
async fn log_in_and_extract_token(
890-
cptestctx: &ControlPlaneTestContext,
891-
) -> String {
892-
let testctx = &cptestctx.external_client;
893-
let url = format!("/v1/login/{}/local", cptestctx.silo_name);
894-
let credentials = test_params::UsernamePasswordCredentials {
895-
username: cptestctx.user_name.as_ref().parse().unwrap(),
896-
password: TEST_SUITE_PASSWORD.to_string(),
897-
};
898-
let login = RequestBuilder::new(&testctx, Method::POST, &url)
899-
.body(Some(&credentials))
900-
.expect_status(Some(StatusCode::NO_CONTENT))
901-
.execute()
902-
.await
903-
.expect("failed to log in");
904-
905-
let session_cookie = get_header_value(login, header::SET_COOKIE);
906-
let (session_token, rest) = session_cookie.split_once("; ").unwrap();
907-
908-
assert!(session_token.starts_with("session="));
909-
assert_eq!(rest, "Path=/; HttpOnly; SameSite=Lax; Max-Age=86400");
910-
911-
session_token.to_string()
912-
}
913-
914880
async fn expect_redirect(testctx: &ClientTestContext, from: &str, to: &str) {
915881
let _ = RequestBuilder::new(&testctx, Method::GET, from)
916882
.expect_status(Some(StatusCode::FOUND))
@@ -942,7 +908,7 @@ async fn test_session_idle_timeout_deletes_session() {
942908
let testctx = &cptestctx.external_client;
943909

944910
// Start session
945-
let session_cookie = log_in_and_extract_token(&cptestctx).await;
911+
let session_cookie = create_console_session(&cptestctx).await;
946912

947913
// sleep here not necessary given TTL of 0
948914

0 commit comments

Comments
 (0)