53 lines
2.4 KiB
Python
53 lines
2.4 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
import _bootstrap # noqa: F401
|
|
|
|
from netbox_plugin_store.editors import EditorError, PluginConfigurationEditor, RequirementsEditor
|
|
|
|
|
|
class ConfigurationEditorTests(unittest.TestCase):
|
|
def test_atomic_change_backup_and_rollback(self):
|
|
with tempfile.TemporaryDirectory() as temp_name:
|
|
root = Path(temp_name)
|
|
config = root / "configuration.py"
|
|
original = "SECRET_KEY = 'unchanged'\nPLUGINS = [\n 'netbox_plugin_store',\n]\n"
|
|
config.write_text(original, encoding="utf-8")
|
|
editor = PluginConfigurationEditor(config, root / "backups", 5)
|
|
receipt = editor.set_enabled("netbox_example", True)
|
|
self.assertTrue(receipt.changed)
|
|
self.assertTrue(receipt.backup_path.is_file())
|
|
self.assertIn("'netbox_example'", config.read_text(encoding="utf-8"))
|
|
editor.rollback(receipt)
|
|
self.assertEqual(config.read_text(encoding="utf-8"), original)
|
|
|
|
def test_dynamic_plugins_assignment_is_rejected(self):
|
|
with tempfile.TemporaryDirectory() as temp_name:
|
|
root = Path(temp_name)
|
|
config = root / "configuration.py"
|
|
config.write_text("PLUGINS = load_plugins()\n", encoding="utf-8")
|
|
editor = PluginConfigurationEditor(config, root / "backups", 5)
|
|
with self.assertRaises(EditorError):
|
|
editor.set_enabled("netbox_example", True)
|
|
|
|
|
|
class RequirementsEditorTests(unittest.TestCase):
|
|
def test_pin_and_remove_preserve_other_lines(self):
|
|
with tempfile.TemporaryDirectory() as temp_name:
|
|
root = Path(temp_name)
|
|
requirements = root / "local_requirements.txt"
|
|
requirements.write_text("# managed manually\nother-package==2\n", encoding="utf-8")
|
|
editor = RequirementsEditor(requirements, root / "backups", 5)
|
|
line = "netbox-example @ https://store.example/example.whl#sha256=" + "a" * 64
|
|
editor.set_requirement("netbox-example", line)
|
|
text = requirements.read_text(encoding="utf-8")
|
|
self.assertIn(line, text)
|
|
self.assertIn("other-package==2", text)
|
|
editor.set_requirement("netbox-example", None)
|
|
self.assertNotIn("netbox-example", requirements.read_text(encoding="utf-8"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|