Skip to content

Commit

Permalink
Merge pull request #367 from algolia/fix/prevent-save-rule-with-empty…
Browse files Browse the repository at this point in the history
…-object-id

fix: Prevent saving a rule with an empty objectID
  • Loading branch information
aseure committed Apr 11, 2018
2 parents e3acc63 + cd11335 commit 4b76d44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions algoliasearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,8 @@ def save_rule(self, rule, forward_to_replicas=False, request_options=None):
"""
if 'objectID' not in rule:
raise AlgoliaException('missing objectID in rule body')
if rule['objectID'] == '':
raise AlgoliaException('objectID in rule body cannot be empty')
params = {'forwardToReplicas': forward_to_replicas}
return self._req(False, '/rules/%s' % str(rule['objectID']), 'PUT', request_options, params, rule)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ def test_save_and_get_rule(index):
assert index.read_rule('my-rule') == rule


def test_empty_objectID_for_rule_should_raise_exception(index):
rule = rule_stub()
rule['objectID'] = ''

try:
index.save_rule(rule)
except AlgoliaException:
return

# We should not be able to save a rule with an empty objectID.
assert False

def test_delete_rule(index):
rule = rule_stub()
res = index.save_rule(rule)
Expand Down

0 comments on commit 4b76d44

Please sign in to comment.