51 lines
No EOL
1.8 KiB
Python
51 lines
No EOL
1.8 KiB
Python
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 |