Add code coverage. also, install dev depends from requirements-dev.txt
Some checks failed
Bandit / audit-runtime-security (3.11) (pull_request) Failing after 5m39s
Flake8 / audit (3.11) (pull_request) Successful in 10m3s
Mypy / audit-typing (3.11) (pull_request) Failing after 6m15s
pip-audit / audit-dependency-security (3.11) (pull_request) Successful in 11m14s
Pylint / audit-runtime-security (3.11) (pull_request) Failing after 5m50s
Python Coverage / test-and-coverage (3.11) (pull_request) Failing after 5m40s
Some checks failed
Bandit / audit-runtime-security (3.11) (pull_request) Failing after 5m39s
Flake8 / audit (3.11) (pull_request) Successful in 10m3s
Mypy / audit-typing (3.11) (pull_request) Failing after 6m15s
pip-audit / audit-dependency-security (3.11) (pull_request) Successful in 11m14s
Pylint / audit-runtime-security (3.11) (pull_request) Failing after 5m50s
Python Coverage / test-and-coverage (3.11) (pull_request) Failing after 5m40s
This commit is contained in:
@ -28,8 +28,8 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install --upgrade bandit
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-dev.txt
|
||||
|
||||
- name: Analyse code with Bandit
|
||||
run: |
|
||||
|
||||
@ -28,8 +28,8 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install --upgrade flake8
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-dev.txt
|
||||
- name: Analyse code with Flake8
|
||||
run: |
|
||||
flake8 $(git ls-files '*.py')
|
||||
|
||||
@ -28,8 +28,8 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install --upgrade mypy
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-dev.txt
|
||||
|
||||
- name: Analyse code with Mypy
|
||||
run: |
|
||||
|
||||
@ -30,8 +30,8 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install --upgrade pip-audit
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-dev.txt
|
||||
|
||||
- name: Check vulnerable components with pip-audit
|
||||
run: |
|
||||
|
||||
@ -28,8 +28,8 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install --upgrade pylint
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-dev.txt
|
||||
- name: Analyse code with Pylint
|
||||
run: |
|
||||
pylint $(git ls-files '*.py')
|
||||
|
||||
@ -29,11 +29,11 @@ jobs:
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
python -m pip install --upgrade pytest-cov
|
||||
pip install -r requirements-dev.txt
|
||||
|
||||
- name: Run tests with coverage
|
||||
run: |
|
||||
pytest --cov=./ --cov-report=term --cov-report=xml --cov-report=html --junitxml=report.xml
|
||||
pytest --cov=./ --cov-report=term --cov-report=xml --cov-report=html --junitxml=report.xml app
|
||||
|
||||
- name: Upload coverage artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -51,6 +51,7 @@ coverage.xml
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
report.xml
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
|
||||
91
app/test_iplookup.py
Normal file
91
app/test_iplookup.py
Normal file
@ -0,0 +1,91 @@
|
||||
'''
|
||||
Test ismijnverweg geolookup api
|
||||
'''
|
||||
import logging
|
||||
import random
|
||||
import re
|
||||
from operator import itemgetter
|
||||
|
||||
from faker import Faker
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from .main import app
|
||||
|
||||
# Set up logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Initialize Faker for generating test data
|
||||
fake = Faker()
|
||||
|
||||
# Create test client
|
||||
fake_ipv6 = fake.ipv6()
|
||||
client = TestClient(app, client=(fake_ipv6, 31337))
|
||||
|
||||
|
||||
def test_no_query():
|
||||
"""Test searching without a query parameter"""
|
||||
|
||||
response = client.get("/")
|
||||
|
||||
assert response.status_code == 200
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
assert results['ip'] == fake_ipv6
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
def test_single_query():
|
||||
"""Test searching with an ip address"""
|
||||
fake_ipv4 = fake.ipv4_public()
|
||||
|
||||
response = client.get(f"/{fake_ipv4}")
|
||||
|
||||
assert response.status_code == 200
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
assert results['ip'] == fake_ipv4
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
def test_multi_query():
|
||||
"""Test searching with an ip address"""
|
||||
fake_ips = [{'ip': fake.ipv6() if random.random() > 0.5 else fake.ipv4()}
|
||||
for _ in range(16)]
|
||||
|
||||
response = client.post("/", json=fake_ips)
|
||||
|
||||
assert response.status_code == 200
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
|
||||
for ip in map(itemgetter('ip'), results):
|
||||
assert ip in map(itemgetter('ip'), fake_ips)
|
||||
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
def test_invalid_query():
|
||||
"""Test searching with an invalid ip address"""
|
||||
invalid_ip = '500.312.77.31337'
|
||||
test_pattern = 'Input is not a valid IPv[46] address'
|
||||
|
||||
response = client.get(f"/{invalid_ip}")
|
||||
|
||||
assert response.status_code == 422
|
||||
results = response.json()
|
||||
logging.info(results)
|
||||
assert all(map(lambda x: x == invalid_ip, (
|
||||
map(itemgetter('input'), results['detail']))))
|
||||
assert all(map(lambda x: re.match(test_pattern, x), (
|
||||
map(itemgetter('msg'), results['detail']))))
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run tests
|
||||
test_no_query()
|
||||
test_single_query()
|
||||
test_invalid_query()
|
||||
test_multi_query()
|
||||
print("All tests passed!")
|
||||
5
requirements-dev.txt
Normal file
5
requirements-dev.txt
Normal file
@ -0,0 +1,5 @@
|
||||
faker==40.11.0
|
||||
flake8==7.3.0
|
||||
mypy==1.19.1
|
||||
pylint==4.0.5
|
||||
pytest-cov==7.0.0
|
||||
Reference in New Issue
Block a user