Skip to content

Commit 3853ad8

Browse files
authored
Merge pull request #110 from onelogin/copilot/fix-7dfdb782-e53a-4e01-bdb9-5a6a639884ab
Add custom_attributes field support to User model
2 parents 384d5d9 + cb5ae4d commit 3853ad8

File tree

3 files changed

+125
-3
lines changed

3 files changed

+125
-3
lines changed

docs/User.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Name | Type | Description | Notes
4040
**password_confirmation** | **str** | Required if the password is being set. | [optional]
4141
**password_algorithm** | **str** | Use this when importing a password that's already hashed. Prepend the salt value to the cleartext password value before SHA-256-encoding it | [optional]
4242
**salt** | **str** | The salt value used with the password_algorithm. | [optional]
43+
**custom_attributes** | **Dict[str, Any]** | Custom user attributes defined in your OneLogin account. | [optional]
4344

4445
## Example
4546

onelogin/models/user.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import json
1414

1515

16-
from typing import List, Optional
16+
from typing import Any, Dict, List, Optional
1717
from pydantic import BaseModel, Field, StrictInt, StrictStr, conlist, field_validator
1818

1919
class User(BaseModel):
@@ -56,7 +56,8 @@ class User(BaseModel):
5656
password_confirmation: Optional[StrictStr] = Field(None, description="Required if the password is being set.")
5757
password_algorithm: Optional[StrictStr] = Field(None, description="Use this when importing a password that's already hashed. Prepend the salt value to the cleartext password value before SHA-256-encoding it")
5858
salt: Optional[StrictStr] = Field(None, description="The salt value used with the password_algorithm.")
59-
__properties = ["id", "username", "email", "firstname", "lastname", "title", "department", "company", "comment", "group_id", "role_ids", "phone", "state", "status", "directory_id", "trusted_idp_id", "manager_ad_id", "manager_user_id", "samaccountname", "member_of", "userprincipalname", "distinguished_name", "external_id", "activated_at", "last_login", "invitation_sent_at", "updated_at", "preferred_locale_code", "created_at", "invalid_login_attempts", "locked_until", "password_changed_at", "password", "password_confirmation", "password_algorithm", "salt"]
59+
custom_attributes: Optional[Dict[str, Any]] = Field(None, description="Custom user attributes defined in your OneLogin account.")
60+
__properties = ["id", "username", "email", "firstname", "lastname", "title", "department", "company", "comment", "group_id", "role_ids", "phone", "state", "status", "directory_id", "trusted_idp_id", "manager_ad_id", "manager_user_id", "samaccountname", "member_of", "userprincipalname", "distinguished_name", "external_id", "activated_at", "last_login", "invitation_sent_at", "updated_at", "preferred_locale_code", "created_at", "invalid_login_attempts", "locked_until", "password_changed_at", "password", "password_confirmation", "password_algorithm", "salt", "custom_attributes"]
6061

6162
@field_validator('state')
6263
@classmethod
@@ -153,7 +154,8 @@ def from_dict(cls, obj: dict) -> User:
153154
"password": obj.get("password"),
154155
"password_confirmation": obj.get("password_confirmation"),
155156
"password_algorithm": obj.get("password_algorithm"),
156-
"salt": obj.get("salt")
157+
"salt": obj.get("salt"),
158+
"custom_attributes": obj.get("custom_attributes")
157159
})
158160
return _obj
159161

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# coding: utf-8
2+
3+
"""
4+
OneLogin API
5+
6+
Test for custom_attributes support in User model
7+
8+
The version of the OpenAPI document: 3.1.1
9+
"""
10+
11+
12+
import unittest
13+
import json
14+
15+
import onelogin
16+
from onelogin.models.user import User
17+
from onelogin.rest import ApiException
18+
19+
20+
class TestUserCustomAttributes(unittest.TestCase):
21+
"""User custom_attributes unit test"""
22+
23+
def setUp(self):
24+
pass
25+
26+
def tearDown(self):
27+
pass
28+
29+
def test_user_with_custom_attributes(self):
30+
"""Test User with custom_attributes"""
31+
# Create a user with custom_attributes
32+
user_data = {
33+
"id": 123,
34+
"username": "test_user",
35+
"email": "test@example.com",
36+
"firstname": "Test",
37+
"lastname": "User",
38+
"custom_attributes": {
39+
"department_code": "ENG001",
40+
"employee_id": "12345",
41+
"cost_center": "CC-100"
42+
}
43+
}
44+
45+
# Test from_dict
46+
user = User.from_dict(user_data)
47+
48+
# Verify basic fields
49+
self.assertEqual(user.id, 123)
50+
self.assertEqual(user.username, "test_user")
51+
self.assertEqual(user.email, "test@example.com")
52+
self.assertEqual(user.firstname, "Test")
53+
self.assertEqual(user.lastname, "User")
54+
55+
# Verify custom_attributes
56+
self.assertIsNotNone(user.custom_attributes)
57+
self.assertIsInstance(user.custom_attributes, dict)
58+
self.assertEqual(user.custom_attributes.get("department_code"), "ENG001")
59+
self.assertEqual(user.custom_attributes.get("employee_id"), "12345")
60+
self.assertEqual(user.custom_attributes.get("cost_center"), "CC-100")
61+
62+
def test_user_without_custom_attributes(self):
63+
"""Test User without custom_attributes"""
64+
user_data = {
65+
"id": 456,
66+
"username": "test_user2",
67+
"email": "test2@example.com"
68+
}
69+
70+
user = User.from_dict(user_data)
71+
72+
self.assertEqual(user.id, 456)
73+
self.assertEqual(user.username, "test_user2")
74+
self.assertIsNone(user.custom_attributes)
75+
76+
def test_user_to_dict_with_custom_attributes(self):
77+
"""Test User to_dict with custom_attributes"""
78+
user = User(
79+
id=789,
80+
username="test_user3",
81+
email="test3@example.com",
82+
custom_attributes={
83+
"location": "San Francisco",
84+
"employee_type": "Full-time"
85+
}
86+
)
87+
88+
user_dict = user.to_dict()
89+
90+
self.assertEqual(user_dict["id"], 789)
91+
self.assertEqual(user_dict["username"], "test_user3")
92+
self.assertEqual(user_dict["email"], "test3@example.com")
93+
self.assertIn("custom_attributes", user_dict)
94+
self.assertEqual(user_dict["custom_attributes"]["location"], "San Francisco")
95+
self.assertEqual(user_dict["custom_attributes"]["employee_type"], "Full-time")
96+
97+
def test_user_from_json_with_custom_attributes(self):
98+
"""Test User from_json with custom_attributes"""
99+
json_str = '''{
100+
"id": 999,
101+
"username": "test_user4",
102+
"email": "test4@example.com",
103+
"custom_attributes": {
104+
"badge_number": "B12345",
105+
"clearance_level": "Level 2"
106+
}
107+
}'''
108+
109+
user = User.from_json(json_str)
110+
111+
self.assertEqual(user.id, 999)
112+
self.assertEqual(user.username, "test_user4")
113+
self.assertIsNotNone(user.custom_attributes)
114+
self.assertEqual(user.custom_attributes.get("badge_number"), "B12345")
115+
self.assertEqual(user.custom_attributes.get("clearance_level"), "Level 2")
116+
117+
118+
if __name__ == '__main__':
119+
unittest.main()

0 commit comments

Comments
 (0)