Skip to content

Commit

Permalink
added model test for mpsk
Browse files Browse the repository at this point in the history
added tests for the model of mpsk

Signed-off-by: Alex <robokatze182@gmx.de>
#744
  • Loading branch information
agmes4 committed Sep 17, 2024
1 parent 8b9efd8 commit 6b16064
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/factories/mpsk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2024. The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details
import factory
from tests.factories.base import BaseFactory
from pycroft.model.mpsk_client import MPSKClient

from tests.factories.user import UserFactory
from tests.factories.host import UnicastMacProvider

factory.Faker.add_provider(UnicastMacProvider)


class BareMPSKFactory(BaseFactory):
"""A host without owner or interface."""

class Meta:
model = MPSKClient

mac = factory.Faker("unicast_mac_address")
name = factory.Faker("name")


class MPSKFactory(BareMPSKFactory):

owner = factory.SubFactory(UserFactory)
81 changes: 81 additions & 0 deletions tests/model/test_mpsk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (c) 2024. The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details

import pytest
from packaging.utils import InvalidName
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.base import object_state

from pycroft.model.mpsk_client import MPSKClient
from pycroft.model.types import InvalidMACAddressException
from .. import factories


class TestMPSKValidators:

@pytest.fixture(scope="class")
def user(self, class_session):
user = factories.UserFactory.build(with_host=True)
class_session.add(user)
return user

@pytest.fixture(scope="class")
def mpsk_client(self, user):
return factories.MPSKFactory()

@pytest.mark.parametrize(
"mac",
[
"ff:ff:ff:ff:ff",
"ff:ff:ff:ff:ff:ff",
"ff:asjfjsdaf:ff:ff:ff:ff",
"aj:00:ff:1f:ff:ff",
"ff:ff:ff:ff:ff:ff:ff",
],
)
def test_bad_macs(self, session, user, mac):

mpsk_client = MPSKClient(
name="the needs of the many outweigh the needs of the few", owner=user
)
assert object_state(mpsk_client).transient
with pytest.raises(InvalidMACAddressException):
mpsk_client.mac = mac
with pytest.raises(IntegrityError):
session.add(mpsk_client)
session.flush()

def test_no_name(self, session, user):
mpsk_client = MPSKClient(mac="00:00:00:00:00:00", owner=user)
with pytest.raises(IntegrityError):
session.add(mpsk_client)
session.flush()
with pytest.raises(InvalidName):
MPSKClient(mac="00:00:00:00:00:00", name="", owner=user)

def test_no_mac(self, session, user):
mpsk_client = MPSKClient(name="00:00:00:00:00:00", owner=user)
with pytest.raises(IntegrityError):
session.add(mpsk_client)
session.flush()

def test_no_owner(self, session, user):
mpsk_client = MPSKClient(mac="00:00:00:00:00:00", name="as")
with pytest.raises(IntegrityError):
session.add(mpsk_client)
session.flush()

@pytest.mark.parametrize(
"name",
[
" a ",
" a",
"ff:asjfjsdaf:ff:ff:ff:ff",
"aj:00:ff:1f:ff:ff",
"ff:ff:ff:ff:ff:ff:ff",
],
)
def test_names(self, session, user, name):
mpsk_client = MPSKClient(mac="00:00:00:00:00:00", name=name, owner=user)
assert mpsk_client.name == name

0 comments on commit 6b16064

Please sign in to comment.