Skip to content
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
2,010 changes: 1,018 additions & 992 deletions poetry.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ apscheduler = "^3.10.4"
asyncpg = "^0.29.0"
boto3 = "^1.34.122"
botocore = "^1.34.122"
flask = "^3.0.2"
flask = "^3.1.1"
gitpython = "^3.1.43"
connexion = {version = "~=3.1.0", extras = ["swagger-ui", "flask"]}
gunicorn = "^23.0.0"
peewee = "^3.17.1"
peewee = "3.17.9"
prometheus-client = "^0.20.0"
psycopg2 = "^2.9.9"
psycopg-pool = "^3.2.1"
py-flags = "^1.1.4"
pytz = "^2024.1"
pytz = "^2025.2"
requests = "^2.32.3"
watchtower = "^3.2.0"
psycopg = {version = "^3.1.19", extras = ["pool"]}
Expand All @@ -68,7 +68,7 @@ prometheus-async = "^25.0.0"
pre-commit = "^4.0.0"
pur = "^7.3.1"
pytest = "^8.3.4"
pytest-asyncio = "^0.25.1"
pytest-asyncio = "^1.0.0"
schema = "^0.7.4"
testing-postgresql = "^1.3.0"
python-box = "^7.0.0"
Expand Down
7 changes: 0 additions & 7 deletions tests/listener_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# -*- coding: utf-8 -*-
# pylint:disable=missing-docstring,redefined-outer-name
import asyncio

import psycopg2
import pytest


@pytest.fixture(scope="class")
def event_loop():
return asyncio.new_event_loop()


@pytest.fixture(scope="module")
def pg_db_conn(pg_db_mod):
"""Returns connection to PostgreSQL database."""
Expand Down
11 changes: 6 additions & 5 deletions tests/notificator_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Fixtures for notificator tests
"""

import asyncio

import asyncpg
import pytest
import pytest_asyncio
from aiohttp import test_utils
from aiohttp import web
Expand All @@ -13,20 +14,20 @@


@pytest_asyncio.fixture(loop_scope="function", scope="function")
async def asyncpg_pool(event_loop, pg_db_func):
async def asyncpg_pool(pg_db_func):
"""Creates asyncpg pool fixture"""
dsn = pg_db_func.dsn()
dsn_str = f"postgres://{dsn['user']}@{dsn['host']}:{dsn['port']}/{dsn['database']}"

pool = await asyncpg.create_pool(
dsn=dsn_str,
loop=event_loop,
loop=asyncio.get_running_loop(),
)
yield pool
await pool.close()


@pytest.fixture
@pytest_asyncio.fixture()
async def notificator_server():
"""Creates notificator app server"""
app = create_notificator_app(None, [(r"/api/v1/cache", CacheHandler)])
Expand All @@ -41,7 +42,7 @@ async def notificator_server():
await runner.cleanup()


@pytest.fixture()
@pytest_asyncio.fixture()
async def http_client(notificator_server, aiohttp_client):
"""Http client fixture"""
client = await aiohttp_client(notificator_server)
Expand Down
3 changes: 3 additions & 0 deletions tests/notificator_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import asyncio

import pytest

from notificator.notificator import NOTIFICATOR_PERIOD
from notificator.notificator import NotificationType
from notificator.notificator import NotificatorConditions
Expand All @@ -23,6 +25,7 @@ async def _init_notificator(self, pool):
await queue.init()
return conditions, queue

@pytest.mark.asyncio()
async def test_cache_handler(self, asyncpg_pool, http_client):
"""Test cache refresh handler"""
_, queue = await self._init_notificator(asyncpg_pool)
Expand Down
16 changes: 10 additions & 6 deletions tests/notificator_tests/test_notificator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Notificator unit tests
"""

import asyncio
import json

import pytest
Expand Down Expand Up @@ -65,9 +66,10 @@ async def _build_notificator(pool, loop):
return notificator

@staticmethod
@pytest.mark.asyncio
async def test_notificator_new(asyncpg_pool, event_loop, monkeypatch):
@pytest.mark.asyncio(loop_scope="function")
async def test_notificator_new(asyncpg_pool, monkeypatch):
"""Tests new sys_vulns from evaluator to notificator"""
event_loop = asyncio.get_running_loop()
# update CVE-2014-1, CVE-2017-1, CVE-2017-6 to be fresh
# leave out CVE-2016-1 to not be fresh because it has exploits
async with asyncpg_pool.acquire() as conn:
Expand Down Expand Up @@ -100,9 +102,10 @@ async def test_notificator_new(asyncpg_pool, event_loop, monkeypatch):
NotificatorQueue.delete()

@staticmethod
@pytest.mark.asyncio
async def test_notificator_mitigated(asyncpg_pool, event_loop, monkeypatch):
@pytest.mark.asyncio(loop_scope="function")
async def test_notificator_mitigated(asyncpg_pool, monkeypatch):
"""Tests mitigated sys_vulns from evaluator to notificator"""
event_loop = asyncio.get_running_loop()
notificator = await TestNotificator._build_notificator(asyncpg_pool, event_loop)
monkeypatch.setattr(json, "loads", lambda _: EVALUATOR_RESULT_MIT_VULNS)

Expand All @@ -121,9 +124,10 @@ async def test_notificator_mitigated(asyncpg_pool, event_loop, monkeypatch):
NotificatorQueue.delete()

@staticmethod
@pytest.mark.asyncio
async def test_notificator_unmitigated(asyncpg_pool, event_loop, monkeypatch):
@pytest.mark.asyncio(loop_scope="function")
async def test_notificator_unmitigated(asyncpg_pool, monkeypatch):
"""Tests unmitigated sys_vulns from evaluator to notificator"""
event_loop = asyncio.get_running_loop()
# update CVE-2014-1, CVE-2017-1, CVE-2017-6, CVE-2017-8 to be fresh
# leave out CVE-2016-1 to not be fresh because it has exploits
async with asyncpg_pool.acquire() as conn:
Expand Down
32 changes: 20 additions & 12 deletions tests/notificator_tests/test_notificator_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""
Notificator Queue unit tests
"""
import asyncio

import pytest

from common.peewee_model import NotificationType
Expand Down Expand Up @@ -33,9 +35,10 @@ async def _notified_users_cnt(pool):
return res[0]

@staticmethod
@pytest.mark.asyncio
async def test_queue_empty(asyncpg_pool, event_loop):
@pytest.mark.asyncio(loop_scope="function")
async def test_queue_empty(asyncpg_pool):
"""Tests queue single run when its empty"""
event_loop = asyncio.get_running_loop()
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)
await queue._process_queue()
# Queues should be empty
Expand All @@ -44,9 +47,10 @@ async def test_queue_empty(asyncpg_pool, event_loop):
NotificatorQueue.delete()

@staticmethod
@pytest.mark.asyncio
async def test_queue_nonexisting(asyncpg_pool, event_loop):
@pytest.mark.asyncio(loop_scope="function")
async def test_queue_nonexisting(asyncpg_pool):
"""Tests queue run with non existing sys_vuln"""
event_loop = asyncio.get_running_loop()
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)
# add nonexisting sys vuln to queue
queue.notif_cves_queue[1337] = QueueItem("CVE-NONEXISTING", 1337, 0, "0", [NotificationType.CVSS_NOTIFICATION])
Expand All @@ -60,9 +64,10 @@ async def test_queue_nonexisting(asyncpg_pool, event_loop):
NotificatorQueue.delete()

@staticmethod
@pytest.mark.asyncio
async def test_queue_non_advisor_eval(asyncpg_pool, event_loop):
@pytest.mark.asyncio(loop_scope="function")
async def test_queue_non_advisor_eval(asyncpg_pool):
"""Tests queue run with not yet advisor evaluated system"""
event_loop = asyncio.get_running_loop()
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)

# insert into db system which is not advisor evaluated and has single CVE
Expand All @@ -88,9 +93,10 @@ async def test_queue_non_advisor_eval(asyncpg_pool, event_loop):
NotificatorQueue.delete()

@staticmethod
@pytest.mark.asyncio
async def test_queue_mitigated(asyncpg_pool, event_loop):
@pytest.mark.asyncio(loop_scope="function")
async def test_queue_mitigated(asyncpg_pool):
"""Test queue run but cve gets mitigated"""
event_loop = asyncio.get_running_loop()
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)

# insert into db system which is not vulnerable anymore, based on the advisor listener
Expand All @@ -117,9 +123,10 @@ async def test_queue_mitigated(asyncpg_pool, event_loop):
NotificatorQueue.delete()

@staticmethod
@pytest.mark.asyncio
async def test_queue_valid(asyncpg_pool, event_loop):
@pytest.mark.asyncio(loop_scope="function")
async def test_queue_valid(asyncpg_pool):
"""Test queue run with valid system vulnerabilities, which should be notified"""
event_loop = asyncio.get_running_loop()
msgs = []

def _send_kafka_notif_mock(self, acc_id, event_type, *_, **__):
Expand Down Expand Up @@ -156,9 +163,10 @@ def _send_kafka_notif_mock(self, acc_id, event_type, *_, **__):
NotificatorQueue.delete()

@staticmethod
@pytest.mark.asyncio
async def test_queue_unknown_cve(asyncpg_pool, event_loop):
@pytest.mark.asyncio(loop_scope="function")
async def test_queue_unknown_cve(asyncpg_pool):
"""Test queue run with unknown cve vulnerabilities yet"""
event_loop = asyncio.get_running_loop()
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)

# insert into db system which vulnerable to CVE which vulnerability does not know yet
Expand Down
5 changes: 3 additions & 2 deletions tests/taskomatic_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint:disable=missing-docstring,redefined-outer-name
import psycopg2
import pytest
import pytest_asyncio
from aiohttp import test_utils
from aiohttp import web

Expand All @@ -27,7 +28,7 @@ def db_cleanup():
request.addfinalizer(db_cleanup)


@pytest.fixture
@pytest_asyncio.fixture()
async def taskomatic_server():
app = taskomatic.TaskomaticApp()
runner = web.AppRunner(app.app)
Expand All @@ -41,7 +42,7 @@ async def taskomatic_server():
await runner.cleanup()


@pytest.fixture()
@pytest_asyncio.fixture()
async def http_client(taskomatic_server, aiohttp_client):
client = await aiohttp_client(taskomatic_server)
yield client
Expand Down
3 changes: 3 additions & 0 deletions tests/taskomatic_tests/test_taskomatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import asyncio

import pytest
from psycopg2.extensions import connection

import taskomatic.jobs.common as tjc
Expand Down Expand Up @@ -90,11 +91,13 @@ def run_forever(self, *_, **__):
class TestTaskomaticApp:
"""Test taskomatic App handlers"""

@pytest.mark.asyncio
async def test_run_job(self, http_client):
"""Test run job handler"""
response = await http_client.put("/api/v1/run/stale_systems", data={})
assert response.status == 200

@pytest.mark.asyncio
async def test_run_job_wrong(self, http_client):
"""Test run job handler with wrong job"""
response = await http_client.put("/api/v1/run/nonexisting_job", data={})
Expand Down