From 2e73fd76dae2a66734b77144e0e1f4714acc9e9c Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Wed, 13 Dec 2023 13:51:53 +0100 Subject: [PATCH] Test set_editable_flag_on_interfaces() --- tests/unittests/web/portadmin/__init__.py | 0 tests/unittests/web/portadmin/utils_test.py | 39 +++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/unittests/web/portadmin/__init__.py create mode 100644 tests/unittests/web/portadmin/utils_test.py diff --git a/tests/unittests/web/portadmin/__init__.py b/tests/unittests/web/portadmin/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unittests/web/portadmin/utils_test.py b/tests/unittests/web/portadmin/utils_test.py new file mode 100644 index 0000000000..84ccff0824 --- /dev/null +++ b/tests/unittests/web/portadmin/utils_test.py @@ -0,0 +1,39 @@ +from unittest.mock import patch, Mock + +from nav.web.portadmin.utils import set_editable_flag_on_interfaces + + +class TestSetEditableFlagOnInterfaces: + def test_when_user_is_admin_it_should_set_all_interfaces_to_editable(self): + with patch( + "nav.web.portadmin.utils.should_check_access_rights", return_value=False + ): + mock_admin = Mock() + mock_interfaces = [Mock(iseditable=False)] * 3 + set_editable_flag_on_interfaces(mock_interfaces, [], mock_admin) + + assert all(ifc.iseditable for ifc in mock_interfaces) + + def test_when_user_is_not_admin_it_should_set_only_matching_interfaces_to_editable( + self, + ): + with patch( + "nav.web.portadmin.utils.should_check_access_rights", return_value=True + ): + mock_user = Mock() + mock_vlans = [Mock(vlan=42), Mock(vlan=69), Mock(vlan=666)] + editable_interface = Mock(vlan=666, iseditable=False) + mock_interfaces = [ + Mock(vlan=99, iseditable=False), + editable_interface, + Mock(vlan=27, iseditable=False), + ] + + set_editable_flag_on_interfaces(mock_interfaces, mock_vlans, mock_user) + + assert editable_interface.iseditable + assert all( + not ifc.iseditable + for ifc in mock_interfaces + if ifc is not editable_interface + )