Skip to content

Commit

Permalink
Merge pull request #40 from Kinto/20-index-existing-records
Browse files Browse the repository at this point in the history
Safety check with collections created before plugin activation (ref #20)
  • Loading branch information
leplatrem committed May 26, 2017
2 parents d529ea7 + 3400b9a commit a585ab8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: trusty
language: python
python: 3.5
sudo: false
Expand All @@ -8,6 +9,7 @@ addons:
packages:
- oracle-java8-set-default
- elasticsearch
postgresql: "9.5"
services:
- elasticsearch
env:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Changelog

- Only index records if the storage transaction is committed (fixes #15)
- Do not allow to search if no read permission on collection or bucket (fixes #7)
- Fix empty results response when plugin was enabled after collection creation (ref #20)

**Internal changes**

Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pytest-capturelog
mock
unittest2
webtest
kinto[postgresql]
10 changes: 10 additions & 0 deletions kinto_elasticsearch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@ def search_view(request, **kwargs):
indexer = request.registry.indexer
try:
results = indexer.search(bucket_id, collection_id, **kwargs)

except elasticsearch.NotFoundError as e:
# If plugin was enabled after the creation of the collection.
indexer.create_index(bucket_id, collection_id)
results = indexer.search(bucket_id, collection_id, **kwargs)

except elasticsearch.RequestError as e:
# Malformed query.
message = e.info["error"]["reason"]
details = e.info["error"]["root_cause"][0]
response = http_error(httpexceptions.HTTPBadRequest(),
errno=ERRORS.INVALID_PARAMETERS,
message=message,
details=details)
raise response

except elasticsearch.ElasticsearchException as e:
# General failure.
logger.exception("Index query failed.")
results = {}

return results


Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ def get_app_settings(cls, extras=None):
config = configparser.ConfigParser()
config.read(ini_path)
settings = dict(config.items('app:main'))
if extras:
settings.update(extras)
return settings
44 changes: 44 additions & 0 deletions tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,50 @@ def test_returns_false_if_connection_fails(self):
assert not resp.json["elasticsearch"]


class PostActivation(BaseWebTest, unittest.TestCase):

@classmethod
def get_app_settings(cls, extras=None):
settings = super().get_app_settings(extras)
settings['storage_backend'] = 'kinto.core.storage.postgresql'
settings['storage_url'] = 'postgres://postgres:postgres@localhost:5432/postgres'
settings['permission_backend'] = 'kinto.core.permission.postgresql'
settings['permission_url'] = settings['storage_url']
return settings

def setUp(self):
app = self.make_app(settings={"kinto.includes": ""})
capabilities = app.get("/").json["capabilities"]
assert "elasticsearch" not in capabilities

app.put("/buckets/bid", headers=self.headers)
app.put("/buckets/bid/collections/cid", headers=self.headers)
app.post_json("/buckets/bid/collections/cid/records",
{"data": {"before": "indexing"}},
headers=self.headers)

def test_search_does_not_fail(self):
resp = self.app.get("/buckets/bid/collections/cid/records",
headers=self.headers)
assert len(resp.json["data"]) == 1

resp = self.app.get("/buckets/bid/collections/cid/search",
headers=self.headers)
results = resp.json
assert len(results["hits"]["hits"]) == 0

def test_record_creation_does_not_fail(self):
self.app.post_json("/buckets/bid/collections/cid/records",
{"data": {"after": "indexing"}},
headers=self.headers)

resp = self.app.get("/buckets/bid/collections/cid/search",
headers=self.headers)
results = resp.json
assert len(results["hits"]["hits"]) == 1
assert results["hits"]["hits"][0]["_source"]["after"] == "indexing"


class RecordIndexing(BaseWebTest, unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit a585ab8

Please sign in to comment.