-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
196 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""empty message | ||
Revision ID: 5df43dc932dd | ||
Revises: c27fec1bfee2 | ||
Create Date: 2024-07-23 14:49:17.928909 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "5df43dc932dd" | ||
down_revision = "c27fec1bfee2" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"cve", sa.Column("numerical_id", sa.Numeric(), nullable=True) | ||
) | ||
op.create_index( | ||
op.f("ix_cve_numerical_id"), "cve", ["numerical_id"], unique=False | ||
) | ||
op.drop_index("notices_published_desc_idx", table_name="notice") | ||
op.drop_index("notices_published_idx", table_name="notice") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_index( | ||
"notices_published_idx", "notice", ["published"], unique=False | ||
) | ||
op.create_index( | ||
"notices_published_desc_idx", | ||
"notice", | ||
[sa.text("published DESC")], | ||
unique=False, | ||
) | ||
op.drop_index(op.f("ix_cve_numerical_id"), table_name="cve") | ||
op.drop_column("cve", "numerical_id") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Standard library | ||
from contextlib import redirect_stderr | ||
import io | ||
import os | ||
import unittest | ||
import warnings | ||
|
||
# Packages | ||
from sqlalchemy_utils import database_exists, create_database | ||
import flask_migrate | ||
|
||
|
||
# Local | ||
from tests.fixtures.models import make_models | ||
|
||
""" | ||
Monkey-patching before importing the main application | ||
=== | ||
Get the database connection string from the TEST_DATABASE_URL environment | ||
variable. This variabel is required, as it's important not to accidentally | ||
wipe out a real database. | ||
Replace the authorization_required view decorator with a transparent function | ||
to disable authorization checks for testing privileged views. | ||
This is not ideal, as it means we're not testing the actual authorization | ||
functionality, but I don't know of a good way to do that right now. | ||
""" | ||
|
||
from webapp import auth | ||
from tests.helpers import transparent_decorator | ||
|
||
auth.authorization_required = transparent_decorator | ||
os.environ["DATABASE_URL"] = os.environ["TEST_DATABASE_URL"] | ||
|
||
from webapp.app import app, db # noqa: E402 | ||
|
||
|
||
# Create database if it doesn't exist | ||
with app.app_context(): | ||
if not database_exists(db.engine.url): | ||
create_database(db.engine.url) | ||
|
||
|
||
# Suppress annoying ResourceWarnings | ||
warnings.filterwarnings(action="ignore", category=ResourceWarning) | ||
|
||
|
||
class BaseTestCase(unittest.TestCase): | ||
def setUp(self): | ||
app.testing = True | ||
|
||
# Set up app context | ||
self.context = app.app_context() | ||
self.context.push() | ||
|
||
# Clear DB | ||
db.drop_all() | ||
with redirect_stderr(io.StringIO()): | ||
flask_migrate.stamp(revision="base") | ||
|
||
# Prepare DB | ||
with redirect_stderr(io.StringIO()): | ||
flask_migrate.upgrade() | ||
|
||
# Import data | ||
self.models = make_models() | ||
db.session.add(self.models["cve"]) | ||
db.session.add(self.models["notice"]) | ||
db.session.add(self.models["release"]) | ||
db.session.add(self.models["package"]) | ||
db.session.add(self.models["status"]) | ||
db.session.commit() | ||
|
||
self.client = app.test_client() | ||
return super().setUp() | ||
|
||
def tearDown(self): | ||
db.session.close() | ||
|
||
self.context.pop() | ||
|
||
return super().tearDown() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from click.testing import CliRunner | ||
import unittest | ||
|
||
from tests import BaseTestCase, db | ||
from webapp.commands import insert_numerical_cve_ids | ||
from webapp.models import CVE | ||
|
||
|
||
class TestCommands(BaseTestCase): | ||
runner = CliRunner() | ||
|
||
def test_upsert_numerical_cve_ids(self): | ||
""" | ||
Numerical CVE ids should be inserted correctly | ||
""" | ||
result = self.runner.invoke(insert_numerical_cve_ids) | ||
assert result.exit_code == 0 | ||
|
||
# Check that the numerical cve id was inserted | ||
new_cve = ( | ||
db.session.query(CVE).filter(CVE.id == "CVE-1111-0001").first() | ||
) | ||
assert new_cve.numerical_id == int("11110001") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import click | ||
from webapp.app import app | ||
from webapp.models import ( | ||
upsert_numerical_cve_ids, | ||
) | ||
|
||
|
||
@app.cli.command("insert-numerical-cve-ids") | ||
def insert_numerical_cve_ids(): | ||
"""Management script for the Wiki application.""" | ||
upsert_numerical_cve_ids() | ||
click.echo("Numerical CVE ids inserted successfully.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters