batman (working version kinda)

This commit is contained in:
pika 2025-03-30 19:20:13 +02:00
commit 6dd38036e7
65 changed files with 3950 additions and 0 deletions

24
tests/conftest.py Normal file
View file

@ -0,0 +1,24 @@
import pytest
from app import create_app
from app.core.extensions import db as _db
@pytest.fixture
def app():
app = create_app('testing')
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
with app.app_context():
_db.create_all()
yield app
_db.session.remove()
_db.drop_all()
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def db(app):
with app.app_context():
yield _db

4
tests/pytest.ini Normal file
View file

@ -0,0 +1,4 @@
[pytest]
minversion = 6.0
cache_dir = .pytest_cache
addopts = --strict-markers

11
tests/test_app.py Normal file
View file

@ -0,0 +1,11 @@
import pytest
from app.core import models
# Example test
@pytest.mark.parametrize("cidr, expected", [
('192.168.1.0/24', 256),
('10.0.0.0/8', 16777216),
])
def test_subnet_cidr_parsing(cidr, expected):
subnet = models.Subnet(cidr=cidr)
assert subnet.used_ips == expected

51
tests/test_models.py Normal file
View file

@ -0,0 +1,51 @@
import pytest
from app.core.models import Subnet, Server, App
import ipaddress
def test_subnet_cidr_validation():
"""Test CIDR format validation for Subnet model"""
# Valid CIDR formats
valid_cidrs = [
'192.168.1.0/24',
'10.0.0.0/8',
'172.16.0.0/16'
]
for cidr in valid_cidrs:
subnet = Subnet(cidr=cidr, location='Test')
# This shouldn't raise an exception
ipaddress.ip_network(subnet.cidr)
# Invalid CIDR formats should raise ValueError
invalid_cidrs = [
'192.168.1.0', # Missing mask
'192.168.1.0/33', # Invalid mask
'256.0.0.0/24' # Invalid IP
]
for cidr in invalid_cidrs:
subnet = Subnet(cidr=cidr, location='Test')
with pytest.raises(ValueError):
ipaddress.ip_network(subnet.cidr)
def test_server_open_ports(app):
"""Test the get_open_ports method"""
# Create test server with app having specific ports
subnet = Subnet(cidr='192.168.1.0/24', location='Test')
server = Server(hostname='test-server', ip_address='192.168.1.10', subnet=subnet)
app1 = App(
name='Test App',
server=server,
ports=[
{'port': 80, 'type': 'tcp', 'status': 'open', 'desc': 'HTTP'},
{'port': 443, 'type': 'tcp', 'status': 'open', 'desc': 'HTTPS'},
{'port': 8080, 'type': 'tcp', 'status': 'closed', 'desc': 'Alt HTTP'}
]
)
# The get_open_ports should only return ports with status 'open'
open_ports = server.get_open_ports()
assert len(open_ports) == 2
assert {'port': 80, 'type': 'tcp', 'status': 'open', 'desc': 'HTTP'} in open_ports
assert {'port': 443, 'type': 'tcp', 'status': 'open', 'desc': 'HTTPS'} in open_ports
assert {'port': 8080, 'type': 'tcp', 'status': 'closed', 'desc': 'Alt HTTP'} not in open_ports

62
tests/test_routes.py Normal file
View file

@ -0,0 +1,62 @@
import pytest
from flask import url_for
def test_dashboard_home(client):
"""Test dashboard home route"""
response = client.get('/dashboard/')
assert response.status_code == 200
assert b"Dashboard" in response.data
def test_server_view(client, app):
"""Test server detail view"""
# Create test server
from app.core.models import Subnet, Server
from app.core.extensions import db
with app.app_context():
subnet = Subnet(cidr='192.168.1.0/24', location='Test')
server = Server(hostname='test-server', ip_address='192.168.1.10', subnet=subnet)
db.session.add(subnet)
db.session.add(server)
db.session.commit()
# Test viewing the server
response = client.get(f'/dashboard/server/{server.id}')
assert response.status_code == 200
assert b'test-server' in response.data
assert b'192.168.1.10' in response.data
def test_api_status(client):
"""Test API status endpoint"""
response = client.get('/api/status')
assert response.status_code == 200
json_data = response.get_json()
assert json_data['status'] == 'OK'
def test_markdown_preview(client):
"""Test markdown preview API"""
md_content = "# Test Heading\nThis is a test."
response = client.post('/api/markdown-preview',
json={'markdown': md_content})
assert response.status_code == 200
json_data = response.get_json()
assert '<h1>Test Heading</h1>' in json_data['html']
assert '<p>This is a test.</p>' in json_data['html']
def test_subnet_scan(client, app):
"""Test subnet scanning API"""
# Create test subnet
from app.core.models import Subnet
from app.core.extensions import db
with app.app_context():
subnet = Subnet(cidr='192.168.1.0/24', location='Test', auto_scan=True)
db.session.add(subnet)
db.session.commit()
# Test scanning endpoint
response = client.post(f'/ipam/subnet/{subnet.id}/scan')
assert response.status_code == 200
json_data = response.get_json()
assert json_data['status'] == 'scanning'
assert json_data['subnet_id'] == subnet.id