Skip to content

Commit 32d642f

Browse files
committed
Make use of argument bundles in the server.
1 parent 8a5e0c9 commit 32d642f

File tree

3 files changed

+35
-60
lines changed

3 files changed

+35
-60
lines changed

mig/server/createuser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ def main(_main, args, cwd, db_path=keyword_auto):
219219
if verbose:
220220
print('using configuration from MIG_CONF (or default)')
221221

222-
bundle = _BUNDLE_DEFINITION(*args)
222+
try:
223+
bundle = _BUNDLE_DEFINITION(*args)
224+
except ValueError as valueexc:
225+
print(valueexc)
226+
sys.exit(1)
223227

224228
ret = _main(None, bundle,
225229
conf_path=conf_path,
@@ -249,7 +253,7 @@ def main(_main, args, cwd, db_path=keyword_auto):
249253
sys.exit(ret)
250254

251255

252-
def _main(configuration, args,
256+
def _main(configuration, bundle,
253257
conf_path=keyword_auto,
254258
db_path=keyword_auto,
255259
auth_type='custom',
@@ -268,6 +272,9 @@ def _main(configuration, args,
268272
hash_password=True,
269273
_generate_salt=None
270274
):
275+
276+
args = _BUNDLE_DEFINITION.ensure_bundle(bundle)
277+
271278
if configuration is None:
272279
if conf_path == keyword_auto:
273280
config_file = None

mig/services/coreapi/server.py

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
check_hash, search_users as useradm_search_users
7878
from mig.shared.userdb import default_db_path
7979
from mig.shared.validstring import possible_user_id, is_valid_email_address
80-
from mig.server.createuser import _main as createuser
80+
from mig.server.createuser import _main as createuser, \
81+
_BUNDLE_DEFINITION as _REQUEST_ARGS_POST_USER
8182

8283
# Update with extra fields
8384
cert_field_map.update({'role': 'ROLE', 'timezone': 'TZ', 'nickname': 'NICK',
@@ -171,38 +172,6 @@ def serialize(self, output_format='text'):
171172
return 'payload failed to validate:%s' % ('\n'.join(lines),)
172173

173174

174-
def _is_not_none(value):
175-
"""value is not None"""
176-
return value is not None
177-
178-
179-
def _is_string_and_non_empty(value):
180-
"""value is a non-empty string"""
181-
return isinstance(value, str) and len(value) > 0
182-
183-
184-
_REQUEST_ARGS_POST_USER = namedtuple('PostUserArgs', [
185-
'full_name',
186-
'organization',
187-
'state',
188-
'country',
189-
'email',
190-
'comment',
191-
'password',
192-
])
193-
194-
195-
_REQUEST_ARGS_POST_USER._validators = defaultdict(lambda: _is_not_none, dict(
196-
full_name=_is_string_and_non_empty,
197-
organization=_is_string_and_non_empty,
198-
state=_is_string_and_non_empty,
199-
country=_is_string_and_non_empty,
200-
email=_is_string_and_non_empty,
201-
comment=_is_string_and_non_empty,
202-
password=_is_string_and_non_empty,
203-
))
204-
205-
206175
def search_users(configuration, search_filter):
207176
conf_path = configuration.config_file
208177
db_path = default_db_path(configuration)
@@ -214,7 +183,7 @@ def validate_payload(definition, payload):
214183
args = definition(*[payload.get(field, None) for field in definition._fields])
215184

216185
errors_by_field = {}
217-
for field_name, field_value in args._asdict().items():
186+
for field_name, field_value in args.items():
218187
validator_fn = definition._validators[field_name]
219188
if not validator_fn(field_value):
220189
errors_by_field[field_name] = validator_fn.__doc__
@@ -257,9 +226,7 @@ def POST_user():
257226
except ValidationReport as vr:
258227
return http_error_from_status_code(400, None, vr.serialize())
259228

260-
args = list(validated)
261-
262-
ret = createuser(configuration, args)
229+
ret = createuser(configuration, validated)
263230
if ret != 0:
264231
raise http_error_from_status_code(400, None)
265232

tests/test_mig_server_createuser.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
from tests.support.picklesupp import PickleAssertMixin
3838

3939
from mig.shared.arguments import ArgumentBundle
40-
from mig.server.createuser import _main as createuser, main as createuser_main
40+
from mig.server.createuser import _main as createuser, \
41+
main as createuser_main, _BUNDLE_DEFINITION
4142
from mig.shared.useradm import _USERADM_CONFIG_DIR_KEYS
4243

4344

@@ -84,17 +85,17 @@ def _instrumented_main(*args, **kwargs):
8485
self.assertEqual(maybe_argument_bundle.name, 'UserArguments')
8586

8687
def test_user_db_is_created_when_absent(self):
87-
args = [
88+
bundle = _BUNDLE_DEFINITION(*[
8889
"Test User",
8990
"Test Org",
9091
"NA",
9192
"DK",
9293
"user@example.com",
9394
"This is the create comment",
9495
"password"
95-
]
96+
])
9697
print("") # acount for output generated by the logic
97-
createuser(self.configuration, args, default_renew=True)
98+
createuser(self.configuration, bundle, default_renew=True)
9899

99100
# presence of user home
100101
path_kind = MigTestCase._absolute_path_kind(self.expected_user_db_home)
@@ -117,18 +118,18 @@ def _generate_salt():
117118
else:
118119
expected_user_password_hash = "PBKDF2$sha256$10000$b'CCCC12344321CCCC'$b'bph8p/avUq42IYeOdJoJuUqrJ7Q32eaT'"
119120

120-
args = [
121+
bundle = _BUNDLE_DEFINITION(*[
121122
"Test User",
122123
"Test Org",
123124
"NA",
124125
"DK",
125126
"user@example.com",
126127
"This is the create comment",
127128
"password"
128-
]
129+
])
129130
print("") # acount for output generated by the logic
130131

131-
createuser(self.configuration, args, default_renew=True,
132+
createuser(self.configuration, bundle, default_renew=True,
132133
_generate_salt=_generate_salt)
133134

134135
pickled = self.assertPickledFile(self.expected_user_db_file)
@@ -160,20 +161,20 @@ def _generate_salt():
160161
})
161162

162163
def test_missing_arguments(self):
163-
args = [
164-
"Test User",
165-
"Test Org",
166-
"NA",
167-
"DK",
168-
"user@example.com",
169-
"This is the create comment",
170-
# leave off a password
171-
]
172-
print("") # acount for output generated by the logic
173-
174-
ret = createuser(self.configuration, args)
175-
176-
self.assertEqual(ret, errno.ENOTSUP)
164+
with self.assertRaises(ValueError) as raised:
165+
_BUNDLE_DEFINITION(*[
166+
"Test User",
167+
"Test Org",
168+
"NA",
169+
"DK",
170+
"user@example.com",
171+
"This is the create comment",
172+
# leave off a password
173+
])
174+
175+
theexception = raised.exception
176+
self.assertEqual(str(theexception),
177+
"Error: too few arguments given (expected 7 got 6)")
177178

178179
if __name__ == '__main__':
179180
testmain()

0 commit comments

Comments
 (0)