diff --git a/pynetbox/core/endpoint.py b/pynetbox/core/endpoint.py index dfd989e..a0f7bef 100644 --- a/pynetbox/core/endpoint.py +++ b/pynetbox/core/endpoint.py @@ -587,9 +587,10 @@ def choices(self): token=self.api.token, http_session=self.api.http_session, ).options() - try: - post_data = req["actions"]["POST"] - except KeyError: + + actions = req.get("actions", {}) + post_data = actions.get("POST") or actions.get("PUT") + if post_data is None: raise ValueError( "Unexpected format in the OPTIONS response at {}".format(self.url) ) diff --git a/tests/unit/test_endpoint.py b/tests/unit/test_endpoint.py index 344b116..fe6a495 100644 --- a/tests/unit/test_endpoint.py +++ b/tests/unit/test_endpoint.py @@ -60,6 +60,59 @@ def test_choices(self): self.assertEqual(choices["letter"][1]["display_name"], "B") self.assertEqual(choices["letter"][1]["value"], 2) + def test_choices_put(self): + with patch("pynetbox.core.query.Request.options", return_value=Mock()) as mock: + api = Mock(base_url="http://localhost:8000/api") + app = Mock(name="test") + mock.return_value = { + "actions": { + "PUT": { + "letter": { + "choices": [ + {"display_name": "A", "value": 1}, + {"display_name": "B", "value": 2}, + {"display_name": "C", "value": 3}, + ] + } + } + } + } + test_obj = Endpoint(api, app, "test") + choices = test_obj.choices() + self.assertEqual(choices["letter"][0]["display_name"], "A") + self.assertEqual(choices["letter"][0]["value"], 1) + + def test_choices_precedence(self): + with patch("pynetbox.core.query.Request.options", return_value=Mock()) as mock: + api = Mock(base_url="http://localhost:8000/api") + app = Mock(name="test") + mock.return_value = { + "actions": { + "POST": { + "letter": { + "choices": [ + {"display_name": "A", "value": 1}, + {"display_name": "B", "value": 2}, + {"display_name": "C", "value": 3}, + ] + } + }, + "PUT": { + "letter": { + "choices": [ + {"display_name": "D", "value": 4}, + {"display_name": "E", "value": 5}, + {"display_name": "F", "value": 6}, + ] + } + }, + } + } + test_obj = Endpoint(api, app, "test") + choices = test_obj.choices() + self.assertEqual(choices["letter"][2]["display_name"], "C") + self.assertEqual(choices["letter"][2]["value"], 3) + def test_get_with_filter(self): with patch( "pynetbox.core.query.Request._make_call", return_value=Mock()