diff --git a/test/test_option_username.py b/test/test_option_username.py new file mode 100644 index 0000000..472e9cd --- /dev/null +++ b/test/test_option_username.py @@ -0,0 +1,25 @@ +"configuration objects global API" +import autopath +from py.test import raises + +from tiramisu.config import Config +from tiramisu.option import UsernameOption + +def test_username(): + UsernameOption('a', '', 'string') + UsernameOption('a', '', '_string') + UsernameOption('a', '', 's_tring') + UsernameOption('a', '', 'string_') + UsernameOption('a', '', 'string$') + UsernameOption('a', '', '_string$') + raises(ValueError, "UsernameOption('a', '', 'strin$g')") + UsernameOption('a', '', 's-tring') + raises(ValueError, "UsernameOption('a', '', '-string')") + UsernameOption('a', '', 's9tring') + raises(ValueError, "UsernameOption('a', '', '9string')") + raises(ValueError, "UsernameOption('a', '', '')") + UsernameOption('a', '', 's') + UsernameOption('a', '', 's2345678901234567890123456789012') + raises(ValueError, "UsernameOption('a', '', 's23456789012345678901234567890123')") + UsernameOption('a', '', 's234567890123456789012345678901$') + raises(ValueError, "UsernameOption('a', '', 's2345678901234567890123456789012$')") diff --git a/tiramisu/option.py b/tiramisu/option.py index b9960b6..da93102 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -1079,6 +1079,18 @@ class URLOption(DomainnameOption): raise ValueError(_('invalid url, should ends with filename')) +class UsernameOption(Option): + __slots__ = tuple() + _opt_type = 'username' + #regexp build with 'man 8 adduser' informations + username_re = re.compile(r"^[a-z_][a-z0-9_-]{0,30}[$a-z0-9_-]{0,1}$") + + def _validate(self, value): + match = self.username_re.search(value) + if not match: + raise ValueError(_('invalid username')) + + class FilenameOption(Option): __slots__ = tuple() _opt_type = 'file'