44
55 Tests for the user cli command
66"""
7- from SoftLayer import testing
8-
97import json
8+ import sys
9+
10+ import mock
11+ import testtools
12+
13+ from SoftLayer import testing
1014
1115
1216class UserCLITests (testing .TestCase ):
@@ -69,6 +73,42 @@ def test_detail_events(self):
6973 self .assert_no_fail (result )
7074 self .assert_called_with ('SoftLayer_Event_Log' , 'getAllObjects' )
7175
76+ def test_print_hardware_access (self ):
77+ mock = self .set_mock ('SoftLayer_User_Customer' , 'getObject' )
78+ mock .return_value = {
79+ 'accountId' : 12345 ,
80+ 'address1' : '315 Test Street' ,
81+ 'city' : 'Houston' ,
82+ 'companyName' : 'SoftLayer Development Community' ,
83+ 'country' : 'US' ,
84+ 'displayName' : 'Test' ,
85+ 'email' : 'test@us.ibm.com' ,
86+ 'firstName' : 'Test' ,
87+ 'id' : 244956 ,
88+ 'lastName' : 'Testerson' ,
89+ 'postalCode' : '77002' ,
90+ 'state' : 'TX' ,
91+ 'statusDate' : None ,
92+ 'hardware' : [
93+ {'id' : 1234 ,
94+ 'fullyQualifiedDomainName' : 'test.test.test' ,
95+ 'provisionDate' : '2018-05-08T15:28:32-06:00' ,
96+ 'primaryBackendIpAddress' : '175.125.126.118' ,
97+ 'primaryIpAddress' : '175.125.126.118' }
98+ ],
99+ 'dedicatedHosts' : [
100+ {'id' : 1234 ,
101+ 'fullyQualifiedDomainName' : 'test.test.test' ,
102+ 'provisionDate' : '2018-05-08T15:28:32-06:00' ,
103+ 'primaryBackendIpAddress' : '175.125.126.118' ,
104+ 'primaryIpAddress' : '175.125.126.118' }
105+ ],
106+ }
107+ result = self .run_command (['user' , 'detail' , '11100' , '-h' ])
108+ self .assert_no_fail (result )
109+ self .assert_called_with ('SoftLayer_User_Customer' , 'getObject' , identifier = 11100 ,
110+ mask = "mask[id, hardware, dedicatedHosts]" )
111+
72112 """User permissions tests"""
73113
74114 def test_permissions_list (self ):
@@ -95,3 +135,118 @@ def test_edit_perms_off(self):
95135 result = self .run_command (['user' , 'edit-permissions' , '11100' , '--disable' , '-p' , 'TEST' ])
96136 self .assert_no_fail (result )
97137 self .assert_called_with ('SoftLayer_User_Customer' , 'removeBulkPortalPermission' , identifier = 11100 )
138+
139+ @mock .patch ('SoftLayer.CLI.user.edit_permissions.click' )
140+ def test_edit_perms_off_failure (self , click ):
141+ permission_mock = self .set_mock ('SoftLayer_User_Customer' , 'removeBulkPortalPermission' )
142+ permission_mock .return_value = False
143+ result = self .run_command (['user' , 'edit-permissions' , '11100' , '--disable' , '-p' , 'TEST' ])
144+ click .secho .assert_called_with ('Failed to update permissions: TEST' , fg = 'red' )
145+ self .assert_no_fail (result )
146+ self .assert_called_with ('SoftLayer_User_Customer' , 'removeBulkPortalPermission' , identifier = 11100 )
147+
148+ def test_edit_perms_from_user (self ):
149+ result = self .run_command (['user' , 'edit-permissions' , '11100' , '-u' , '1234' ])
150+ self .assert_no_fail (result )
151+ self .assert_called_with ('SoftLayer_User_Customer' , 'getPermissions' , identifier = 1234 )
152+ self .assert_called_with ('SoftLayer_User_Customer' , 'removeBulkPortalPermission' , identifier = 11100 )
153+ self .assert_called_with ('SoftLayer_User_Customer' , 'addBulkPortalPermission' , identifier = 11100 )
154+
155+ """User create tests"""
156+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
157+ def test_create_user (self , confirm_mock ):
158+ confirm_mock .return_value = True
159+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' , '-p' , 'testword' ])
160+ self .assert_no_fail (result )
161+ self .assertIn ('test@us.ibm.com' , result .output )
162+ self .assert_called_with ('SoftLayer_Account' , 'getCurrentUser' )
163+ self .assert_called_with ('SoftLayer_User_Customer' , 'createObject' , args = mock .ANY )
164+
165+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
166+ def test_create_user_no_confirm (self , confirm_mock ):
167+ confirm_mock .return_value = False
168+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' , '-p' , 'testword' ])
169+ self .assertEqual (result .exit_code , 2 )
170+
171+ @testtools .skipIf (sys .version_info < (3 , 6 ), "Secrets module only exists in version 3.6+" )
172+ @mock .patch ('secrets.choice' )
173+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
174+ def test_create_user_generate_password_36 (self , confirm_mock , secrets ):
175+ secrets .return_value = 'Q'
176+ confirm_mock .return_value = True
177+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' , '-p' , 'generate' ])
178+
179+ self .assert_no_fail (result )
180+ self .assertIn ('test@us.ibm.com' , result .output )
181+ self .assertIn ('QQQQQQQQQQQQQQQQQQQQQQ' , result .output )
182+ self .assert_called_with ('SoftLayer_Account' , 'getCurrentUser' )
183+ self .assert_called_with ('SoftLayer_User_Customer' , 'createObject' , args = mock .ANY )
184+
185+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
186+ def test_create_user_generate_password_2 (self , confirm_mock ):
187+ if sys .version_info >= (3 , 6 ):
188+ self .skipTest ("Python needs to be < 3.6 for this test." )
189+
190+ confirm_mock .return_value = True
191+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' , '-p' , 'generate' ])
192+ self .assertIn (result .output , "ImportError" )
193+
194+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
195+ def test_create_user_and_apikey (self , confirm_mock ):
196+ confirm_mock .return_value = True
197+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' , '-a' ])
198+ self .assert_no_fail (result )
199+ self .assert_called_with ('SoftLayer_User_Customer' , 'addApiAuthenticationKey' )
200+
201+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
202+ def test_create_user_with_template (self , confirm_mock ):
203+ confirm_mock .return_value = True
204+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' ,
205+ '-t' , '{"firstName": "Supermand"}' ])
206+ self .assertIn ('Supermand' , result .output )
207+
208+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
209+ def test_create_user_with_bad_template (self , confirm_mock ):
210+ confirm_mock .return_value = True
211+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' ,
212+ '-t' , '{firstName: "Supermand"}' ])
213+ self .assertIn ("Argument Error" , result .exception .message )
214+ self .assertEqual (result .exit_code , 2 )
215+
216+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
217+ def test_create_user_with_no_confirm (self , confirm_mock ):
218+ confirm_mock .return_value = False
219+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' ])
220+ self .assertIn ("Canceling creation!" , result .exception .message )
221+ self .assertEqual (result .exit_code , 2 )
222+
223+ @mock .patch ('SoftLayer.CLI.formatting.confirm' )
224+ def test_create_user_from_user (self , confirm_mock ):
225+ confirm_mock .return_value = True
226+ result = self .run_command (['user' , 'create' , 'test' , '-e' , 'test@us.ibm.com' , '-u' , '1234' ])
227+ self .assert_no_fail (result )
228+ self .assert_called_with ('SoftLayer_User_Customer' , 'getObject' , identifier = 1234 )
229+
230+ """User edit-details tests"""
231+ @mock .patch ('SoftLayer.CLI.user.edit_details.click' )
232+ def test_edit_details (self , click ):
233+ result = self .run_command (['user' , 'edit-details' , '1234' , '-t' , '{"firstName":"Supermand"}' ])
234+ click .secho .assert_called_with ('1234 updated successfully' , fg = 'green' )
235+ self .assert_no_fail (result )
236+ self .assert_called_with ('SoftLayer_User_Customer' , 'editObject' ,
237+ args = ({'firstName' : 'Supermand' },), identifier = 1234 )
238+
239+ @mock .patch ('SoftLayer.CLI.user.edit_details.click' )
240+ def test_edit_details_failure (self , click ):
241+ mock = self .set_mock ('SoftLayer_User_Customer' , 'editObject' )
242+ mock .return_value = False
243+ result = self .run_command (['user' , 'edit-details' , '1234' , '-t' , '{"firstName":"Supermand"}' ])
244+ click .secho .assert_called_with ('Failed to update 1234' , fg = 'red' )
245+ self .assert_no_fail (result )
246+ self .assert_called_with ('SoftLayer_User_Customer' , 'editObject' ,
247+ args = ({'firstName' : 'Supermand' },), identifier = 1234 )
248+
249+ def test_edit_details_bad_json (self ):
250+ result = self .run_command (['user' , 'edit-details' , '1234' , '-t' , '{firstName:"Supermand"}' ])
251+ self .assertIn ("Argument Error" , result .exception .message )
252+ self .assertEqual (result .exit_code , 2 )
0 commit comments