From 9372c34dab58fa3b377b9723c3d27821699fc867 Mon Sep 17 00:00:00 2001 From: Tamo Date: Tue, 30 Jul 2024 13:56:44 +0200 Subject: [PATCH] prepare the tests to share indexes with api key --- meilisearch/tests/auth/mod.rs | 60 ------------- meilisearch/tests/auth/tenant_token.rs | 32 +------ meilisearch/tests/common/mod.rs | 5 ++ meilisearch/tests/common/server.rs | 73 +++++++++++++++- meilisearch/tests/search/mod.rs | 36 ++++++++ meilisearch/tests/search/multi.rs | 112 +++++++++++++++++++++++++ 6 files changed, 225 insertions(+), 93 deletions(-) diff --git a/meilisearch/tests/auth/mod.rs b/meilisearch/tests/auth/mod.rs index 781b2dcc0..04c2d18b1 100644 --- a/meilisearch/tests/auth/mod.rs +++ b/meilisearch/tests/auth/mod.rs @@ -5,63 +5,3 @@ mod payload; mod tenant_token; mod tenant_token_multi_search; - -use actix_web::http::StatusCode; - -use crate::common::{Server, Value}; -use crate::json; - -impl Server { - pub fn use_api_key(&mut self, api_key: impl AsRef) { - self.service.api_key = Some(api_key.as_ref().to_string()); - } - - /// Fetch and use the default admin key for nexts http requests. - pub async fn use_admin_key(&mut self, master_key: impl AsRef) { - self.use_api_key(master_key); - let (response, code) = self.list_api_keys("").await; - assert_eq!(200, code, "{:?}", response); - let admin_key = &response["results"][1]["key"]; - self.use_api_key(admin_key.as_str().unwrap()); - } - - pub async fn add_api_key(&self, content: Value) -> (Value, StatusCode) { - let url = "/keys"; - self.service.post(url, content).await - } - - pub async fn get_api_key(&self, key: impl AsRef) -> (Value, StatusCode) { - let url = format!("/keys/{}", key.as_ref()); - self.service.get(url).await - } - - pub async fn patch_api_key(&self, key: impl AsRef, content: Value) -> (Value, StatusCode) { - let url = format!("/keys/{}", key.as_ref()); - self.service.patch(url, content).await - } - - pub async fn list_api_keys(&self, params: &str) -> (Value, StatusCode) { - let url = format!("/keys{params}"); - self.service.get(url).await - } - - pub async fn delete_api_key(&self, key: impl AsRef) -> (Value, StatusCode) { - let url = format!("/keys/{}", key.as_ref()); - self.service.delete(url).await - } - - pub async fn dummy_request( - &self, - method: impl AsRef, - url: impl AsRef, - ) -> (Value, StatusCode) { - match method.as_ref() { - "POST" => self.service.post(url, json!({})).await, - "PUT" => self.service.put(url, json!({})).await, - "PATCH" => self.service.patch(url, json!({})).await, - "GET" => self.service.get(url).await, - "DELETE" => self.service.delete(url).await, - _ => unreachable!(), - } - } -} diff --git a/meilisearch/tests/auth/tenant_token.rs b/meilisearch/tests/auth/tenant_token.rs index 5e8a75c36..2e3b228d3 100644 --- a/meilisearch/tests/auth/tenant_token.rs +++ b/meilisearch/tests/auth/tenant_token.rs @@ -6,7 +6,7 @@ use once_cell::sync::Lazy; use time::{Duration, OffsetDateTime}; use super::authorization::{ALL_ACTIONS, AUTHORIZATIONS}; -use crate::common::{Server, Value}; +use crate::common::{Server, Value, DOCUMENTS}; use crate::json; fn generate_tenant_token( @@ -22,36 +22,6 @@ fn generate_tenant_token( .unwrap() } -static DOCUMENTS: Lazy = Lazy::new(|| { - json!([ - { - "title": "Shazam!", - "id": "287947", - "color": ["green", "blue"] - }, - { - "title": "Captain Marvel", - "id": "299537", - "color": ["yellow", "blue"] - }, - { - "title": "Escape Room", - "id": "522681", - "color": ["yellow", "red"] - }, - { - "title": "How to Train Your Dragon: The Hidden World", - "id": "166428", - "color": ["green", "red"] - }, - { - "title": "Glass", - "id": "450465", - "color": ["blue", "red"] - } - ]) -}); - static INVALID_RESPONSE: Lazy = Lazy::new(|| { json!({ "message": null, diff --git a/meilisearch/tests/common/mod.rs b/meilisearch/tests/common/mod.rs index 972c9de60..9a594f8f1 100644 --- a/meilisearch/tests/common/mod.rs +++ b/meilisearch/tests/common/mod.rs @@ -167,26 +167,31 @@ pub static DOCUMENTS: Lazy = Lazy::new(|| { { "title": "Shazam!", "id": "287947", + "color": ["green", "blue"], "_vectors": { "manual": [1, 2, 3]}, }, { "title": "Captain Marvel", "id": "299537", + "color": ["yellow", "blue"], "_vectors": { "manual": [1, 2, 54] }, }, { "title": "Escape Room", "id": "522681", + "color": ["yellow", "red"], "_vectors": { "manual": [10, -23, 32] }, }, { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": ["green", "red"], "_vectors": { "manual": [-100, 231, 32] }, }, { "title": "Gläss", "id": "450465", + "color": ["blue", "red"], "_vectors": { "manual": [-100, 340, 90] }, } ]) diff --git a/meilisearch/tests/common/server.rs b/meilisearch/tests/common/server.rs index bedfc9283..2a5d78241 100644 --- a/meilisearch/tests/common/server.rs +++ b/meilisearch/tests/common/server.rs @@ -14,6 +14,7 @@ use meilisearch::option::{IndexerOpts, MaxMemory, MaxThreads, Opt}; use meilisearch::{analytics, create_app, setup_meilisearch, SubscriberForSecondLayer}; use once_cell::sync::Lazy; use tempfile::TempDir; +use tokio::sync::OnceCell; use tokio::time::sleep; use tracing::level_filters::LevelFilter; use tracing_subscriber::Layer; @@ -34,9 +35,12 @@ pub struct Server { } pub static TEST_TEMP_DIR: Lazy = Lazy::new(|| TempDir::new().unwrap()); -pub static TEST_SHARED_INSTANCE: Lazy> = Lazy::new(Server::init_new_shared_instance); impl Server { + fn to_shared(self) -> Server { + Server { service: self.service, _dir: self._dir, _marker: PhantomData } + } + pub async fn new() -> Self { let dir = TempDir::new().unwrap(); @@ -82,6 +86,34 @@ impl Server { Ok(Server { service, _dir: None, _marker: PhantomData }) } + pub fn use_api_key(&mut self, api_key: impl AsRef) { + self.service.api_key = Some(api_key.as_ref().to_string()); + } + + /// Fetch and use the default admin key for nexts http requests. + pub async fn use_admin_key(&mut self, master_key: impl AsRef) { + self.use_api_key(master_key); + let (response, code) = self.list_api_keys("").await; + assert_eq!(200, code, "{:?}", response); + let admin_key = &response["results"][1]["key"]; + self.use_api_key(admin_key.as_str().unwrap()); + } + + pub async fn add_api_key(&self, content: Value) -> (Value, StatusCode) { + let url = "/keys"; + self.service.post(url, content).await + } + + pub async fn patch_api_key(&self, key: impl AsRef, content: Value) -> (Value, StatusCode) { + let url = format!("/keys/{}", key.as_ref()); + self.service.patch(url, content).await + } + + pub async fn delete_api_key(&self, key: impl AsRef) -> (Value, StatusCode) { + let url = format!("/keys/{}", key.as_ref()); + self.service.delete(url).await + } + /// Returns a view to an index. There is no guarantee that the index exists. pub fn index(&self, uid: impl AsRef) -> Index<'_> { self.index_with_encoder(uid, Encoder::Plain) @@ -157,7 +189,19 @@ impl Server { } pub fn new_shared() -> &'static Server { - &TEST_SHARED_INSTANCE + static SERVER: Lazy> = Lazy::new(Server::init_new_shared_instance); + &SERVER + } + + pub async fn new_shared_with_admin_key() -> &'static Server { + static SERVER: OnceCell> = OnceCell::const_new(); + SERVER + .get_or_init(|| async { + let mut server = Server::new_auth().await; + server.use_admin_key("MASTER_KEY").await; + server.to_shared() + }) + .await } /// You shouldn't access random indexes on a shared instance thus this method @@ -243,6 +287,31 @@ impl Server { .await } + pub async fn list_api_keys(&self, params: &str) -> (Value, StatusCode) { + let url = format!("/keys{params}"); + self.service.get(url).await + } + + pub async fn dummy_request( + &self, + method: impl AsRef, + url: impl AsRef, + ) -> (Value, StatusCode) { + match method.as_ref() { + "POST" => self.service.post(url, json!({})).await, + "PUT" => self.service.put(url, json!({})).await, + "PATCH" => self.service.patch(url, json!({})).await, + "GET" => self.service.get(url).await, + "DELETE" => self.service.delete(url).await, + _ => unreachable!(), + } + } + + pub async fn get_api_key(&self, key: impl AsRef) -> (Value, StatusCode) { + let url = format!("/keys/{}", key.as_ref()); + self.service.get(url).await + } + pub(super) fn _index(&self, uid: impl AsRef) -> Index<'_> { Index { uid: uid.as_ref().to_string(), diff --git a/meilisearch/tests/search/mod.rs b/meilisearch/tests/search/mod.rs index 54c0b343e..3c406cd5f 100644 --- a/meilisearch/tests/search/mod.rs +++ b/meilisearch/tests/search/mod.rs @@ -742,6 +742,10 @@ async fn test_score_details() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -1168,6 +1172,10 @@ async fn experimental_feature_vector_store() { { "title": "Shazam!", "id": "287947", + "color": [ + "green", + "blue" + ], "_vectors": { "manual": { "embeddings": [ @@ -1185,6 +1193,10 @@ async fn experimental_feature_vector_store() { { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": { "embeddings": [ @@ -1202,6 +1214,10 @@ async fn experimental_feature_vector_store() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": { "embeddings": [ @@ -1219,6 +1235,10 @@ async fn experimental_feature_vector_store() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": { "embeddings": [ @@ -1236,6 +1256,10 @@ async fn experimental_feature_vector_store() { { "title": "Escape Room", "id": "522681", + "color": [ + "yellow", + "red" + ], "_vectors": { "manual": { "embeddings": [ @@ -1502,6 +1526,10 @@ async fn simple_search_with_strange_synonyms() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -1523,6 +1551,10 @@ async fn simple_search_with_strange_synonyms() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -1544,6 +1576,10 @@ async fn simple_search_with_strange_synonyms() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, diff --git a/meilisearch/tests/search/multi.rs b/meilisearch/tests/search/multi.rs index 89b06c68c..08ad0b18c 100644 --- a/meilisearch/tests/search/multi.rs +++ b/meilisearch/tests/search/multi.rs @@ -107,6 +107,10 @@ async fn simple_search_single_index() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -128,6 +132,10 @@ async fn simple_search_single_index() { { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -168,6 +176,10 @@ async fn federation_single_search_single_index() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -287,6 +299,10 @@ async fn federation_two_search_single_index() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -303,6 +319,10 @@ async fn federation_two_search_single_index() { { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -454,6 +474,10 @@ async fn simple_search_two_indexes() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -555,6 +579,10 @@ async fn federation_two_search_two_indexes() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -673,6 +701,10 @@ async fn federation_multiple_search_multiple_indexes() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -735,6 +767,10 @@ async fn federation_multiple_search_multiple_indexes() { { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -751,6 +787,10 @@ async fn federation_multiple_search_multiple_indexes() { { "title": "Escape Room", "id": "522681", + "color": [ + "yellow", + "red" + ], "_vectors": { "manual": [ 10.0, @@ -852,6 +892,10 @@ async fn federation_multiple_search_multiple_indexes() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -2053,6 +2097,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() { { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -2070,6 +2118,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() { { "title": "Escape Room", "id": "522681", + "color": [ + "yellow", + "red" + ], "_vectors": { "manual": [ 10.0, @@ -2087,6 +2139,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -2104,6 +2160,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -2121,6 +2181,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() { { "title": "Shazam!", "id": "287947", + "color": [ + "green", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -2158,6 +2222,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() { { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -2341,6 +2409,10 @@ async fn federation_sort_different_ranking_rules() { { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -2358,6 +2430,10 @@ async fn federation_sort_different_ranking_rules() { { "title": "Escape Room", "id": "522681", + "color": [ + "yellow", + "red" + ], "_vectors": { "manual": [ 10.0, @@ -2375,6 +2451,10 @@ async fn federation_sort_different_ranking_rules() { { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -2392,6 +2472,10 @@ async fn federation_sort_different_ranking_rules() { { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -2409,6 +2493,10 @@ async fn federation_sort_different_ranking_rules() { { "title": "Shazam!", "id": "287947", + "color": [ + "green", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -2622,6 +2710,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction() { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -2659,6 +2751,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction() { "title": "Escape Room", "id": "522681", + "color": [ + "yellow", + "red" + ], "_vectors": { "manual": [ 10.0, @@ -2676,6 +2772,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction() { "title": "Gläss", "id": "450465", + "color": [ + "blue", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -2693,6 +2793,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction() { "title": "How to Train Your Dragon: The Hidden World", "id": "166428", + "color": [ + "green", + "red" + ], "_vectors": { "manual": [ -100.0, @@ -2710,6 +2814,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction() { "title": "Shazam!", "id": "287947", + "color": [ + "green", + "blue" + ], "_vectors": { "manual": [ 1.0, @@ -2767,6 +2875,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction() { "title": "Captain Marvel", "id": "299537", + "color": [ + "yellow", + "blue" + ], "_vectors": { "manual": [ 1.0,