mirror of
https://github.com/creyD/apilog.git
synced 2026-04-12 19:30:29 +02:00
feat: added environment for log entries
This commit is contained in:
29
alembic/versions/21dc1dc045b8_.py
Normal file
29
alembic/versions/21dc1dc045b8_.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 21dc1dc045b8
|
||||
Revises: 74c576cf9560
|
||||
Create Date: 2024-10-10 20:32:12.579725
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "21dc1dc045b8"
|
||||
down_revision: Union[str, None] = "74c576cf9560"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with op.batch_alter_table("logentry", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("environment", sa.String(length=64), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("logentry", schema=None) as batch_op:
|
||||
batch_op.drop_column("environment")
|
||||
@@ -23,6 +23,7 @@ class LogEntry(Base):
|
||||
application = Column(
|
||||
UUID(as_uuid=True), ForeignKey("application.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
environment = Column(String(64), nullable=True, default="prod")
|
||||
# type of the log entry
|
||||
l_type = Column(Enum(LogType), nullable=False, default=LogType.INFO)
|
||||
# type of the transaction
|
||||
|
||||
@@ -73,6 +73,7 @@ async def get_logs(
|
||||
filters: FilterValues = Depends(
|
||||
create_filters(
|
||||
created_by_id=str,
|
||||
environment=str,
|
||||
l_type=LogType,
|
||||
t_type=TransactionType,
|
||||
application=UUID,
|
||||
|
||||
@@ -6,6 +6,7 @@ from pydantic.json_schema import SkipJsonSchema
|
||||
|
||||
class LogIN(BaseSchemaModelIN):
|
||||
application: UUID
|
||||
environment: str = "prod"
|
||||
l_type: LogType = LogType.INFO
|
||||
t_type: TransactionType = TransactionType.UNDEFINED
|
||||
|
||||
|
||||
@@ -24,14 +24,25 @@ def app_context(self, name: str = "Testing"):
|
||||
@contextlib.contextmanager
|
||||
def log_examples(self):
|
||||
LOG_EXAMPLES = [
|
||||
{"l_type": "info", "t_type": "create", "message": "User Max Mustermann created"},
|
||||
{"l_type": "info", "t_type": "update", "message": "User Max Mustermann updated"},
|
||||
{
|
||||
"l_type": "info",
|
||||
"t_type": "create",
|
||||
"message": "User Max Mustermann created",
|
||||
"environment": "dev",
|
||||
},
|
||||
{
|
||||
"l_type": "info",
|
||||
"t_type": "update",
|
||||
"message": "User Max Mustermann updated",
|
||||
"environment": "dev",
|
||||
},
|
||||
{
|
||||
"l_type": "info",
|
||||
"t_type": "create",
|
||||
"author": "auth|max_muster",
|
||||
"message": "User Max Mustermann created a Unit",
|
||||
"object_reference": "1",
|
||||
"environment": "dev",
|
||||
},
|
||||
{
|
||||
"l_type": "info",
|
||||
@@ -40,8 +51,14 @@ def log_examples(self):
|
||||
"message": "User Max Mustermann updated Unit 1",
|
||||
"object_reference": "1",
|
||||
"previous_object": {"name": "Unit 1"},
|
||||
"environment": "prod",
|
||||
},
|
||||
{
|
||||
"l_type": "warning",
|
||||
"t_type": "delete",
|
||||
"message": "User Max Mustermann deleted",
|
||||
"environment": "prod",
|
||||
},
|
||||
{"l_type": "warning", "t_type": "delete", "message": "User Max Mustermann deleted"},
|
||||
]
|
||||
with app_context(self) as app_id:
|
||||
for entry in LOG_EXAMPLES:
|
||||
@@ -129,6 +146,7 @@ class TestAPI:
|
||||
assert re["t_type"] == "undefined"
|
||||
assert re["message"] == None
|
||||
assert re["author"] == "system"
|
||||
assert re["environment"] == "prod"
|
||||
assert re["object_reference"] == None
|
||||
assert re["previous_object"] == None
|
||||
assert re["created_by_id"] == CURRENT_USER
|
||||
@@ -211,3 +229,13 @@ class TestAPI:
|
||||
re = self.c.get("/log/?author%5Bne%5D=auth|max_muster")
|
||||
assert re["total"] == 3
|
||||
assert len(re["results"]) == 3
|
||||
|
||||
# environment
|
||||
re = self.c.get("/log/?environment=dev")
|
||||
assert re["total"] == 3
|
||||
assert len(re["results"]) == 3
|
||||
|
||||
# application and environment
|
||||
re = self.c.get("/log/?application=" + app_id + "&environment=prod")
|
||||
assert re["total"] == 2
|
||||
assert len(re["results"]) == 2
|
||||
|
||||
Reference in New Issue
Block a user