From e9b50fd26a30bdfb5ec7369ce74969af56d6fb78 Mon Sep 17 00:00:00 2001 From: Lukas Juhrich Date: Sat, 31 Aug 2019 23:28:36 +0200 Subject: [PATCH] Add most factories and a POC test for model creation Refs #330 --- tests/factories/nat.py | 55 +++++++++++++++++++++++++++++++++++++++++ tests/model/test_nat.py | 37 +++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/tests/factories/nat.py b/tests/factories/nat.py index e69de29bb..a3e5ad988 100644 --- a/tests/factories/nat.py +++ b/tests/factories/nat.py @@ -0,0 +1,55 @@ +from factory import Sequence, SubFactory, Faker + +from pycroft.model.nat import NATDomain, OutsideIPAddress +from pycroft.model.nat import InsideNetwork, Translation, Forwarding, \ + DHCPHostReservation +from .base import BaseFactory + + +class NatDomainFactory(BaseFactory): + class Meta: + model = NATDomain + + name = Sequence(lambda n: f"NAT Domain {n + 1}") + + +class OutsideIpAddressFactory(BaseFactory): + class Meta: + model = OutsideIPAddress + + nat_domain = SubFactory(NatDomainFactory) + ip_address = Faker('ipv4', network=False) + owner = None + + +class InsideNetworkFactory(BaseFactory): + # TODO this should be filled with all available inside networks + class Meta: + model = InsideNetwork + + nat_domain = SubFactory(NatDomainFactory) + ip_network = Faker('ipv4', network=True) + gateway = Faker('ipv4', network=False) + + +class TranslationFactory(BaseFactory): + class Meta: + model = Translation + + pass + + +class ForwardingFactory(BaseFactory): + class Meta: + model = Forwarding + + pass + + +class DhcpHostReservationFactory(BaseFactory): + class Meta: + model = DHCPHostReservation + + pass + + diff --git a/tests/model/test_nat.py b/tests/model/test_nat.py index e69de29bb..894748b2d 100644 --- a/tests/model/test_nat.py +++ b/tests/model/test_nat.py @@ -0,0 +1,37 @@ +from typing import List + +from pycroft.model.nat import NATDomain +from tests import FactoryDataTestBase +from tests.factories import ConfigFactory, UserFactory +from tests.factories.nat import NatDomainFactory, OutsideIpAddressFactory + + +class NatTestBase(FactoryDataTestBase): + def create_factories(self): + ConfigFactory.create() + self.nat_domain = NatDomainFactory() + + +class TrivialNatModelTestCase(NatTestBase): + def create_factories(self): + super().create_factories() + + def test_one_natdomain(self): + doms: List[NATDomain] = NATDomain.q.all() + self.assertEqual(len(doms), 1) + dom = doms[0] + self.assertEqual(dom.name, self.nat_domain.name) + + +class SimpleNatModelTestCase(NatTestBase): + def create_factories(self): + super().create_factories() + self.user = UserFactory.create() + self.outside_ip = OutsideIpAddressFactory(owner=self.user, + nat_domain=self.nat_domain) + # TODO later check that this doesn't work because every outside_ip needs a translation + # TODO add a pool of inside networks + + def test_forwardings_correct(self): + domain: NATDomain = NATDomain.q.one() + self.assertEqual(domain.name, self.nat_domain.name)