-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for the model of mpsk Signed-off-by: Alex <robokatze182@gmx.de> #744
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |