Skip to content

Commit

Permalink
Merge pull request #549 from markkuleinio/fix-constraints
Browse files Browse the repository at this point in the history
Fix permissions contraints handling in case of list of dicts (fixes #465)
  • Loading branch information
abhi1693 authored Aug 28, 2023
2 parents e85daed + 3f4396b commit a0da422
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ def list_parser(key_name, list_item):
if len(v) and isinstance(v[0], dict) and "object_type" in v[0]:
v = [generic_list_parser(k, i) for i in v]
to_cache = list(v)
elif k == "constraints":
# Permissions constraints can be either dict or list
to_cache = copy.deepcopy(v)
else:
v = [list_parser(k, i) for i in v]
to_cache = list(v)
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/users/permission.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@
{
"username": "user1"
}
],
"constraints": [
{
"status": "active"
},
{
"region__name": "Europe"
}
]
}
9 changes: 9 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ def test_username(self, _):
user = permission.users[0]
self.assertEqual(str(user), "user1")

@patch(
"requests.sessions.Session.get",
return_value=Response(fixture="users/permission.json"),
)
def test_constraints(self, _):
permission = nb.permissions.get(1)
self.assertIsInstance(permission.constraints, list)
self.assertIsInstance(permission.constraints[0], dict)


class UnknownModelTestCase(unittest.TestCase):
"""This test validates that an unknown model is returned as Record object
Expand Down

0 comments on commit a0da422

Please sign in to comment.