Skip to content

Commit

Permalink
fix: fixing and enabling ignored tests (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Oct 24, 2023
1 parent a070782 commit 7bf555a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 88 deletions.
53 changes: 2 additions & 51 deletions tests/functional/multitenant/apns.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
37 changes: 26 additions & 11 deletions tests/functional/multitenant/fcm.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
11 changes: 10 additions & 1 deletion tests/functional/multitenant/mod.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
54 changes: 29 additions & 25 deletions tests/functional/multitenant/tenancy.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"));
Expand All @@ -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
Expand All @@ -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);
}

0 comments on commit 7bf555a

Please sign in to comment.