diff options
-rw-r--r-- | service/pixelated/register.py | 22 | ||||
-rw-r--r-- | service/test/unit/config/test_register.py | 6 |
2 files changed, 18 insertions, 10 deletions
diff --git a/service/pixelated/register.py b/service/pixelated/register.py index b8e6e4fc..2bdbb27b 100644 --- a/service/pixelated/register.py +++ b/service/pixelated/register.py @@ -16,6 +16,7 @@ import re import getpass import logging +import sys from pixelated.config import arguments from pixelated.config import logger as logger_config @@ -39,14 +40,16 @@ def register( provider_cert, provider_cert_fingerprint): - try: - validate_username(username) - except ValueError: - print('Only lowercase letters, digits, . - and _ allowed.') - if not password: password = getpass.getpass('Please enter password for %s: ' % username) + try: + validate_username(username) + validate_password(password) + except ValueError, e: + print(e.message) + sys.exit(1) + events_server.ensure_server() LeapCertificate.set_cert_and_fingerprint(provider_cert, provider_cert_fingerprint) config = LeapConfig(leap_home=leap_home) @@ -62,8 +65,13 @@ def register( def validate_username(username): accepted_characters = '^[a-z0-9\-\_\.]*$' - if not re.match(accepted_characters, username) or len(username) > 8: - raise ValueError + if (not re.match(accepted_characters, username)): + raise ValueError('Only lowercase letters, digits, . - and _ allowed.') + + +def validate_password(password): + if len(password) < 8: + raise ValueError('The password must have at least 8 characters') def initialize(): diff --git a/service/test/unit/config/test_register.py b/service/test/unit/config/test_register.py index b127ab86..08cf56f0 100644 --- a/service/test/unit/config/test_register.py +++ b/service/test/unit/config/test_register.py @@ -1,6 +1,6 @@ import unittest -from pixelated.register import validate_username +from pixelated.register import validate_username, validate_password class TestRegister(unittest.TestCase): @@ -13,9 +13,9 @@ class TestRegister(unittest.TestCase): with self.assertRaises(ValueError): validate_username('invalid@username') - def test_username_at_least_8_characters(self): + def test_password_raises_error_if_shorter_than_8_characters(self): with self.assertRaises(ValueError): - validate_username('short') + validate_password('short') def test_username_pass_when_valid(self): try: |