From 558b66e5352805b707f700258efe731c5c394095 Mon Sep 17 00:00:00 2001 From: Tamo Date: Mon, 24 Jun 2024 19:00:44 +0200 Subject: [PATCH] makes most tests works with variable error messages --- meilisearch/tests/auth/authorization.rs | 2 +- meilisearch/tests/auth/tenant_token.rs | 19 +++++++++----- .../tests/auth/tenant_token_multi_search.rs | 25 +++++++++++++------ meilisearch/tests/common/mod.rs | 6 +++++ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/meilisearch/tests/auth/authorization.rs b/meilisearch/tests/auth/authorization.rs index d26bb26b8..a57c9e11d 100644 --- a/meilisearch/tests/auth/authorization.rs +++ b/meilisearch/tests/auth/authorization.rs @@ -280,7 +280,7 @@ async fn access_authorized_no_index_restriction() { route, action ); - assert_ne!(code, 403); + assert_ne!(code, 403, "on route: {:?} - {:?} with action: {:?}", method, route, action); } } } diff --git a/meilisearch/tests/auth/tenant_token.rs b/meilisearch/tests/auth/tenant_token.rs index ba3b0b234..5e8a75c36 100644 --- a/meilisearch/tests/auth/tenant_token.rs +++ b/meilisearch/tests/auth/tenant_token.rs @@ -53,7 +53,8 @@ static DOCUMENTS: Lazy = Lazy::new(|| { }); static INVALID_RESPONSE: Lazy = Lazy::new(|| { - json!({"message": "The provided API key is invalid.", + json!({ + "message": null, "code": "invalid_api_key", "type": "auth", "link": "https://docs.meilisearch.com/errors#invalid_api_key" @@ -191,7 +192,9 @@ macro_rules! compute_forbidden_search { server.use_api_key(&web_token); let index = server.index("sales"); index - .search(json!({}), |response, code| { + .search(json!({}), |mut response, code| { + // We don't assert anything on the message since it may change between cases + response["message"] = serde_json::json!(null); assert_eq!( response, INVALID_RESPONSE.clone(), @@ -495,7 +498,8 @@ async fn error_access_forbidden_routes() { for ((method, route), actions) in AUTHORIZATIONS.iter() { if !actions.contains("search") { - let (response, code) = server.dummy_request(method, route).await; + let (mut response, code) = server.dummy_request(method, route).await; + response["message"] = serde_json::json!(null); assert_eq!(response, INVALID_RESPONSE.clone()); assert_eq!(code, 403); } @@ -529,14 +533,16 @@ async fn error_access_expired_parent_key() { server.use_api_key(&web_token); // test search request while parent_key is not expired - let (response, code) = server.dummy_request("POST", "/indexes/products/search").await; + let (mut response, code) = server.dummy_request("POST", "/indexes/products/search").await; + response["message"] = serde_json::json!(null); assert_ne!(response, INVALID_RESPONSE.clone()); assert_ne!(code, 403); // wait until the key is expired. thread::sleep(time::Duration::new(1, 0)); - let (response, code) = server.dummy_request("POST", "/indexes/products/search").await; + let (mut response, code) = server.dummy_request("POST", "/indexes/products/search").await; + response["message"] = serde_json::json!(null); assert_eq!(response, INVALID_RESPONSE.clone()); assert_eq!(code, 403); } @@ -585,7 +591,8 @@ async fn error_access_modified_token() { .join("."); server.use_api_key(&altered_token); - let (response, code) = server.dummy_request("POST", "/indexes/products/search").await; + let (mut response, code) = server.dummy_request("POST", "/indexes/products/search").await; + response["message"] = serde_json::json!(null); assert_eq!(response, INVALID_RESPONSE.clone()); assert_eq!(code, 403); } diff --git a/meilisearch/tests/auth/tenant_token_multi_search.rs b/meilisearch/tests/auth/tenant_token_multi_search.rs index 09b5dbbcc..81146d14e 100644 --- a/meilisearch/tests/auth/tenant_token_multi_search.rs +++ b/meilisearch/tests/auth/tenant_token_multi_search.rs @@ -109,9 +109,11 @@ static NESTED_DOCUMENTS: Lazy = Lazy::new(|| { fn invalid_response(query_index: Option) -> Value { let message = if let Some(query_index) = query_index { - format!("Inside `.queries[{query_index}]`: The provided API key is invalid.") + json!(format!("Inside `.queries[{query_index}]`: The provided API key is invalid.")) } else { - "The provided API key is invalid.".to_string() + // if it's anything else we simply return null and will tests all the + // error messages somewhere else + json!(null) }; json!({"message": message, "code": "invalid_api_key", @@ -414,7 +416,10 @@ macro_rules! compute_forbidden_single_search { for (tenant_token, failed_query_index) in $tenant_tokens.iter().zip(failed_query_indexes.into_iter()) { let web_token = generate_tenant_token(&uid, &key, tenant_token.clone()); server.use_api_key(&web_token); - let (response, code) = server.multi_search(json!({"queries" : [{"indexUid": "sales"}]})).await; + let (mut response, code) = server.multi_search(json!({"queries" : [{"indexUid": "sales"}]})).await; + if failed_query_index.is_none() && !response["message"].is_null() { + response["message"] = serde_json::json!(null); + } assert_eq!( response, invalid_response(failed_query_index), @@ -469,10 +474,13 @@ macro_rules! compute_forbidden_multiple_search { for (tenant_token, failed_query_index) in $tenant_tokens.iter().zip(failed_query_indexes.into_iter()) { let web_token = generate_tenant_token(&uid, &key, tenant_token.clone()); server.use_api_key(&web_token); - let (response, code) = server.multi_search(json!({"queries" : [ + let (mut response, code) = server.multi_search(json!({"queries" : [ {"indexUid": "sales"}, {"indexUid": "products"}, ]})).await; + if failed_query_index.is_none() && !response["message"].is_null() { + response["message"] = serde_json::json!(null); + } assert_eq!( response, invalid_response(failed_query_index), @@ -1073,18 +1081,20 @@ async fn error_access_expired_parent_key() { server.use_api_key(&web_token); // test search request while parent_key is not expired - let (response, code) = server + let (mut response, code) = server .multi_search(json!({"queries" : [{"indexUid": "sales"}, {"indexUid": "products"}]})) .await; + response["message"] = serde_json::json!(null); assert_ne!(response, invalid_response(None)); assert_ne!(code, 403); // wait until the key is expired. thread::sleep(time::Duration::new(1, 0)); - let (response, code) = server + let (mut response, code) = server .multi_search(json!({"queries" : [{"indexUid": "sales"}, {"indexUid": "products"}]})) .await; + response["message"] = serde_json::json!(null); assert_eq!(response, invalid_response(None)); assert_eq!(code, 403); } @@ -1134,8 +1144,9 @@ async fn error_access_modified_token() { .join("."); server.use_api_key(&altered_token); - let (response, code) = + let (mut response, code) = server.multi_search(json!({"queries" : [{"indexUid": "products"}]})).await; + response["message"] = serde_json::json!(null); assert_eq!(response, invalid_response(None)); assert_eq!(code, 403); } diff --git a/meilisearch/tests/common/mod.rs b/meilisearch/tests/common/mod.rs index 3117dd185..1391cf7cf 100644 --- a/meilisearch/tests/common/mod.rs +++ b/meilisearch/tests/common/mod.rs @@ -42,6 +42,12 @@ impl std::ops::Deref for Value { } } +impl std::ops::DerefMut for Value { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + impl PartialEq for Value { fn eq(&self, other: &serde_json::Value) -> bool { &self.0 == other