Skip to content

Commit

Permalink
fix: Prevent saving a rule with an empty objectID
Browse files Browse the repository at this point in the history
  • Loading branch information
aseure committed Apr 18, 2018
1 parent d6b5cb5 commit bdb89fb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@ Task saveRule(
Boolean forwardToReplicas,
RequestOptions requestOptions)
throws AlgoliaException {
if (ruleId.isEmpty()) {
throw new AlgoliaException("Cannot save rule with empty queryRuleID");
}

Task task =
httpClient.requestWithRetry(
new AlgoliaRequest<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,11 @@ CompletableFuture<AsyncTask> saveRule(
Rule queryRule,
Boolean forwardToReplicas,
RequestOptions requestOptions) {
if (queryRuleID.isEmpty()) {
CompletableFuture f = new CompletableFuture<>();
f.completeExceptionally(new AlgoliaException("Cannot save rule with empty queryRuleID"));
return f;
}
return httpClient
.requestWithRetry(
new AlgoliaRequest<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ default Task saveRule(
boolean forwardToReplicas,
@Nonnull RequestOptions requestOptions)
throws AlgoliaException {
if (queryRuleID.isEmpty()) {
throw new AlgoliaException("Cannot save rule with empty queryRuleID");
}
return getApiClient()
.saveRule(getName(), queryRuleID, content, forwardToReplicas, requestOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.algolia.search.integration.common.async;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.algolia.search.AsyncAlgoliaIntegrationTest;
import com.algolia.search.AsyncIndex;
import com.algolia.search.inputs.query_rules.Condition;
import com.algolia.search.inputs.query_rules.Consequence;
import com.algolia.search.inputs.query_rules.Rule;
import com.algolia.search.objects.RuleQuery;
import com.algolia.search.objects.tasks.async.AsyncTask;
import com.algolia.search.responses.SearchRuleResult;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
Expand Down Expand Up @@ -38,6 +40,15 @@ public void saveAndGetRule() throws Exception {
.isEqualToComparingFieldByFieldRecursively(generateRule(ruleID));
}

@Test
public void trySaveRuleWithEmptyObjectID() throws Exception {
AsyncIndex<?> index = createIndex();

assertThatThrownBy(
() -> index.saveRule("", generateRule("")).get()
).hasMessageContaining("Cannot save rule with empty queryRuleID");
}

@Test
public void deleteQueryRule() throws Exception {
String queryRuleID = "queryRule2";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.algolia.search.integration.common.sync;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.algolia.search.Index;
import com.algolia.search.SyncAlgoliaIntegrationTest;
import com.algolia.search.exceptions.AlgoliaException;
import com.algolia.search.inputs.query_rules.Rule;
import com.algolia.search.objects.RuleQuery;
import com.algolia.search.objects.tasks.sync.Task;
import com.algolia.search.responses.SearchRuleResult;
import java.util.Arrays;
import java.util.Optional;
Expand All @@ -29,6 +31,15 @@ public void saveAndGetRule() throws AlgoliaException {
.isEqualToComparingFieldByFieldRecursively(generateRule(ruleId));
}

@Test
public void trySaveRuleWithEmptyObjectID() throws Exception {
Index<?> index = createIndex();

assertThatThrownBy(
() -> index.saveRule("", generateRule(""))
).hasMessageContaining("Cannot save rule with empty queryRuleID");
}

@Test
public void deleteRule() throws AlgoliaException {
String queryRuleID = "queryRule2";
Expand Down

0 comments on commit bdb89fb

Please sign in to comment.