From 3bee31e6c70593da015f1425dd72b7dc1b9407d0 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Wed, 2 Feb 2022 18:18:17 +0100 Subject: [PATCH] bug(auth): Make API keys accept Null descriptions --- meilisearch-auth/src/key.rs | 13 +++++---- meilisearch-http/tests/auth/api_keys.rs | 37 ++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/meilisearch-auth/src/key.rs b/meilisearch-auth/src/key.rs index 51b9016ad..ffa56fa88 100644 --- a/meilisearch-auth/src/key.rs +++ b/meilisearch-auth/src/key.rs @@ -20,13 +20,14 @@ pub struct Key { impl Key { pub fn create_from_value(value: Value) -> Result { - let description = value - .get("description") - .map(|des| { + let description = match value.get("description") { + Some(Value::Null) => None, + Some(des) => Some( from_value(des.clone()) - .map_err(|_| AuthControllerError::InvalidApiKeyDescription(des.clone())) - }) - .transpose()?; + .map_err(|_| AuthControllerError::InvalidApiKeyDescription(des.clone()))?, + ), + None => None, + }; let id = generate_id(); diff --git a/meilisearch-http/tests/auth/api_keys.rs b/meilisearch-http/tests/auth/api_keys.rs index fbe107837..52915b96a 100644 --- a/meilisearch-http/tests/auth/api_keys.rs +++ b/meilisearch-http/tests/auth/api_keys.rs @@ -1,6 +1,6 @@ use crate::common::Server; use assert_json_diff::assert_json_include; -use serde_json::json; +use serde_json::{json, Value}; use std::{thread, time}; #[actix_rt::test] @@ -155,6 +155,41 @@ async fn add_valid_api_key_no_description() { assert_eq!(code, 201); } +#[actix_rt::test] +async fn add_valid_api_key_null_description() { + let mut server = Server::new_auth().await; + server.use_api_key("MASTER_KEY"); + + let content = json!({ + "description": Value::Null, + "indexes": ["products"], + "actions": [ + "documents.add" + ], + "expiresAt": "2050-11-13T00:00:00" + }); + + let (response, code) = server.add_api_key(content).await; + + assert!(response["key"].is_string()); + assert!(response["expiresAt"].is_string()); + assert!(response["createdAt"].is_string()); + assert!(response["updatedAt"].is_string()); + + let expected_response = json!({ + "actions": [ + "documents.add" + ], + "indexes": [ + "products" + ], + "expiresAt": "2050-11-13T00:00:00Z" + }); + + assert_json_include!(actual: response, expected: expected_response); + assert_eq!(code, 201); +} + #[actix_rt::test] async fn error_add_api_key_no_header() { let server = Server::new_auth().await;