fix error 500 when sending bad rankingRules; fix #500

This commit is contained in:
qdequele 2020-03-05 15:18:19 +01:00 committed by Quentin de Quelen
parent 8aeddec982
commit 8df6d6e954
3 changed files with 53 additions and 8 deletions

View File

@ -115,7 +115,8 @@ pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_update()?)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -177,7 +178,8 @@ pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_update()?)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -229,7 +231,8 @@ pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_update()?)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -284,7 +287,8 @@ pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_update()?)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -342,7 +346,8 @@ pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_update()?)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -395,7 +400,8 @@ pub async fn update_accept_new_fields(mut ctx: Request<Data>) -> SResult<Respons
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_update()?)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };

View File

@ -260,6 +260,11 @@ impl Server {
self.post_request_async(&url, body); self.post_request_async(&url, body);
} }
pub fn update_ranking_rules_sync(&mut self, body: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.post_request(&url, body)
}
pub fn delete_ranking_rules(&mut self) -> (Value, StatusCode) { pub fn delete_ranking_rules(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid); let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url)

View File

@ -37,7 +37,7 @@ fn write_all_and_delete() {
let (response, _status_code) = server.get_ranking_rules(); let (response, _status_code) = server.get_ranking_rules();
let json = json!([ let expected = json!([
"typo", "typo",
"words", "words",
"proximity", "proximity",
@ -46,7 +46,7 @@ fn write_all_and_delete() {
"exactness" "exactness"
]); ]);
assert_json_eq!(expect, response, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }
#[test] #[test]
@ -105,3 +105,37 @@ fn write_all_and_update() {
assert_json_eq!(expected, response, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }
#[test]
fn send_undefined_rule() {
let mut server = common::Server::with_uid("movies");
let body = json!({
"uid": "movies",
"identifier": "id",
});
server.create_index(body);
let body = json!([
"typos",
]);
let (_response, status_code) = server.update_ranking_rules_sync(body);
assert_eq!(status_code, 400);
}
#[test]
fn send_malformed_custom_rule() {
let mut server = common::Server::with_uid("movies");
let body = json!({
"uid": "movies",
"identifier": "id",
});
server.create_index(body);
let body = json!([
"dsc(truc)",
]);
let (_response, status_code) = server.update_ranking_rules_sync(body);
assert_eq!(status_code, 400);
}