Skip to content

Commit

Permalink
Resolve issue with serializing default permissions (#10)
Browse files Browse the repository at this point in the history
* Add unit test to address observed permissions serialization issue
Update `PermissionsConfig` to ensure default values are properly serialized

* Remove added unused import

* Remove extraneous refactoring of permissions default value
  • Loading branch information
NeonDaniel authored Jan 6, 2025
1 parent ed7f3df commit fe040fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions neon_data_models/models/user/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class PermissionsConfig(BaseModel):

class Config:
use_enum_values = True
validate_default = True

@classmethod
def from_roles(cls, roles: List[str]):
Expand Down
14 changes: 13 additions & 1 deletion tests/models/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_neon_user_config(self):
self.assertIsInstance(config.user.dob, date)

def test_user(self):
from neon_data_models.enum import AccessRoles
user_kwargs = dict(username="test",
password_hash="test",
tokens=[{"token_name": "test_token",
Expand All @@ -98,6 +99,11 @@ def test_user(self):
"last_refresh_timestamp": round(time())}])
default_user = User(**user_kwargs)
self.assertIsInstance(default_user.tokens[0], HanaToken)

dumped = default_user.model_dump()
for perm in dumped['permissions']:
self.assertEqual(int, type(dumped['permissions'][perm]), perm)
self.assertIsInstance(AccessRoles(dumped['permissions'][perm]), AccessRoles, perm)
with self.assertRaises(ValidationError):
User()

Expand All @@ -124,8 +130,14 @@ def test_permissions_config(self):
node=AccessRoles.NODE,
hub=AccessRoles.NODE,
llm=AccessRoles.NONE)

# Test dumped enums
dumped = test_config.model_dump()
for key, value in dumped.items():
self.assertEqual(type(value), int, key)

# Test dump/load
self.assertEqual(PermissionsConfig(**test_config.model_dump()),
self.assertEqual(PermissionsConfig(**dumped),
test_config)

# Test to/from roles
Expand Down

0 comments on commit fe040fe

Please sign in to comment.