Skip to content

Commit

Permalink
Add unit test to address observed permissions serialization issue
Browse files Browse the repository at this point in the history
Update `PermissionsConfig` to ensure default values are properly serialized
  • Loading branch information
NeonDaniel committed Dec 27, 2024
1 parent ed7f3df commit f56f632
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions neon_data_models/models/user/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

# from neon_data_models.models.api import HanaToken
from neon_data_models.models.base import BaseModel
from pydantic import Field
from pydantic import Field, ConfigDict
from datetime import date

from neon_data_models.enum import AccessRoles
Expand Down Expand Up @@ -126,7 +126,8 @@ class PermissionsConfig(BaseModel):
description="Defines access to DIANA backend services. "
"(i.e. API proxy, email proxy).")
users: AccessRoles = Field(
AccessRoles.NONE, description="Defines access to the users service.")
default_factory=lambda: AccessRoles.NONE,
description="Defines access to the users service.")
node: AccessRoles = Field(
AccessRoles.NONE,
description="Defines access to the node websocket in HANA.")
Expand All @@ -140,6 +141,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 f56f632

Please sign in to comment.