prepare the tests to share indexes with api key

This commit is contained in:
Tamo 2024-07-30 13:56:44 +02:00
parent 6666c57880
commit 9372c34dab
6 changed files with 225 additions and 93 deletions

View File

@ -5,63 +5,3 @@ mod payload;
mod tenant_token; mod tenant_token;
mod tenant_token_multi_search; 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<str>) {
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<str>) {
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<str>) -> (Value, StatusCode) {
let url = format!("/keys/{}", key.as_ref());
self.service.get(url).await
}
pub async fn patch_api_key(&self, key: impl AsRef<str>, 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<str>) -> (Value, StatusCode) {
let url = format!("/keys/{}", key.as_ref());
self.service.delete(url).await
}
pub async fn dummy_request(
&self,
method: impl AsRef<str>,
url: impl AsRef<str>,
) -> (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!(),
}
}
}

View File

@ -6,7 +6,7 @@ use once_cell::sync::Lazy;
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime};
use super::authorization::{ALL_ACTIONS, AUTHORIZATIONS}; use super::authorization::{ALL_ACTIONS, AUTHORIZATIONS};
use crate::common::{Server, Value}; use crate::common::{Server, Value, DOCUMENTS};
use crate::json; use crate::json;
fn generate_tenant_token( fn generate_tenant_token(
@ -22,36 +22,6 @@ fn generate_tenant_token(
.unwrap() .unwrap()
} }
static DOCUMENTS: Lazy<Value> = 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<Value> = Lazy::new(|| { static INVALID_RESPONSE: Lazy<Value> = Lazy::new(|| {
json!({ json!({
"message": null, "message": null,

View File

@ -167,26 +167,31 @@ pub static DOCUMENTS: Lazy<Value> = Lazy::new(|| {
{ {
"title": "Shazam!", "title": "Shazam!",
"id": "287947", "id": "287947",
"color": ["green", "blue"],
"_vectors": { "manual": [1, 2, 3]}, "_vectors": { "manual": [1, 2, 3]},
}, },
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": ["yellow", "blue"],
"_vectors": { "manual": [1, 2, 54] }, "_vectors": { "manual": [1, 2, 54] },
}, },
{ {
"title": "Escape Room", "title": "Escape Room",
"id": "522681", "id": "522681",
"color": ["yellow", "red"],
"_vectors": { "manual": [10, -23, 32] }, "_vectors": { "manual": [10, -23, 32] },
}, },
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": ["green", "red"],
"_vectors": { "manual": [-100, 231, 32] }, "_vectors": { "manual": [-100, 231, 32] },
}, },
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": ["blue", "red"],
"_vectors": { "manual": [-100, 340, 90] }, "_vectors": { "manual": [-100, 340, 90] },
} }
]) ])

View File

@ -14,6 +14,7 @@ use meilisearch::option::{IndexerOpts, MaxMemory, MaxThreads, Opt};
use meilisearch::{analytics, create_app, setup_meilisearch, SubscriberForSecondLayer}; use meilisearch::{analytics, create_app, setup_meilisearch, SubscriberForSecondLayer};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use tempfile::TempDir; use tempfile::TempDir;
use tokio::sync::OnceCell;
use tokio::time::sleep; use tokio::time::sleep;
use tracing::level_filters::LevelFilter; use tracing::level_filters::LevelFilter;
use tracing_subscriber::Layer; use tracing_subscriber::Layer;
@ -34,9 +35,12 @@ pub struct Server<State = Owned> {
} }
pub static TEST_TEMP_DIR: Lazy<TempDir> = Lazy::new(|| TempDir::new().unwrap()); pub static TEST_TEMP_DIR: Lazy<TempDir> = Lazy::new(|| TempDir::new().unwrap());
pub static TEST_SHARED_INSTANCE: Lazy<Server<Shared>> = Lazy::new(Server::init_new_shared_instance);
impl Server<Owned> { impl Server<Owned> {
fn to_shared(self) -> Server<Shared> {
Server { service: self.service, _dir: self._dir, _marker: PhantomData }
}
pub async fn new() -> Self { pub async fn new() -> Self {
let dir = TempDir::new().unwrap(); let dir = TempDir::new().unwrap();
@ -82,6 +86,34 @@ impl Server<Owned> {
Ok(Server { service, _dir: None, _marker: PhantomData }) Ok(Server { service, _dir: None, _marker: PhantomData })
} }
pub fn use_api_key(&mut self, api_key: impl AsRef<str>) {
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<str>) {
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<str>, 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<str>) -> (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. /// Returns a view to an index. There is no guarantee that the index exists.
pub fn index(&self, uid: impl AsRef<str>) -> Index<'_> { pub fn index(&self, uid: impl AsRef<str>) -> Index<'_> {
self.index_with_encoder(uid, Encoder::Plain) self.index_with_encoder(uid, Encoder::Plain)
@ -157,7 +189,19 @@ impl Server<Shared> {
} }
pub fn new_shared() -> &'static Server<Shared> { pub fn new_shared() -> &'static Server<Shared> {
&TEST_SHARED_INSTANCE static SERVER: Lazy<Server<Shared>> = Lazy::new(Server::init_new_shared_instance);
&SERVER
}
pub async fn new_shared_with_admin_key() -> &'static Server<Shared> {
static SERVER: OnceCell<Server<Shared>> = 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 /// You shouldn't access random indexes on a shared instance thus this method
@ -243,6 +287,31 @@ impl<State> Server<State> {
.await .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<str>,
url: impl AsRef<str>,
) -> (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<str>) -> (Value, StatusCode) {
let url = format!("/keys/{}", key.as_ref());
self.service.get(url).await
}
pub(super) fn _index(&self, uid: impl AsRef<str>) -> Index<'_> { pub(super) fn _index(&self, uid: impl AsRef<str>) -> Index<'_> {
Index { Index {
uid: uid.as_ref().to_string(), uid: uid.as_ref().to_string(),

View File

@ -742,6 +742,10 @@ async fn test_score_details() {
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -1168,6 +1172,10 @@ async fn experimental_feature_vector_store() {
{ {
"title": "Shazam!", "title": "Shazam!",
"id": "287947", "id": "287947",
"color": [
"green",
"blue"
],
"_vectors": { "_vectors": {
"manual": { "manual": {
"embeddings": [ "embeddings": [
@ -1185,6 +1193,10 @@ async fn experimental_feature_vector_store() {
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": { "manual": {
"embeddings": [ "embeddings": [
@ -1202,6 +1214,10 @@ async fn experimental_feature_vector_store() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": { "manual": {
"embeddings": [ "embeddings": [
@ -1219,6 +1235,10 @@ async fn experimental_feature_vector_store() {
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": { "manual": {
"embeddings": [ "embeddings": [
@ -1236,6 +1256,10 @@ async fn experimental_feature_vector_store() {
{ {
"title": "Escape Room", "title": "Escape Room",
"id": "522681", "id": "522681",
"color": [
"yellow",
"red"
],
"_vectors": { "_vectors": {
"manual": { "manual": {
"embeddings": [ "embeddings": [
@ -1502,6 +1526,10 @@ async fn simple_search_with_strange_synonyms() {
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -1523,6 +1551,10 @@ async fn simple_search_with_strange_synonyms() {
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -1544,6 +1576,10 @@ async fn simple_search_with_strange_synonyms() {
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,

View File

@ -107,6 +107,10 @@ async fn simple_search_single_index() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -128,6 +132,10 @@ async fn simple_search_single_index() {
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -168,6 +176,10 @@ async fn federation_single_search_single_index() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -287,6 +299,10 @@ async fn federation_two_search_single_index() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -303,6 +319,10 @@ async fn federation_two_search_single_index() {
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -454,6 +474,10 @@ async fn simple_search_two_indexes() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -555,6 +579,10 @@ async fn federation_two_search_two_indexes() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -673,6 +701,10 @@ async fn federation_multiple_search_multiple_indexes() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -735,6 +767,10 @@ async fn federation_multiple_search_multiple_indexes() {
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -751,6 +787,10 @@ async fn federation_multiple_search_multiple_indexes() {
{ {
"title": "Escape Room", "title": "Escape Room",
"id": "522681", "id": "522681",
"color": [
"yellow",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
10.0, 10.0,
@ -852,6 +892,10 @@ async fn federation_multiple_search_multiple_indexes() {
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -2053,6 +2097,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() {
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -2070,6 +2118,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() {
{ {
"title": "Escape Room", "title": "Escape Room",
"id": "522681", "id": "522681",
"color": [
"yellow",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
10.0, 10.0,
@ -2087,6 +2139,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -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", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -2121,6 +2181,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() {
{ {
"title": "Shazam!", "title": "Shazam!",
"id": "287947", "id": "287947",
"color": [
"green",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -2158,6 +2222,10 @@ async fn federation_sort_different_indexes_same_criterion_same_direction() {
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -2341,6 +2409,10 @@ async fn federation_sort_different_ranking_rules() {
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -2358,6 +2430,10 @@ async fn federation_sort_different_ranking_rules() {
{ {
"title": "Escape Room", "title": "Escape Room",
"id": "522681", "id": "522681",
"color": [
"yellow",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
10.0, 10.0,
@ -2375,6 +2451,10 @@ async fn federation_sort_different_ranking_rules() {
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -2392,6 +2472,10 @@ async fn federation_sort_different_ranking_rules() {
{ {
"title": "How to Train Your Dragon: The Hidden World", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -2409,6 +2493,10 @@ async fn federation_sort_different_ranking_rules() {
{ {
"title": "Shazam!", "title": "Shazam!",
"id": "287947", "id": "287947",
"color": [
"green",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -2622,6 +2710,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction()
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -2659,6 +2751,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction()
{ {
"title": "Escape Room", "title": "Escape Room",
"id": "522681", "id": "522681",
"color": [
"yellow",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
10.0, 10.0,
@ -2676,6 +2772,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction()
{ {
"title": "Gläss", "title": "Gläss",
"id": "450465", "id": "450465",
"color": [
"blue",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -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", "title": "How to Train Your Dragon: The Hidden World",
"id": "166428", "id": "166428",
"color": [
"green",
"red"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
-100.0, -100.0,
@ -2710,6 +2814,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction()
{ {
"title": "Shazam!", "title": "Shazam!",
"id": "287947", "id": "287947",
"color": [
"green",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,
@ -2767,6 +2875,10 @@ async fn federation_sort_different_indexes_different_criterion_same_direction()
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"id": "299537", "id": "299537",
"color": [
"yellow",
"blue"
],
"_vectors": { "_vectors": {
"manual": [ "manual": [
1.0, 1.0,