Skip to content

Commit 0ce3708

Browse files
committed
repair exit conditions and add negative test
1 parent c7b7e3f commit 0ce3708

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

mig/server/createuser.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from builtins import input
3434
from getpass import getpass
3535
import datetime
36+
import errno
3637
import getopt
3738
import os
3839
import sys
@@ -181,7 +182,7 @@ def main(args, cwd, db_path=keyword_auto):
181182
if verbose:
182183
print('using configuration from MIG_CONF (or default)')
183184

184-
_main(None, args,
185+
ret = _main(None, args,
185186
conf_path=conf_path,
186187
db_path=db_path,
187188
expire=expire,
@@ -199,6 +200,12 @@ def main(args, cwd, db_path=keyword_auto):
199200
hash_password=hash_password
200201
)
201202

203+
if ret == errno.ENOTSUP:
204+
usage()
205+
sys.exit(1)
206+
207+
sys.exit(ret)
208+
202209

203210
def _main(configuration, args,
204211
conf_path=keyword_auto,
@@ -234,14 +241,12 @@ def _main(configuration, args,
234241

235242
if user_file and args:
236243
print('Error: Only one kind of user specification allowed at a time')
237-
usage()
238-
sys.exit(1)
244+
return errno.ENOTSUP
239245

240246
if auth_type not in valid_auth_types:
241247
print('Error: invalid account auth type %r requested (allowed: %s)' %
242248
(auth_type, ', '.join(valid_auth_types)))
243-
usage()
244-
sys.exit(1)
249+
return errno.ENOTSUP
245250

246251
# NOTE: renew requires original password
247252
if auth_type == 'cert':
@@ -262,8 +267,7 @@ def _main(configuration, args,
262267
except IndexError:
263268
print('Error: too few arguments given (expected 7 got %d)'
264269
% len(args))
265-
usage()
266-
sys.exit(1)
270+
return errno.ENOTSUP
267271
# Force user ID fields to canonical form for consistency
268272
# Title name, lowercase email, uppercase country and state, etc.
269273
user_dict = canonical_user(configuration, raw_user, raw_user.keys())
@@ -272,14 +276,12 @@ def _main(configuration, args,
272276
user_dict = load(user_file)
273277
except Exception as err:
274278
print('Error in user name extraction: %s' % err)
275-
usage()
276-
sys.exit(1)
279+
return errno.ENOTSUP
277280
elif default_renew and user_id:
278281
saved = load_user_dict(logger, user_id, db_path, verbose)
279282
if not saved:
280283
print('Error: no such user in user db: %s' % user_id)
281-
usage()
282-
sys.exit(1)
284+
return errno.ENOTSUP
283285
user_dict.update(saved)
284286
del user_dict['expire']
285287
elif not configuration.site_enable_gdp:
@@ -301,7 +303,7 @@ def _main(configuration, args,
301303
print("Error: Missing one or more of the arguments: "
302304
+ "[FULL_NAME] [ORGANIZATION] [STATE] [COUNTRY] "
303305
+ "[EMAIL] [COMMENT] [PASSWORD]")
304-
sys.exit(1)
306+
return 1
305307

306308
# Encode password if set but not already encoded
307309

@@ -364,14 +366,16 @@ def _main(configuration, args,
364366
print("Error creating user: %s" % exc)
365367
import traceback
366368
logger.warning("Error creating user: %s" % traceback.format_exc())
367-
sys.exit(1)
369+
return 1
368370
print('Created or updated %s in user database and in file system' %
369371
user_dict['distinguished_name'])
370372
if user_file:
371373
if verbose:
372374
print('Cleaning up tmp file: %s' % user_file)
373375
os.remove(user_file)
374376

377+
return 0
378+
375379

376380
if __name__ == '__main__':
377381
(args, cwd, db_path) = init_user_adm()

tests/test_mig_server_createuser.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"""Unit tests for the migrid createuser CLI"""
2929

3030
from __future__ import print_function
31+
import errno
3132
import os
3233
import shutil
3334
import sys
@@ -133,6 +134,21 @@ def _generate_salt():
133134
'unique_id': '__UNIQUE_ID__',
134135
})
135136

137+
def test_missing_arguments(self):
138+
args = [
139+
"Test User",
140+
"Test Org",
141+
"NA",
142+
"DK",
143+
"user@example.com",
144+
"This is the create comment",
145+
# leave off a password
146+
]
147+
print("") # acount for output generated by the logic
148+
149+
ret = createuser(self.configuration, args)
150+
151+
self.assertEqual(ret, errno.ENOTSUP)
136152

137153
if __name__ == '__main__':
138154
testmain()

0 commit comments

Comments
 (0)