Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host web tests #642

Merged
merged 14 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,4 @@ unfixable = [
"__init__.py" = ["E402", "F401"]
"pycroft/model/_all.py" = ["F403"]
"helpers/interactive.py" = ["F403"]
"**/*.pyi" = ["F811"]
21 changes: 15 additions & 6 deletions tests/factories/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,21 @@ def property_grants(self):

class AdminPropertyGroupFactory(PropertyGroupFactory):
name = factory.Sequence(partial(_maybe_append_seq, prefix="Admin-Gruppe"))
granted = frozenset((
'user_show', 'user_change', 'user_mac_change',
'infrastructure_show', 'infrastructure_change',
'facilities_show', 'facilities_change',
'groups_show', 'groups_change_membership', 'groups_change',
))
granted = frozenset(
(
"user_show",
"user_change",
"user_mac_change",
"hosts_change",
"infrastructure_show",
"infrastructure_change",
"facilities_show",
"facilities_change",
"groups_show",
"groups_change_membership",
"groups_change",
)
)
permission_level = 10


Expand Down
168 changes: 168 additions & 0 deletions tests/frontend/test_host.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Copyright (c) 2015 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 flask import url_for

from pycroft.model.host import Host
from pycroft.model.user import User
from tests import factories as f

from .assertions import TestClient


@pytest.fixture(scope="module")
def client(module_test_client: TestClient) -> TestClient:
return module_test_client


@pytest.fixture(scope="module")
def owner(module_session) -> User:
return f.UserFactory()


@pytest.fixture(scope="module")
def host(module_session, owner) -> Host:
return f.HostFactory(owner=owner, room__patched_with_subnet=True)


@pytest.mark.usefixtures("admin_logged_in")
class TestHostDelete:
def test_delete_nonexistent_host(self, client):
client.assert_url_response_code(
url_for("host.host_delete", host_id=999), code=404
)

def test_host_delete_successful(self, session, client, host, owner):
with client.flashes_message("Host.*gelöscht", category="success"):
client.assert_url_redirects(
url_for("host.host_delete", host_id=host.id),
method="POST",
)
session.refresh(owner)
assert owner.hosts == []

def test_host_get_returns_form(self, client, host):
with client.renders_template("generic_form.html"):
client.assert_url_ok(url_for("host.host_delete", host_id=host.id))


@pytest.mark.usefixtures("admin_logged_in")
class TestHostEdit:
def test_edit_nonexistent_host(self, client):
client.assert_url_response_code(
url_for("host.host_edit", host_id=999), code=404
)

def test_edit_host_get(self, client, host):
with client.renders_template("generic_form.html"):
client.assert_url_ok(url_for("host.host_edit", host_id=host.id))

def test_post_without_data(self, client, host):
"""works because the room data is automatically derived from the host"""
# HTTP 200 OK although form invalid
client.assert_url_ok(
url_for("host.host_edit", host_id=host.id),
method="POST",
)

def test_post_with_data(self, client, host):
with client.flashes_message("Host.*bearbeitet", category="success"):
client.assert_url_redirects(
url_for("host.host_edit", host_id=host.id),
method="POST",
data={
"owner": host.owner.id,
"name": f"new-{host.name}",
"building": host.room.building.id,
"level": host.room.level,
"room_number": host.room.number,
},
)

def test_post_with_invalid_data(self, client, host):
client.assert_url_ok(
url_for("host.host_edit", host_id=host.id),
method="POST",
data={
"owner": host.owner.id,
"name": f"new-{host.name}",
"building": host.room.building.id,
"level": host.room.level,
"room_number": 999,
},
)


@pytest.mark.usefixtures("admin_logged_in")
class TestHostCreate:
def test_create_host_nonexistent_owner(self, client):
client.assert_url_response_code(
url_for("host.host_create", user_id=999), code=404
)

def test_create_host_get(self, client, owner):
with client.renders_template("generic_form.html"):
client.assert_url_ok(url_for("host.host_create", user_id=owner.id))

def test_create_host_post(self, session, client, owner, host):
with client.flashes_message("Host.*erstellt", category="success"):
client.assert_url_redirects(
url_for("host.host_create", user_id=owner.id),
method="POST",
data={
"name": "test-host",
"building": owner.room.building.id,
"level": owner.room.level,
"room_number": owner.room.number,
},
)
session.refresh(owner)
# assert len(owner.hosts) == 1
# assert owner.hosts[0].name == "test-host"
new_hosts = set(owner.hosts) - {host}
assert len(new_hosts) == 1
assert list(new_hosts)[0].name == "test-host"

def test_create_host_post_invalid_data(self, session, client, owner):
client.assert_url_ok(
url_for("host.host_create", user_id=owner.id),
method="POST",
data={
"name": "test-host",
"building": owner.room.building.id,
"level": owner.room.level,
"room_number": 999,
},
)
session.refresh(owner)
assert len(owner.hosts) == 1


def test_user_hosts(client, host):
resp = client.assert_url_ok(url_for("host.user_hosts_json", user_id=host.owner.id))

assert "items" in resp.json
items = resp.json["items"]
assert len(items) == 1
[item] = items
assert item["switch"]
assert item["port"]
assert item["id"] == host.id
assert len(item["actions"]) == 2


@pytest.fixture()
def host_without_room(session):
return f.HostFactory(room=None)


def test_user_host_without_room(client, host_without_room):
resp = client.assert_url_ok(
url_for("host.user_hosts_json", user_id=host_without_room.owner.id)
)
assert len(resp.json["items"]) == 1
[it] = resp.json["items"]
assert it["switch"] is None
assert it["port"] is None
154 changes: 154 additions & 0 deletions tests/frontend/test_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Copyright (c) 2015 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 flask import url_for

from pycroft.model.host import Host, Interface
from tests import factories as f

from .assertions import TestClient


@pytest.fixture(scope="module")
def client(module_test_client: TestClient) -> TestClient:
return module_test_client


@pytest.fixture(scope="module", autouse=True)
def host(module_session) -> Host:
return f.HostFactory(interface=None, room__patched_with_subnet=True)


@pytest.fixture(scope="module", autouse=True)
def interface(module_session, host) -> Interface:
return f.InterfaceFactory(host=host)


pytestmark = pytest.mark.usefixtures("admin_logged_in")


class TestInterfacesJson:
def test_interfaces_nonexistent_host(self, client):
client.assert_url_response_code(
url_for("host.host_interfaces_json", host_id=999), code=404
)

def test_interfaces_json(self, client, interface):
resp = client.assert_url_ok(
url_for("host.host_interfaces_json", host_id=interface.host.id)
)

assert "items" in resp.json
items = resp.json["items"]
assert len(items) == 1
[item] = items
assert item["id"] == interface.id
assert len(item["actions"]) == 2
assert item["ips"] != ""


def test_interface_table(client, interface):
with client.renders_template("host/interface_table.html"):
client.assert_url_ok(url_for("host.interface_table", host_id=interface.host_id))


@pytest.mark.usefixtures("session")
class TestInterfaceDelete:
def test_delete_nonexistent_interface(self, client):
client.assert_url_response_code(
url_for("host.interface_delete", interface_id=999), code=404
)

def test_delete_interface_get(self, client, interface):
with client.renders_template("generic_form.html"):
client.assert_url_ok(
url_for("host.interface_delete", interface_id=interface.id)
)

def test_delete_interface_post(self, session, client, interface, host):
with client.flashes_message("Interface.*gelöscht", category="success"):
client.assert_url_redirects(
url_for("host.interface_delete", interface_id=interface.id),
method="POST",
)
session.refresh(host)
assert interface not in host.interfaces


@pytest.mark.usefixtures("session")
class TestInterfaceEdit:
def test_edit_nonexistent_interface(self, client):
client.assert_url_response_code(
url_for("host.interface_edit", interface_id=999), code=404
)

def test_edit_interface_get(self, client, interface):
with client.renders_template("generic_form.html"):
client.assert_url_ok(
url_for("host.interface_edit", interface_id=interface.id)
)

def test_edit_interface_post_invalid_data(self, client, interface):
with client.renders_template("generic_form.html"):
client.assert_url_ok(
url_for("host.interface_edit", interface_id=interface.id),
data={"mac": "invalid"},
method="POST",
)

def test_edit_interface_success(self, session, client, interface):
with client.flashes_message("Interface.*bearbeitet", category="success"):
client.assert_url_redirects(
url_for("host.interface_edit", interface_id=interface.id),
method="POST",
data={"mac": "00:11:22:33:44:55", "name": "new name"},
)
session.refresh(interface)
assert interface.mac == "00:11:22:33:44:55"
assert interface.name == "new name"


@pytest.mark.usefixtures("session")
class TestInterfaceCreate:
def test_create_interface_nonexistent_host(self, client, host):
client.assert_url_response_code(
url_for("host.interface_create", host_id=999), code=404
)

def test_create_interface_get(self, client, host):
with client.renders_template("generic_form.html"):
client.assert_url_ok(url_for("host.interface_create", host_id=host.id))

def test_create_interface_post_invalid_data(self, client, host):
with client.renders_template("generic_form.html"):
client.assert_url_ok(
url_for("host.interface_create", host_id=host.id),
method="POST",
data={"mac": "invalid"},
)

def test_create_interface_success(self, client, host):
with client.flashes_message("Interface.*erstellt", category="success"):
client.assert_url_redirects(
url_for("host.interface_create", host_id=host.id),
method="POST",
data={"mac": "00:11:22:33:44:55", "name": "new name"},
)


class TestManufacturer:
def test_manufacturer_invalid_mac(self, client):
client.assert_url_response_code(
url_for("host.interface_manufacturer_json", mac="invalid"), code=400
)

def test_manufacturer_valid_mac(self, client, monkeypatch):
monkeypatch.setattr(
"web.blueprints.host.get_interface_manufacturer", lambda mac: "AG DSN"
)
resp = client.assert_url_ok(
url_for("host.interface_manufacturer_json", mac="00:11:22:33:44:55")
)
assert resp.json["manufacturer"] == "AG DSN"
Loading
Loading