Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

feat: add template scan trigger model #99

Merged
merged 2 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def upgrade():
"organisations",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("name", sa.Unicode(255), nullable=False, unique=True),
sa.Column("created_at", sa.DateTime, default=sa.func.now()),
sa.Column("created_at", sa.DateTime, default=sa.func.utc_timestamp()),
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def upgrade():
sa.Column(
"access_token", postgresql.UUID(as_uuid=True), nullable=False, unique=True
),
sa.Column("created_at", sa.DateTime, default=sa.func.now()),
sa.Column("created_at", sa.DateTime, default=sa.func.utc_timestamp()),
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()),
sa.ForeignKeyConstraint(
["organisation_id"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def upgrade():
sa.Column("organisation_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("token", postgresql.UUID(as_uuid=True), nullable=False, unique=True),
sa.Column("name", sa.String(), nullable=False),
sa.Column("created_at", sa.DateTime, default=sa.func.now()),
sa.Column("created_at", sa.DateTime, default=sa.func.utc_timestamp()),
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()),
sa.ForeignKeyConstraint(
["organisation_id"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def upgrade():
"scan_types",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("name", sa.Unicode(255), nullable=False, unique=True),
sa.Column("created_at", sa.DateTime, default=sa.func.now()),
sa.Column("created_at", sa.DateTime, default=sa.func.utc_timestamp()),
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()),
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""create template scan triggers table

Revision ID: db98eafe1333
Revises: e6ec4f01db2f
Create Date: 2021-08-24 17:21:59.537651

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "db98eafe1333"
down_revision = "e6ec4f01db2f"
branch_labels = None
depends_on = None


def upgrade():
op.create_table(
"template_scan_triggers",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("template_scan_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("callback", postgresql.JSONB(), nullable=False),
sa.Column("created_at", sa.DateTime, default=sa.func.utc_timestamp()),
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()),
dj2 marked this conversation as resolved.
Show resolved Hide resolved
sa.ForeignKeyConstraint(
["template_scan_id"],
["template_scans.id"],
),
)


def downgrade():
op.drop_table("template_scan_triggers")
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def upgrade():
sa.Column("template_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("scan_type_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("data", postgresql.JSONB(), nullable=False),
sa.Column("created_at", sa.DateTime, default=sa.func.now()),
sa.Column("created_at", sa.DateTime, default=sa.func.utc_timestamp()),
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()),
sa.ForeignKeyConstraint(
["template_id"],
Expand Down
4 changes: 3 additions & 1 deletion api/models/TemplateScan.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class TemplateScan(Base):
)
scan_type = relationship("ScanType", back_populates="template_scans")

template_scan_triggers = relationship("TemplateScanTrigger")

@validates("data")
def validate_name(self, _key, value):
def validate_data(self, _key, value):
assert value != ""
return value
41 changes: 41 additions & 0 deletions api/models/TemplateScanTrigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import datetime
import uuid

from sqlalchemy import DateTime, Column, ForeignKey
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import relationship, validates

from models import Base
from models.TemplateScan import TemplateScan


class TemplateScanTrigger(Base):
__tablename__ = "template_scan_triggers"

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
callback = Column(JSONB, nullable=False)
created_at = Column(
DateTime,
index=False,
unique=False,
nullable=False,
default=datetime.datetime.utcnow,
)
updated_at = Column(
DateTime,
index=False,
unique=False,
nullable=True,
onupdate=datetime.datetime.utcnow,
)
template_scan_id = Column(
UUID(as_uuid=True), ForeignKey(TemplateScan.id), index=True, nullable=False
)
template_scan = relationship(
"TemplateScan", back_populates="template_scan_triggers"
)

@validates("callback")
def validate_callback(self, _key, value):
assert value != ""
return value
12 changes: 12 additions & 0 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from models.Organisation import Organisation
from models.ScanType import ScanType
from models.Template import Template
from models.TemplateScan import TemplateScan


from sqlalchemy import create_engine
Expand Down Expand Up @@ -62,3 +63,14 @@ def template_fixture(session, organisation_fixture):
template = Template(name="name", organisation=organisation_fixture)
session.add(template)
return template


@pytest.fixture(scope="session")
def template_scan_fixture(scan_type_fixture, template_fixture, session):
template_scan = TemplateScan(
data={"jsonb": "data"},
scan_type=scan_type_fixture,
template=template_fixture,
)
session.add(template_scan)
return template_scan
65 changes: 65 additions & 0 deletions api/tests/models/test_TemplateScanTrigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pytest

from sqlalchemy.exc import IntegrityError

from models.TemplateScanTrigger import TemplateScanTrigger


def test_template_scan_trigger_belongs_to_an_template_scan(
template_scan_fixture, session
):
template_scan_trigger = TemplateScanTrigger(
callback={"jsonb": "data"},
template_scan=template_scan_fixture,
)
session.add(template_scan_trigger)
session.commit()
assert (
template_scan_fixture.template_scan_triggers[0].id == template_scan_trigger.id
)
session.delete(template_scan_trigger)
session.commit()


def test_template_scan_trigger_model(template_scan_fixture):
template_scan_trigger = TemplateScanTrigger(
callback={"jsonb": "data"},
template_scan=template_scan_fixture,
)
assert template_scan_trigger.callback == {"jsonb": "data"}
assert template_scan_trigger.template_scan is not None


def test_template_scan_trigger_model_saved(
assert_new_model_saved, template_scan_fixture, session
):
template_scan_trigger = TemplateScanTrigger(
callback={"jsonb": "data"},
template_scan=template_scan_fixture,
)
session.add(template_scan_trigger)
session.commit()
assert template_scan_trigger.callback == {"jsonb": "data"}
assert_new_model_saved(template_scan_trigger)
session.delete(template_scan_trigger)
session.commit()


def test_template_scan_trigger_empty_data_fails(template_scan_fixture, session):
template_scan_trigger = TemplateScanTrigger(
template_scan=template_scan_fixture,
)
session.add(template_scan_trigger)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()


def test_template_scan_trigger_empty_template_scan_fails(session):
template_scan_trigger = TemplateScanTrigger(
callback={"jsonb": "data"},
)
session.add(template_scan_trigger)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()