diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 1b1b089..0b8b49e 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.7, 3.8, 3.9, "3.10"] + python-version: [3.8, 3.9, "3.10", "3.11"] database: ["DUMMY", "postgres"] env: DATABASE: ${{ matrix.database }} @@ -32,7 +32,7 @@ jobs: flake8 guillotina_audit --config=setup.cfg isort -c -rc guillotina_audit black --check --verbose guillotina_audit - + # Run tests - name: Run tests run: | diff --git a/guillotina_audit/models.py b/guillotina_audit/models.py index ba8e89d..eff787c 100644 --- a/guillotina_audit/models.py +++ b/guillotina_audit/models.py @@ -1,29 +1,21 @@ -import json -import datetime - -from pydantic import BaseModel, field_serializer -from enum import Enum -from typing import Optional from datetime import date - from datetime import timezone +from pydantic import BaseModel +from pydantic import field_serializer +from typing import Optional +import datetime +import json -class Action(str, Enum): - modified = "modified" - added = "added" - removed = "removed" - moved = "moved" - duplicated = "duplicated" -class Document(BaseModel): - action: Action +class AuditDocument(BaseModel): + action: str path: Optional[str] = None uuid: Optional[str] = None payload: Optional[dict] = None creator: Optional[str] = None creation_date: date = datetime.datetime.now(timezone.utc) - type_name: str + type_name: Optional[str] = None @field_serializer('payload') def serialize_payload(self, payload: dict, _info): diff --git a/guillotina_audit/subscriber.py b/guillotina_audit/subscriber.py index a31299c..2651d92 100644 --- a/guillotina_audit/subscriber.py +++ b/guillotina_audit/subscriber.py @@ -1,10 +1,10 @@ from guillotina import configure from guillotina.component import query_utility from guillotina.interfaces import IObjectAddedEvent +from guillotina.interfaces import IObjectDuplicatedEvent from guillotina.interfaces import IObjectModifiedEvent -from guillotina.interfaces import IObjectRemovedEvent from guillotina.interfaces import IObjectMovedEvent -from guillotina.interfaces import IObjectDuplicatedEvent +from guillotina.interfaces import IObjectRemovedEvent from guillotina.interfaces import IResource from guillotina_audit.interfaces import IAuditUtility diff --git a/guillotina_audit/tests/test_audit_basic.py b/guillotina_audit/tests/test_audit_basic.py index e270800..79879fe 100644 --- a/guillotina_audit/tests/test_audit_basic.py +++ b/guillotina_audit/tests/test_audit_basic.py @@ -2,7 +2,7 @@ from datetime import timedelta from guillotina.component import query_utility from guillotina_audit.interfaces import IAuditUtility -from guillotina_audit.models import Document +from guillotina_audit.models import AuditDocument import asyncio import json @@ -132,7 +132,7 @@ async def test_audit_wildcard(guillotina_es): await asyncio.sleep(2) audit_utility = query_utility(IAuditUtility) - payload = Document(action="added", type_name="Fullscreen") + payload = AuditDocument(action="added", type_name="Fullscreen") audit_utility.log_wildcard(payload) await asyncio.sleep(2) @@ -143,7 +143,7 @@ async def test_audit_wildcard(guillotina_es): assert status == 200 assert len(resp["hits"]["hits"]) == 1 - payload = Document(action="added", type_name="Click", path="/foopath", payload={"hola": "hola"}, creator="creator", uuid="12345") + payload = AuditDocument(action="added", type_name="Click", path="/foopath", payload={"hola": "hola"}, creator="creator", uuid="12345") audit_utility.log_wildcard(payload) await asyncio.sleep(2) @@ -153,3 +153,14 @@ async def test_audit_wildcard(guillotina_es): ) # noqa assert status == 200 assert len(resp["hits"]["hits"]) == 1 + + payload = AuditDocument(action="CustomAction") + audit_utility.log_wildcard(payload) + await asyncio.sleep(2) + + resp, status = await guillotina_es( + "GET", + "/db/guillotina/@audit?action=CustomAction", + ) # noqa + assert status == 200 + assert len(resp["hits"]["hits"]) == 1 diff --git a/guillotina_audit/utility.py b/guillotina_audit/utility.py index a8f178c..e773334 100644 --- a/guillotina_audit/utility.py +++ b/guillotina_audit/utility.py @@ -4,13 +4,13 @@ from elasticsearch.exceptions import RequestError from guillotina import app_settings from guillotina.interfaces import IObjectAddedEvent +from guillotina.interfaces import IObjectDuplicatedEvent from guillotina.interfaces import IObjectModifiedEvent -from guillotina.interfaces import IObjectRemovedEvent from guillotina.interfaces import IObjectMovedEvent -from guillotina.interfaces import IObjectDuplicatedEvent +from guillotina.interfaces import IObjectRemovedEvent from guillotina.utils.auth import get_authenticated_user from guillotina.utils.content import get_content_path -from guillotina_audit.models import Document +from guillotina_audit.models import AuditDocument import asyncio import datetime @@ -68,7 +68,7 @@ def default_mappings(self): }, } - def log_wildcard(self, payload: Document): + def log_wildcard(self, payload: AuditDocument): coroutine = self.async_es.index(index=self.index, body=payload.dict(exclude_unset=True)) asyncio.create_task(coroutine)