1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
"""
SoftLayer.tests.managers.user_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import datetime
from unittest import mock as mock
import SoftLayer
from SoftLayer import exceptions
from SoftLayer import testing
real_datetime_class = datetime.datetime
def mock_datetime(target, datetime_module):
"""A way to use specific datetimes in tests. Just mocking datetime doesn't work because of pypy
https://solidgeargroup.com/mocking-the-time
"""
class DatetimeSubclassMeta(type):
@classmethod
def __instancecheck__(mcs, obj):
return isinstance(obj, real_datetime_class)
class BaseMockedDatetime(real_datetime_class):
@classmethod
def now(cls, tz=None):
return target.replace(tzinfo=tz)
@classmethod
def utcnow(cls):
return target
@classmethod
def today(cls):
return target
# Python2 & Python3-compatible metaclass
MockedDatetime = DatetimeSubclassMeta('datetime', (BaseMockedDatetime,), {})
return mock.patch.object(datetime_module, 'datetime', MockedDatetime)
class UserManagerTests(testing.TestCase):
def set_up(self):
self.manager = SoftLayer.UserManager(self.client)
def test_list_user_defaults(self):
self.manager.list_users()
self.assert_called_with('SoftLayer_Account', 'getUsers', mask=mock.ANY)
def test_list_user_mask(self):
self.manager.list_users(objectmask="mask[id]")
self.assert_called_with('SoftLayer_Account', 'getUsers', mask="mask[id]")
def test_list_user_filter(self):
test_filter = {'id': {'operation': 1234}}
self.manager.list_users(objectfilter=test_filter)
self.assert_called_with('SoftLayer_Account', 'getUsers', filter=test_filter)
def test_get_user_default(self):
self.manager.get_user(1234)
self.assert_called_with('SoftLayer_User_Customer', 'getObject', identifier=1234,
mask="mask[userStatus[name], parent[id, username]]")
def test_get_user_mask(self):
self.manager.get_user(1234, objectmask="mask[id]")
self.assert_called_with('SoftLayer_User_Customer', 'getObject', identifier=1234, mask="mask[id]")
def test_get_all_permissions(self):
self.manager.get_all_permissions()
self.assert_called_with('SoftLayer_User_Permission_Action', 'getAllObjects')
def test_add_permissions(self):
self.manager.add_permissions(1234, ['TEST'])
expected_args = (
[{'keyName': 'TEST'}],
)
self.assert_called_with('SoftLayer_User_Customer', 'addBulkPortalPermission',
args=expected_args, identifier=1234)
def test_remove_permissions(self):
self.manager.remove_permissions(1234, ['TEST'])
expected_args = (
[{'keyName': 'TEST'}],
)
self.assert_called_with('SoftLayer_User_Customer', 'removeBulkPortalPermission',
args=expected_args, identifier=1234)
def test_get_logins_default(self):
target = datetime.datetime(2018, 5, 15)
with mock_datetime(target, datetime):
self.manager.get_logins(1234)
expected_filter = {
'loginAttempts': {
'createDate': {
'operation': 'greaterThanDate',
'options': [{'name': 'date', 'value': ['04/15/2018 0:0:0']}]
}
}
}
self.assert_called_with('SoftLayer_User_Customer', 'getLoginAttempts', filter=expected_filter)
def test_get_events_default(self):
target = datetime.datetime(2018, 5, 15)
with mock_datetime(target, datetime):
self.manager.get_events(1234)
expected_filter = {
'userId': {
'operation': 1234
},
'eventCreateDate': {
'operation': 'greaterThanDate',
'options': [{'name': 'date', 'value': ['2018-04-15T00:00:00']}]
}
}
self.assert_called_with('SoftLayer_Event_Log', 'getAllObjects', filter=expected_filter)
def test_get_events_empty(self):
event_mock = self.set_mock('SoftLayer_Event_Log', 'getAllObjects')
event_mock.return_value = None
result = self.manager.get_events(1234)
self.assert_called_with('SoftLayer_Event_Log', 'getAllObjects', filter=mock.ANY)
self.assertEqual([{'eventName': 'No Events Found'}], result)
@mock.patch('SoftLayer.managers.user.UserManager.get_user_permissions')
def test_permissions_from_user(self, user_permissions):
user_permissions.return_value = [
{"keyName": "TICKET_VIEW"},
{"keyName": "TEST"}
]
removed_permissions = [
{'keyName': 'ACCESS_ALL_HARDWARE'},
{'keyName': 'ACCESS_ALL_HARDWARE'},
{'keyName': 'ACCOUNT_SUMMARY_VIEW'},
{'keyName': 'ADD_SERVICE_STORAGE'},
{'keyName': 'TEST_3'},
{'keyName': 'TEST_4'}
]
self.manager.permissions_from_user(1234, 5678)
self.assert_called_with('SoftLayer_User_Customer', 'addBulkPortalPermission',
args=(user_permissions.return_value,))
self.assert_called_with('SoftLayer_User_Customer', 'removeBulkPortalPermission',
args=(removed_permissions,))
def test_get_id_from_username_one_match(self):
account_mock = self.set_mock('SoftLayer_Account', 'getUsers')
account_mock.return_value = [{'id': 1234}]
user_id = self.manager._get_id_from_username('testUser')
expected_filter = {'users': {'username': {'operation': '_= testUser'}}}
self.assert_called_with('SoftLayer_Account', 'getUsers', filter=expected_filter, mask="mask[id, username]")
self.assertEqual([1234], user_id)
def test_get_id_from_username_multiple_match(self):
account_mock = self.set_mock('SoftLayer_Account', 'getUsers')
account_mock.return_value = [{'id': 1234}, {'id': 4567}]
self.assertRaises(exceptions.SoftLayerError, self.manager._get_id_from_username, 'testUser')
def test_get_id_from_username_zero_match(self):
account_mock = self.set_mock('SoftLayer_Account', 'getUsers')
account_mock.return_value = []
self.assertRaises(exceptions.SoftLayerError, self.manager._get_id_from_username, 'testUser')
def test_format_permission_object(self):
result = self.manager.format_permission_object(['TEST'])
self.assert_called_with('SoftLayer_User_Permission_Action', 'getAllObjects')
self.assertEqual([{'keyName': 'TEST'}], result)
def test_format_permission_object_all(self):
expected = [
{'key': 'T_2', 'keyName': 'TEST', 'name': 'A Testing Permission'},
{'key': 'T_1', 'keyName': 'TICKET_VIEW', 'name': 'View Tickets'}
]
service_name = 'SoftLayer_User_Permission_Action'
permission_mock = self.set_mock(service_name, 'getAllObjects')
permission_mock.return_value = expected
result = self.manager.format_permission_object(['ALL'])
self.assert_called_with(service_name, 'getAllObjects')
self.assertEqual(expected, result)
def test_hide_permissions(self):
result = self.manager.get_all_permissions()
hide_permissions = [
{'keyName': 'ACCOUNT_SUMMARY_VIEW'},
{'keyName': 'REQUEST_COMPLIANCE_REPORT'},
{'keyName': 'COMPANY_EDIT'},
{'keyName': 'ONE_TIME_PAYMENTS'},
{'keyName': 'UPDATE_PAYMENT_DETAILS'},
{'keyName': 'EU_LIMITED_PROCESSING_MANAGE'},
{'keyName': 'TICKET_ADD'},
{'keyName': 'TICKET_EDIT'},
{'keyName': 'TICKET_SEARCH'},
{'keyName': 'TICKET_VIEW'},
{'keyName': 'TICKET_VIEW_ALL'}
]
self.assert_called_with('SoftLayer_User_Permission_Action', 'getAllObjects')
self.assertNotEqual(hide_permissions, result)
def test_get_current_user(self):
result = self.manager.get_current_user()
self.assert_called_with('SoftLayer_Account', 'getCurrentUser', mask=mock.ANY)
self.assertEqual(result['id'], 12345)
def test_get_current_user_mask(self):
result = self.manager.get_current_user(objectmask="mask[id]")
self.assert_called_with('SoftLayer_Account', 'getCurrentUser', mask="mask[id]")
self.assertEqual(result['id'], 12345)
def test_create_user_handle_paas_exception(self):
user_template = {"username": "foobar", "email": "foobar@example.com"}
self.manager.user_service = mock.Mock()
# FaultCode IS NOT SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas
any_error = exceptions.SoftLayerAPIError("SoftLayer_Exception_User_Customer",
"This exception indicates an error")
self.manager.user_service.createObject.side_effect = any_error
try:
self.manager.create_user(user_template, "Pass@123")
except exceptions.SoftLayerAPIError as ex:
self.assertEqual(ex.faultCode, "SoftLayer_Exception_User_Customer")
self.assertEqual(ex.faultString, "This exception indicates an error")
# FaultCode is SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas
paas_error = exceptions.SoftLayerAPIError("SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas",
"This exception does NOT indicate an error")
self.manager.user_service.createObject.side_effect = paas_error
try:
self.manager.create_user(user_template, "Pass@123")
except exceptions.SoftLayerError as ex:
self.assertEqual(ex.args[0], "Your request for a new user was received, but it needs to be processed by "
"the Platform Services API first. Barring any errors on the Platform Services "
"side, your new user should be created shortly.")
def test_vpn_manual(self):
user_id = 1234
self.manager.vpn_manual(user_id, True)
self.assert_called_with('SoftLayer_User_Customer', 'editObject', identifier=user_id)
def test_vpn_subnet_add(self):
user_id = 1234
subnet_id = 1234
expected_args = (
[{"userId": user_id, "subnetId": subnet_id}],
)
self.manager.vpn_subnet_add(user_id, [subnet_id])
self.assert_called_with('SoftLayer_Network_Service_Vpn_Overrides', 'createObjects', args=expected_args)
self.assert_called_with('SoftLayer_User_Customer', 'updateVpnUser', identifier=user_id)
def test_vpn_subnet_remove(self):
user_id = 1234
subnet_id = 1234
overrides = [{'id': 3661234, 'subnetId': subnet_id}]
expected_args = (
overrides,
)
self.manager.vpn_subnet_remove(user_id, [subnet_id])
self.assert_called_with('SoftLayer_Network_Service_Vpn_Overrides', 'deleteObjects', args=expected_args)
self.assert_called_with('SoftLayer_User_Customer', 'updateVpnUser', identifier=user_id)
def test_get_all_notifications(self):
self.manager.get_all_notifications()
self.assert_called_with('SoftLayer_Email_Subscription', 'getAllObjects')
def test_enable_notifications(self):
self.manager.enable_notifications(['Test notification'])
self.assert_called_with('SoftLayer_Email_Subscription', 'enable', identifier=111)
def test_disable_notifications(self):
self.manager.disable_notifications(['Test notification'])
self.assert_called_with('SoftLayer_Email_Subscription', 'disable', identifier=111)
def test_enable_notifications_fail(self):
notification = self.set_mock('SoftLayer_Email_Subscription', 'enable')
notification.return_value = False
result = self.manager.enable_notifications(['Test notification'])
self.assert_called_with('SoftLayer_Email_Subscription', 'enable', identifier=111)
self.assertFalse(result)
def test_disable_notifications_fail(self):
notification = self.set_mock('SoftLayer_Email_Subscription', 'disable')
notification.return_value = False
result = self.manager.disable_notifications(['Test notification'])
self.assert_called_with('SoftLayer_Email_Subscription', 'disable', identifier=111)
self.assertFalse(result)
def test_gather_notifications(self):
expected_result = [
{'description': 'Testing description.',
'enabled': True,
'id': 111,
'name': 'Test notification'
}
]
result = self.manager.gather_notifications(['Test notification'])
self.assert_called_with('SoftLayer_Email_Subscription',
'getAllObjects',
mask='mask[enabled]')
self.assertEqual(result, expected_result)
def test_gather_notifications_fail(self):
ex = self.assertRaises(SoftLayer.SoftLayerError,
self.manager.gather_notifications,
['Test not exit'])
self.assertEqual("Test not exit is not a valid notification name", str(ex))
def test_get_hardware(self):
self.manager.get_user_hardware(1234)
self.assert_called_with('SoftLayer_User_Customer', 'getHardware')
def test_get_dedicated_host(self):
self.manager.get_user_dedicated_host(1234)
self.assert_called_with('SoftLayer_User_Customer', 'getDedicatedHosts')
def test_get_virtual(self):
self.manager.get_user_virtuals(1234)
self.assert_called_with('SoftLayer_User_Customer', 'getVirtualGuests')
def test_grant_hardware(self):
self.manager.grant_hardware_access(123456, 369852)
self.assert_called_with('SoftLayer_User_Customer', 'addHardwareAccess')
def test_grant_virtual(self):
self.manager.grant_virtual_access(123456, 369852)
self.assert_called_with('SoftLayer_User_Customer', 'addVirtualGuestAccess')
def test_grant_dedicated(self):
self.manager.grant_dedicated_access(123456, 369852)
self.assert_called_with('SoftLayer_User_Customer', 'addDedicatedHostAccess')
def test_remove_hardware(self):
self.manager.remove_hardware_access(123456, 369852)
self.assert_called_with('SoftLayer_User_Customer', 'removeHardwareAccess')
def test_remove_virtual(self):
self.manager.remove_virtual_access(123456, 369852)
self.assert_called_with('SoftLayer_User_Customer', 'removeVirtualGuestAccess')
def test_remove_dedicated(self):
self.manager.remove_dedicated_access(123456, 369852)
self.assert_called_with('SoftLayer_User_Customer', 'removeDedicatedHostAccess')
def test_update_vpn_password(self):
self.manager.update_vpn_password(123456, "Mypassword1.")
self.assert_called_with('SoftLayer_User_Customer', 'updateVpnPassword')
def test_add_api_authentication_key(self):
self.manager.add_api_authentication_key(123456)
self.assert_called_with('SoftLayer_User_Customer', 'addApiAuthenticationKey')
def test_get_api_authentication_keys(self):
self.manager.get_api_authentication_keys(123456)
self.assert_called_with('SoftLayer_User_Customer', 'getApiAuthenticationKeys')
def test_remove_api_authentication_key(self):
self.manager.remove_api_authentication_key(123456)
self.assert_called_with('SoftLayer_User_Customer', 'removeApiAuthenticationKey')
def test_get_permission_departments(self):
result = self.manager.get_permission_departments()
self.assert_called_with('SoftLayer_User_Permission_Department', 'getAllObjects')
# just making sure the lists are sorted.
self.assertEqual(result[0]['permissions'][0]['keyName'], 'ACCOUNT_BILLING_SYSTEM')
self.assertEqual(result[1]['permissions'][8]['keyName'], 'VIEW_ACH_INFO')
|