|
| 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