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

feat: add A11yReport model #101

Merged
merged 3 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,39 @@
"""create a11y_reports table

Revision ID: e251ec3b0f77
Revises: eb58f2b364a3
Create Date: 2021-08-24 19:49:08.946361

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

# revision identifiers, used by Alembic.
revision = "e251ec3b0f77"
down_revision = "eb58f2b364a3"
branch_labels = None
depends_on = None


def upgrade():
op.create_table(
"a11y_reports",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("scan_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("product", sa.String(), nullable=False),
sa.Column("revision", sa.String(), nullable=False),
sa.Column("url", sa.String()),
sa.Column("ci", sa.Boolean, unique=False, default=False),
sa.Column("summary", 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()),
sa.ForeignKeyConstraint(
["scan_id"],
["scans.id"],
),
)


def downgrade():
op.drop_table("a11y_reports")
53 changes: 53 additions & 0 deletions api/models/A11yReport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import datetime
import uuid

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

from models import Base
from models.Scan import Scan


class A11yReport(Base):
__tablename__ = "a11y_reports"

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
product = Column(String, nullable=False)
revision = Column(String, nullable=False)
url = Column(String)
ci = Column(Boolean, unique=False, default=False)
summary = 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,
)
scan_id = Column(
UUID(as_uuid=True), ForeignKey(Scan.id), index=True, nullable=False
)
scan = relationship("Scan", back_populates="a11y_reports")

@validates("product")
def validate_product(self, _key, value):
assert value != ""
return value

@validates("revision")
def validate_revision(self, _key, value):
assert value != ""
return value

@validates("summary")
def validate_summary(self, _key, value):
assert value is not None
return value
2 changes: 2 additions & 0 deletions api/models/Scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ class Scan(Base):
UUID(as_uuid=True), ForeignKey(ScanType.id), index=True, nullable=False
)
scan_type = relationship("ScanType", back_populates="scans")

a11y_reports = relationship("A11yReport")
2 changes: 1 addition & 1 deletion api/models/TemplateScan.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ class TemplateScan(Base):

@validates("data")
def validate_data(self, _key, value):
assert value != ""
assert value is not None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still catch "" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that just becomes an empty json string, which is not deal either. I will add both tests

return value
2 changes: 1 addition & 1 deletion api/models/TemplateScanTrigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class TemplateScanTrigger(Base):

@validates("callback")
def validate_callback(self, _key, value):
assert value != ""
assert value is not None
return value
12 changes: 12 additions & 0 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from alembic import command

from models.Organisation import Organisation
from models.Scan import Scan
from models.ScanType import ScanType
from models.Template import Template
from models.TemplateScan import TemplateScan
Expand Down Expand Up @@ -51,6 +52,17 @@ def setup_db():
yield


@pytest.fixture(scope="session")
def scan_fixture(scan_type_fixture, template_fixture, organisation_fixture, session):
scan = Scan(
organisation=organisation_fixture,
scan_type=scan_type_fixture,
template=template_fixture,
)
session.add(scan)
return scan


@pytest.fixture(scope="session")
def scan_type_fixture(session):
scan_type = ScanType(name="fixture_name")
Expand Down
124 changes: 124 additions & 0 deletions api/tests/models/test_A11yReport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import pytest

from sqlalchemy.exc import IntegrityError


from models.A11yReport import A11yReport


def test_a11y_report_belongs_to_an_scan(scan_fixture, session):
a11y_report = A11yReport(
product="product",
revision="revision",
url="url",
summary={"jsonb": "data"},
scan=scan_fixture,
)
session.add(a11y_report)
session.commit()
assert scan_fixture.a11y_reports[-1].id == a11y_report.id
session.delete(a11y_report)
session.commit()


def test_a11y_report_model(scan_fixture):
a11y_report = A11yReport(
product="product",
revision="revision",
url="url",
ci=True,
summary={"jsonb": "data"},
scan=scan_fixture,
)
assert a11y_report.product == "product"
assert a11y_report.revision == "revision"
assert a11y_report.url == "url"
assert a11y_report.ci is True
assert a11y_report.summary == {"jsonb": "data"}
assert a11y_report.scan is not None


def test_a11y_report_model_saved(assert_new_model_saved, scan_fixture, session):
a11y_report = A11yReport(
product="product",
revision="revision",
url="url",
summary={"jsonb": "data"},
scan=scan_fixture,
)
session.add(a11y_report)
session.commit()
assert a11y_report.product == "product"
assert a11y_report.revision == "revision"
assert a11y_report.url == "url"
assert_new_model_saved(a11y_report)
assert a11y_report.ci is False
assert a11y_report.summary == {"jsonb": "data"}
session.delete(a11y_report)
session.commit()


def test_a11y_report_empty_productfails(scan_fixture, session):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar version of these tests by product="" instead of just missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes those get caught:

    @validates("product")
    def validate_product(self, _key, value):
>       assert value != ""
E       AssertionError

a11y_report = A11yReport(
revision="revision",
url="url",
summary={"jsonb": "data"},
scan=scan_fixture,
)
session.add(a11y_report)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()


def test_a11y_report_empty_revision_fails(scan_fixture, session):
a11y_report = A11yReport(
product="product",
url="url",
summary={"jsonb": "data"},
scan=scan_fixture,
)
session.add(a11y_report)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()


def test_a11y_report_empty_url_passes(scan_fixture, session):
a11y_report = A11yReport(
product="product",
revision="revision",
summary={"jsonb": "data"},
scan=scan_fixture,
)
session.add(a11y_report)
session.commit()
assert a11y_report.url is None
session.delete(a11y_report)
session.commit()


def test_a11y_report_empty_summary_fails(scan_fixture, session):
a11y_report = A11yReport(
product="product",
revision="revision",
url="url",
scan=scan_fixture,
)
session.add(a11y_report)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()


def test_a11y_report_empty_scan_fails(session):
a11y_report = A11yReport(
product="product",
revision="revision",
url="url",
summary={"jsonb": "data"},
)
session.add(a11y_report)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()