1import unittest
2
3import six
4
5import pynetbox
6
7if six.PY3:
8    from unittest.mock import patch
9else:
10    from mock import patch
11
12host = "http://localhost:8000"
13
14def_kwargs = {
15    "token": "abc123",
16}
17
18
19class AppCustomChoicesTestCase(unittest.TestCase):
20    @patch(
21        "pynetbox.core.query.Request.get",
22        return_value={
23            "Testfield1": {"TF1_1": 1, "TF1_2": 2},
24            "Testfield2": {"TF2_1": 3, "TF2_2": 4},
25        },
26    )
27    def test_custom_choices(self, *_):
28        api = pynetbox.api(host, **def_kwargs)
29        choices = api.extras.custom_choices()
30        self.assertEqual(len(choices), 2)
31        self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])
32
33
34class AppConfigTestCase(unittest.TestCase):
35    @patch(
36        "pynetbox.core.query.Request.get",
37        return_value={
38            "tables": {
39                "DeviceTable": {
40                    "columns": [
41                        "name",
42                        "status",
43                        "tenant",
44                        "tags",
45                    ],
46                },
47            },
48        },
49    )
50    def test_config(self, *_):
51        api = pynetbox.api(host, **def_kwargs)
52        config = api.users.config()
53        self.assertEqual(sorted(config.keys()), ["tables"])
54        self.assertEqual(
55            sorted(config["tables"]["DeviceTable"]["columns"]),
56            ["name", "status", "tags", "tenant"],
57        )
58
59
60class PluginAppCustomChoicesTestCase(unittest.TestCase):
61    @patch(
62        "pynetbox.core.query.Request.get",
63        return_value={
64            "Testfield1": {"TF1_1": 1, "TF1_2": 2},
65            "Testfield2": {"TF2_1": 3, "TF2_2": 4},
66        },
67    )
68    def test_custom_choices(self, *_):
69        api = pynetbox.api(host, **def_kwargs)
70        choices = api.plugins.test_plugin.custom_choices()
71        self.assertEqual(len(choices), 2)
72        self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])
73
74    @patch(
75        "pynetbox.core.query.Request.get",
76        return_value=[
77            {
78                "name": "test_plugin",
79                "package": "netbox_test_plugin",
80            }
81        ],
82    )
83    def test_installed_plugins(self, *_):
84        api = pynetbox.api(host, **def_kwargs)
85        plugins = api.plugins.installed_plugins()
86        self.assertEqual(len(plugins), 1)
87        self.assertEqual(plugins[0]["name"], "test_plugin")
88
89    def test_plugin_app_name(self, *_):
90        api = pynetbox.api(host, **def_kwargs)
91        test_plugin = api.plugins.test_plugin
92        self.assertEqual(test_plugin.name, "plugins/test-plugin")
93