Skip to content

Commit 8a5e0c9

Browse files
committed
Define a user payload using the argument bundling code.
Update the createuser command line interface to operate based on these bundles and add tests covering the bundle packaged by the top-level main method which is passed into the internal logic of the call.
1 parent b67a9aa commit 8a5e0c9

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

mig/server/createuser.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
import time
4141

4242
from mig.shared.accountstate import default_account_expire
43-
from mig.shared.arguments import parse_getopt_args, break_apart_legacy_usage
43+
from mig.shared.arguments import parse_getopt_args, break_apart_legacy_usage, \
44+
ArgumentBundleDefinition
4445
from mig.shared.base import fill_distinguished_name, fill_user, canonical_user
4546
from mig.shared.conf import get_configuration_object
4647
from mig.shared.defaults import valid_auth_types, keyword_auto
@@ -107,8 +108,26 @@ def usage(name='createuser.py'):
107108
-v: Verbose output
108109
""")
109110

111+
def _is_not_none(value):
112+
"""value is not None"""
113+
return value is not None
110114

111-
def main(args, cwd, db_path=keyword_auto):
115+
def _is_string_and_non_empty(value):
116+
"""value is a non-empty string"""
117+
return isinstance(value, str) and len(value) > 0
118+
119+
_BUNDLE_DEFINITION = ArgumentBundleDefinition('UserArguments', [
120+
(None, 'full_name', _is_string_and_non_empty),
121+
(None, 'organization', _is_string_and_non_empty),
122+
(None, 'state', _is_string_and_non_empty),
123+
(None, 'country', _is_string_and_non_empty),
124+
(None, 'email', _is_string_and_non_empty),
125+
(None, 'comment', _is_string_and_non_empty),
126+
(None, 'password', _is_string_and_non_empty),
127+
])
128+
129+
130+
def main(_main, args, cwd, db_path=keyword_auto):
112131
conf_path = None
113132
auth_type = 'custom'
114133
expire = None
@@ -200,7 +219,9 @@ def main(args, cwd, db_path=keyword_auto):
200219
if verbose:
201220
print('using configuration from MIG_CONF (or default)')
202221

203-
ret = _main(None, args,
222+
bundle = _BUNDLE_DEFINITION(*args)
223+
224+
ret = _main(None, bundle,
204225
conf_path=conf_path,
205226
db_path=db_path,
206227
expire=expire,
@@ -218,6 +239,9 @@ def main(args, cwd, db_path=keyword_auto):
218239
hash_password=hash_password
219240
)
220241

242+
if ret is None:
243+
return
244+
221245
if ret == errno.ENOTSUP:
222246
usage()
223247
sys.exit(1)
@@ -397,4 +421,4 @@ def _main(configuration, args,
397421

398422
if __name__ == '__main__':
399423
(args, cwd, db_path) = init_user_adm()
400-
main(args, cwd, db_path=db_path)
424+
main(_main, args, cwd, db_path=db_path)

tests/test_mig_server_createuser.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
from tests.support import PY2, MIG_BASE, TEST_OUTPUT_DIR, MigTestCase, testmain
3737
from tests.support.picklesupp import PickleAssertMixin
3838

39-
from mig.server.createuser import _main as createuser
39+
from mig.shared.arguments import ArgumentBundle
40+
from mig.server.createuser import _main as createuser, main as createuser_main
4041
from mig.shared.useradm import _USERADM_CONFIG_DIR_KEYS
4142

4243

@@ -58,6 +59,30 @@ def before_each(self):
5859
def _provide_configuration(self):
5960
return 'testconfig'
6061

62+
def test_argument_conversion(self):
63+
args = [
64+
"Test User",
65+
"Test Org",
66+
"NA",
67+
"DK",
68+
"user@example.com",
69+
"This is the create comment",
70+
"password"
71+
]
72+
def _instrumented_main(*args, **kwargs):
73+
_instrumented_main.calls.append((args, kwargs))
74+
return None
75+
_instrumented_main.calls = []
76+
77+
createuser_main(_instrumented_main, args, TEST_OUTPUT_DIR)
78+
79+
self.assertEqual(len(_instrumented_main.calls), 1)
80+
thecall_args = _instrumented_main.calls[0][0]
81+
self.assertEqual(len(thecall_args), 2)
82+
maybe_argument_bundle = thecall_args[1]
83+
self.assertIsInstance(maybe_argument_bundle, ArgumentBundle)
84+
self.assertEqual(maybe_argument_bundle.name, 'UserArguments')
85+
6186
def test_user_db_is_created_when_absent(self):
6287
args = [
6388
"Test User",

0 commit comments

Comments
 (0)