Skip to content

Commit

Permalink
passing tests with postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbacardit26 committed Nov 15, 2023
1 parent 16bfc77 commit f9be4e6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, 3.10]
python-version: [3.7, 3.8, 3.9, "3.10"]
database: ["DUMMY", "postgres"]
env:
DATABASE: ${{ matrix.database }}
Expand All @@ -31,7 +31,7 @@ jobs:
# Run commands
flake8 guillotina_audit --config=setup.cfg
isort -c -rc guillotina_audit
black --check --verbose guillotina_elasticsearch
black --check --verbose guillotina_audit
# Run tests
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions guillotina_audit/permissions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from guillotina import configure


configure.grant(role="guillotina.Manager", permission="audit.AccessContent")
14 changes: 10 additions & 4 deletions guillotina_audit/subscriber.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
from guillotina import configure
from guillotina.component import query_utility
from guillotina_audit.interfaces import IAuditUtility
from guillotina.interfaces import IObjectAddedEvent
from guillotina.interfaces import IObjectModifiedEvent
from guillotina.interfaces import IObjectRemovedEvent
from guillotina.interfaces import IResource
from guillotina_audit.interfaces import IAuditUtility


@configure.subscriber(for_=(IResource, IObjectAddedEvent), priority=1001) # after indexing
@configure.subscriber(
for_=(IResource, IObjectAddedEvent), priority=1001
) # after indexing
async def audit_object_added(obj, event):
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)


@configure.subscriber(for_=(IResource, IObjectModifiedEvent), priority=1001) # after indexing
@configure.subscriber(
for_=(IResource, IObjectModifiedEvent), priority=1001
) # after indexing
async def audit_object_modified(obj, event):
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)


@configure.subscriber(for_=(IResource, IObjectRemovedEvent), priority=1001) # after indexing
@configure.subscriber(
for_=(IResource, IObjectRemovedEvent), priority=1001
) # after indexing
async def audit_object_removed(obj, event):
audit = query_utility(IAuditUtility)
audit.log_entry(obj, event)
2 changes: 1 addition & 1 deletion guillotina_audit/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
pytest_plugins = [
"pytest_docker_fixtures",
"guillotina.tests.fixtures",
"guillotina_audit.tests.fixtures"
"guillotina_audit.tests.fixtures",
]
17 changes: 9 additions & 8 deletions guillotina_audit/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from guillotina import testing
from guillotina.tests.fixtures import _update_from_pytest_markers

import json
import os
import pytest
import json


ELASTICSEARCH = os.environ.get("ELASTICSEARCH", "True")

annotations = {
"elasticsearch": {
"host": "localhost:9200"
}
}
annotations = {"elasticsearch": {"host": "localhost:9200"}}


def base_settings_configurator(settings):
if "applications" not in settings:
Expand All @@ -21,7 +18,9 @@ def base_settings_configurator(settings):
settings["applications"].append("guillotina_audit")

settings["audit"] = {
"connection_settings": {"hosts": [f"{annotations['elasticsearch']['host']}"]} # noqa
"connection_settings": {
"hosts": [f"{annotations['elasticsearch']['host']}"]
} # noqa
}


Expand All @@ -42,6 +41,8 @@ def elasticsearch_fixture(es):

@pytest.fixture(scope="function")
async def guillotina_es(elasticsearch_fixture, guillotina):
response, status = await guillotina("POST", "/db/", data=json.dumps({"@type": "Container", "id": "guillotina"}))
response, status = await guillotina(
"POST", "/db/", data=json.dumps({"@type": "Container", "id": "guillotina"})
)
assert status == 200
yield guillotina
17 changes: 13 additions & 4 deletions guillotina_audit/tests/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@


async def test_audit_basic(guillotina_es):
response, status = await guillotina_es("POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"}))
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)
# Let's check the index has been created
resp = await audit_utility.async_es.indices.get_alias()
Expand Down Expand Up @@ -44,14 +47,20 @@ async def test_audit_basic(guillotina_es):
resp, status = await guillotina_es("GET", "/db/guillotina/@audit?action=removed")
assert status == 200
assert len(resp["hits"]["hits"]) == 1
resp, status = await guillotina_es("GET", "/db/guillotina/@audit?action=removed&type_name=Item")
resp, status = await guillotina_es(
"GET", "/db/guillotina/@audit?action=removed&type_name=Item"
)
assert status == 200
assert len(resp["hits"]["hits"]) == 1
resp, status = await guillotina_es("GET", "/db/guillotina/@audit?action=added&type_name=Item")
resp, status = await guillotina_es(
"GET", "/db/guillotina/@audit?action=added&type_name=Item"
)
assert status == 200
assert len(resp["hits"]["hits"]) == 1
assert resp["hits"]["hits"][0]["_source"]["type_name"] == "Item"
resp, status = await guillotina_es("GET", "/db/guillotina/@audit?action=added&type_name=Container")
resp, status = await guillotina_es(
"GET", "/db/guillotina/@audit?action=added&type_name=Container"
)
assert status == 200
assert len(resp["hits"]["hits"]) == 1
assert resp["hits"]["hits"][0]["_source"]["type_name"] == "Container"
Expand Down
13 changes: 10 additions & 3 deletions guillotina_audit/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ async def initialize(self, app):
)

async def create_index(self):
settings = {"settings": self.default_settings(), "mappings": self.default_mappings()}
settings = {
"settings": self.default_settings(),
"mappings": self.default_mappings(),
}
try:
await self.async_es.indices.create(self.index, settings)
except RequestError:
Expand All @@ -40,7 +43,9 @@ def default_settings(self):
return {
"analysis": {
"analyzer": {"path_analyzer": {"tokenizer": "path_tokenizer"}},
"tokenizer": {"path_tokenizer": {"type": "path_hierarchy", "delimiter": "/"}},
"tokenizer": {
"path_tokenizer": {"type": "path_hierarchy", "delimiter": "/"}
},
"filter": {},
"char_filter": {},
}
Expand Down Expand Up @@ -94,7 +99,9 @@ async def query_audit(self, params={}):
):
field_parsed = field.split("__")[0]
operator = field.split("__")[1]
query["query"]["bool"]["must"].append({"range": {field_parsed: {operator: value}}})
query["query"]["bool"]["must"].append(
{"range": {field_parsed: {operator: value}}}
)
else:
query["query"]["bool"]["must"].append({"match": {field: value}})
return await self.async_es.search(index=self.index, body=query)
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"coverage",
"pytest-cov",
"pytest-docker-fixtures[pg]>=1.3.0",
"asyncpg==0.24.0",
"docker>=5.0.0,<6.0.0"
]

Expand Down Expand Up @@ -38,7 +39,8 @@
packages=find_packages(exclude=["ez_setup"]),
install_requires=[
"guillotina>=6.4.3",
"elasticsearch[async]>=7.8.0,<8.0.0"
"elasticsearch[async]>=7.8.0,<8.0.0",
"zope.interface==5.1.0" # TODO: remove once guillotina has solved this
],
tests_require=test_requires,
extras_require={"test": test_requires},
Expand Down

0 comments on commit f9be4e6

Please sign in to comment.