feat: add test framework, unit tests, and GitLab CI pipeline

- Add pytest config in backend/pyproject.toml with asyncio mode and coverage
- Add backend/requirements-test.txt with pytest, pytest-cov, pytest-asyncio, aiosqlite
- Write 86 backend unit tests covering auth (JWT tokens), schemas (Pydantic
  models), config (Settings env overrides), log_parser (log parsing, IP
  resolution, geo data), models (SQLAlchemy table definitions + column
  defaults), and theme export
- Add Vitest config for Angular frontend (vitest.config.ts, src/test-setup.ts)
- Add .gitlab-ci.yml with stages: backend-test, frontend-test, backend-lint,
  docker-build, and quality-gate
- CI enforces 10% minimum coverage threshold for backend
This commit is contained in:
Hermes Agent
2026-07-29 08:34:42 +00:00
parent 8d233b17a5
commit 501df79e5c
12 changed files with 887 additions and 0 deletions
+140
View File
@@ -0,0 +1,140 @@
"""Tests for log_parser — IP resolution, log line parsing, and geo lookups."""
import os
import tempfile
import pytest
from app.log_parser import parse_log_line, resolve_client_ip, _COUNTRY_CENTROIDS
class TestParseLogLine:
"""Tests for parse_log_line()."""
def test_valid_json_line(self):
line = '{"remote_addr": "192.168.1.1", "method": "GET"}'
result = parse_log_line(line)
assert result is not None
assert result["remote_addr"] == "192.168.1.1"
assert result["method"] == "GET"
def test_empty_line_returns_none(self):
assert parse_log_line("") is None
def test_whitespace_only_returns_none(self):
assert parse_log_line(" ") is None
def test_invalid_json_returns_none(self):
assert parse_log_line("not json at all") is None
def test_partial_json_returns_none(self):
assert parse_log_line('{"remote_addr": ') is None
def test_strips_whitespace(self):
line = ' {"key": "value"} \n'
result = parse_log_line(line)
assert result is not None
assert result["key"] == "value"
class TestResolveClientIp:
"""Tests for resolve_client_ip()."""
def test_remote_addr_without_xff(self):
entry = {"remote_addr": "203.0.113.5"}
assert resolve_client_ip(entry) == "203.0.113.5"
def test_xff_single_ip(self):
entry = {
"remote_addr": "10.0.0.1",
"x_forwarded_for": "203.0.113.50",
}
assert resolve_client_ip(entry) == "203.0.113.50"
def test_xff_multiple_ips_returns_leftmost(self):
entry = {
"remote_addr": "10.0.0.1",
"x_forwarded_for": "203.0.113.100, 10.1.2.3, 172.16.0.5",
}
assert resolve_client_ip(entry) == "203.0.113.100"
def test_xff_dash_falls_back_to_remote_addr(self):
entry = {"remote_addr": "192.168.0.1", "x_forwarded_for": "-"}
assert resolve_client_ip(entry) == "192.168.0.1"
def test_missing_fields_returns_empty(self):
entry = {}
assert resolve_client_ip(entry) == ""
def test_xff_with_whitespace(self):
entry = {
"remote_addr": "10.0.0.1",
"x_forwarded_for": " 203.0.113.75 ",
}
assert resolve_client_ip(entry) == "203.0.113.75"
class TestCountryCentroids:
"""Tests for _COUNTRY_CENTROIDS data integrity."""
def test_centroids_are_float_tuples(self):
for code, coords in _COUNTRY_CENTROIDS.items():
assert isinstance(coords, tuple)
assert len(coords) == 2
assert isinstance(coords[0], float)
assert isinstance(coords[1], float)
def test_known_countries_present(self):
expected = {"US", "GB", "DE", "FR", "JP", "AU", "BR", "CA", "IN"}
assert expected.issubset(_COUNTRY_CENTROIDS.keys())
def test_centroid_values_are_reasonable(self):
"""Latitudes should be in [-90, 90], longitudes in [-180, 180]."""
for code, (lat, lon) in _COUNTRY_CENTROIDS.items():
assert -90 <= lat <= 90, f"{code} latitude {lat} out of range"
assert -180 <= lon <= 180, f"{code} longitude {lon} out of range"
class TestParseLogFile:
"""Tests for parse_log_file() — reads from real temp files."""
def test_parses_multiple_lines(self, tmp_path):
log_content = (
'{"remote_addr": "1.2.3.4"}\n'
'{"remote_addr": "5.6.7.8"}\n'
'{"remote_addr": "1.2.3.4"}\n'
)
log_file = tmp_path / "test.log"
log_file.write_text(log_content)
from app.log_parser import parse_log_file
result = parse_log_file(str(log_file))
# Should have 2 unique IPs (1.2.3.4 appears twice)
assert "1.2.3.4" in result["unique_ips"]
assert "5.6.7.8" in result["unique_ips"]
assert len(result["unique_ips"]) == 2
def test_skips_invalid_lines(self, tmp_path):
log_content = (
'{"remote_addr": "1.2.3.4"}\n'
'not valid json\n'
'\n'
'{"remote_addr": "5.6.7.8"}\n'
)
log_file = tmp_path / "test.log"
log_file.write_text(log_content)
from app.log_parser import parse_log_file
result = parse_log_file(str(log_file))
assert len(result["unique_ips"]) == 2
def test_xff_takes_precedence(self, tmp_path):
log_content = (
'{"remote_addr": "10.0.0.1", "x_forwarded_for": "203.0.113.1"}\n'
)
log_file = tmp_path / "test.log"
log_file.write_text(log_content)
from app.log_parser import parse_log_file
result = parse_log_file(str(log_file))
assert "203.0.113.1" in result["unique_ips"]
assert "10.0.0.1" not in result["unique_ips"]