From 7bf555a7dc57ed8d37ef174524333b3de50038ae Mon Sep 17 00:00:00 2001 From: Max Kalashnikoff Date: Tue, 24 Oct 2023 21:37:37 +0200 Subject: [PATCH] fix: fixing and enabling ignored tests (#263) --- tests/functional/multitenant/apns.rs | 53 +----------------------- tests/functional/multitenant/fcm.rs | 37 ++++++++++++----- tests/functional/multitenant/mod.rs | 11 ++++- tests/functional/multitenant/tenancy.rs | 54 +++++++++++++------------ 4 files changed, 67 insertions(+), 88 deletions(-) diff --git a/tests/functional/multitenant/apns.rs b/tests/functional/multitenant/apns.rs index 1a5b02a4..47ace1e2 100644 --- a/tests/functional/multitenant/apns.rs +++ b/tests/functional/multitenant/apns.rs @@ -1,62 +1,13 @@ use { - crate::context::EchoServerContext, - echo_server::handlers::{create_tenant::TenantRegisterBody, get_tenant::GetTenantResponse}, + crate::{context::EchoServerContext, functional::multitenant::ClaimsForValidation}, + echo_server::handlers::create_tenant::TenantRegisterBody, jsonwebtoken::{encode, EncodingKey, Header}, random_string::generate, - serde::Serialize, std::time::SystemTime, test_context::test_context, uuid::Uuid, }; -/// Struct to hold claims for JWT validation -#[derive(Serialize)] -struct ClaimsForValidation { - sub: String, - aud: String, - role: String, - exp: usize, -} - -// #[test_context(EchoServerContext)] -// #[tokio::test] -// async fn tenant_update_apns(ctx: &mut EchoServerContext) { -// let charset = "1234567890"; -// let random_tenant_id = generate(12, charset); -// let payload = TenantRegisterBody { -// id: random_tenant_id.clone(), -// }; -// -// // Register tenant -// let client = reqwest::Client::new(); -// let response = client -// .post(format!("http://{}/tenants", ctx.server.public_addr)) -// .json(&payload) -// .send() -// .await -// .expect("Call failed"); -// -// // Send valid token/cert -// // TODO figure out how to get valid creds into test! -// let api_key = env!("ECHO_TEST_FCM_KEY"); -// let form = reqwest::multipart::Form::new().text("api_key", api_key); -// -// let response = client -// .post(format!( -// "http://{}/tenants/{}/apns", -// ctx.server.public_addr, &random_tenant_id -// )) -// .multipart(form) -// .send() -// .await -// .expect("Call failed"); -// -// assert!( -// response.status().is_success(), -// "Response was not successful" -// ); -// } - #[test_context(EchoServerContext)] #[tokio::test] async fn tenant_update_apns_valid_token(ctx: &mut EchoServerContext) { diff --git a/tests/functional/multitenant/fcm.rs b/tests/functional/multitenant/fcm.rs index 3a13afae..a6b455b0 100644 --- a/tests/functional/multitenant/fcm.rs +++ b/tests/functional/multitenant/fcm.rs @@ -1,48 +1,63 @@ use { - crate::context::EchoServerContext, + crate::{context::EchoServerContext, functional::multitenant::ClaimsForValidation}, echo_server::handlers::create_tenant::TenantRegisterBody, + jsonwebtoken::{encode, EncodingKey, Header}, random_string::generate, + std::time::SystemTime, test_context::test_context, }; #[test_context(EchoServerContext)] #[tokio::test] -// This test is unexpectedly failing and ignored until the resolution -#[ignore] -async fn tenant_update_fcm(ctx: &mut EchoServerContext) { +async fn tenant_update_fcm_valid(ctx: &mut EchoServerContext) { let charset = "1234567890"; let random_tenant_id = generate(12, charset); let payload = TenantRegisterBody { id: random_tenant_id.clone(), }; + let unix_timestamp = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() as usize; + let token_claims = ClaimsForValidation { + sub: random_tenant_id.clone(), + aud: "authenticated".to_string(), + role: "authenticated".to_string(), + exp: unix_timestamp + 60 * 60, // Add an hour for expiration + }; + let jwt_token = encode( + &Header::default(), + &token_claims, + &EncodingKey::from_secret(ctx.config.jwt_secret.as_bytes()), + ) + .expect("Failed to encode jwt token"); // Register tenant let client = reqwest::Client::new(); - client + let register_response = client .post(format!("http://{}/tenants", ctx.server.public_addr)) .json(&payload) + .header("AUTHORIZATION", jwt_token.clone()) .send() .await .expect("Call failed"); + assert_eq!(register_response.status(), reqwest::StatusCode::OK); // Send valid API Key let api_key = env!("ECHO_TEST_FCM_KEY"); let form = reqwest::multipart::Form::new().text("api_key", api_key); - let response = client + let response_fcm_update = client .post(format!( "http://{}/tenants/{}/fcm", ctx.server.public_addr, &random_tenant_id )) + .header("AUTHORIZATION", jwt_token.clone()) .multipart(form) .send() .await .expect("Call failed"); - - assert!( - response.status().is_success(), - "Response was not successful" - ); + assert_eq!(response_fcm_update.status(), reqwest::StatusCode::OK); } #[test_context(EchoServerContext)] diff --git a/tests/functional/multitenant/mod.rs b/tests/functional/multitenant/mod.rs index 5330fb93..cf0a6a95 100644 --- a/tests/functional/multitenant/mod.rs +++ b/tests/functional/multitenant/mod.rs @@ -1,10 +1,19 @@ /// Tests against the handlers -use {crate::context::EchoServerContext, test_context::test_context}; +use {crate::context::EchoServerContext, serde::Serialize, test_context::test_context}; mod apns; mod fcm; mod tenancy; +/// Struct to hold claims for JWT validation +#[derive(Serialize)] +pub struct ClaimsForValidation { + sub: String, + aud: String, + role: String, + exp: usize, +} + #[test_context(EchoServerContext)] #[tokio::test] async fn test_health(ctx: &mut EchoServerContext) { diff --git a/tests/functional/multitenant/tenancy.rs b/tests/functional/multitenant/tenancy.rs index 8e451466..3f028d4e 100644 --- a/tests/functional/multitenant/tenancy.rs +++ b/tests/functional/multitenant/tenancy.rs @@ -1,34 +1,47 @@ use { - crate::context::EchoServerContext, + crate::{context::EchoServerContext, functional::multitenant::ClaimsForValidation}, echo_server::handlers::create_tenant::TenantRegisterBody, + jsonwebtoken::{encode, EncodingKey, Header}, random_string::generate, + std::time::SystemTime, test_context::test_context, }; #[test_context(EchoServerContext)] #[tokio::test] -// This test is unexpectedly failing and ignored until the resolution -#[ignore] -async fn tenant(ctx: &mut EchoServerContext) { +async fn tenant_register_get_delete(ctx: &mut EchoServerContext) { let charset = "1234567890"; let random_tenant_id = generate(12, charset); let payload = TenantRegisterBody { id: random_tenant_id.clone(), }; + let unix_timestamp = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() as usize; + let token_claims = ClaimsForValidation { + sub: random_tenant_id.clone(), + aud: "authenticated".to_string(), + role: "authenticated".to_string(), + exp: unix_timestamp + 60 * 60, // Add an hour for expiration + }; + let jwt_token = encode( + &Header::default(), + &token_claims, + &EncodingKey::from_secret(ctx.config.jwt_secret.as_bytes()), + ) + .expect("Failed to encode jwt token"); // Register tenant let client = reqwest::Client::new(); let response = client .post(format!("http://{}/tenants", ctx.server.public_addr)) .json(&payload) + .header("AUTHORIZATION", jwt_token.clone()) .send() .await .expect("Call failed"); - - assert!( - response.status().is_success(), - "Response was not successful" - ); + assert_eq!(response.status(), reqwest::StatusCode::OK); // Get tenant let response = client @@ -37,15 +50,13 @@ async fn tenant(ctx: &mut EchoServerContext) { ctx.server.public_addr, random_tenant_id.clone() )) + .header("AUTHORIZATION", jwt_token.clone()) .send() .await .expect("Call failed"); + assert_eq!(response.status(), reqwest::StatusCode::OK); - assert!( - response.status().is_success(), - "Response was not successful" - ); - + // Check for CORS assert!(response .headers() .contains_key("Access-Control-Allow-Origin")); @@ -62,14 +73,11 @@ async fn tenant(ctx: &mut EchoServerContext) { ctx.server.public_addr, random_tenant_id.clone() )) + .header("AUTHORIZATION", jwt_token.clone()) .send() .await .expect("Call failed"); - - assert!( - response.status().is_success(), - "Response was not successful" - ); + assert_eq!(response.status(), reqwest::StatusCode::OK); // Get tenant again let response = client @@ -78,14 +86,10 @@ async fn tenant(ctx: &mut EchoServerContext) { ctx.server.public_addr, random_tenant_id.clone() )) + .header("AUTHORIZATION", jwt_token.clone()) .send() .await .expect("Call failed"); - // TODO: this should be changed to 404 - assert_eq!( - response.status().as_u16(), - 400, - "Response was not successful" - ); + assert_eq!(response.status(), reqwest::StatusCode::BAD_REQUEST); }