Refactor history sync into history_service and update docs/tests
This commit is contained in:
@@ -378,3 +378,58 @@ class TestGetIpDetail:
|
||||
assert result.country_name == "United States"
|
||||
assert result.asn == "AS15169"
|
||||
assert result.org == "Google"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# sync_from_fail2ban_db tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSyncFromFail2BanDb:
|
||||
async def test_archives_new_records_and_returns_count(self) -> None:
|
||||
from types import SimpleNamespace
|
||||
|
||||
fake_db = AsyncMock()
|
||||
fake_rows = [
|
||||
SimpleNamespace(
|
||||
jail="sshd",
|
||||
ip="1.2.3.4",
|
||||
timeofban=1000,
|
||||
bancount=1,
|
||||
data="{}",
|
||||
)
|
||||
]
|
||||
|
||||
with patch(
|
||||
"app.services.history_service._get_last_archive_ts",
|
||||
new=AsyncMock(return_value=1000),
|
||||
), patch(
|
||||
"app.services.history_service.get_fail2ban_db_path",
|
||||
new=AsyncMock(return_value="/tmp/fake.sqlite3"),
|
||||
), patch(
|
||||
"app.services.history_service.fail2ban_db_repo.get_history_page",
|
||||
new=AsyncMock(return_value=(fake_rows, 1)),
|
||||
) as mock_page, patch(
|
||||
"app.services.history_service.archive_ban_event",
|
||||
new=AsyncMock(return_value=True),
|
||||
) as archive_mock:
|
||||
count = await history_service.sync_from_fail2ban_db(
|
||||
fake_db, "/tmp/fake.sock"
|
||||
)
|
||||
|
||||
assert count == 1
|
||||
mock_page.assert_awaited_once_with(
|
||||
db_path="/tmp/fake.sqlite3",
|
||||
since=1001,
|
||||
page=1,
|
||||
page_size=500,
|
||||
)
|
||||
archive_mock.assert_awaited_once_with(
|
||||
db=fake_db,
|
||||
jail="sshd",
|
||||
ip="1.2.3.4",
|
||||
timeofban=1000,
|
||||
bancount=1,
|
||||
data="{}",
|
||||
action="ban",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user