def test_just_message(self): exc1 = InvalidInput("Message") exc2 = InvalidInput(message="Message") self.assertEquals(exc1.message, "Message") self.assertEquals(str(exc1), "Message") self.assertEquals(str(exc1), str(exc2))
def test_direct_value_set(self): """ Directly setting exception values works """ exc = InvalidInput() exc.x = 4 self.assertEquals(exc.x['value'], 4) self.assertEquals(exc.x['message'], '')
def create_user(self, username, password): if password == '': # skip hashing process if the password field is left blank # helpful for creating mock user objects without slowing things down. pass_hash = '' else: pass_hash = bcrypt.hashpw(password, bcrypt.gensalt()) r = get_config('auth_regex', r'^[\d\w]{4,30}$') errors = {} if not re.match(r, username): errors['username'] = { 'message': 'Username not valid', 'value': username } if len(password) < 4: errors['password'] = { 'message': 'Password much be at least 4 characters' } if User.objects.filter(username=username).exists(): errors['username'] = { 'message': 'Username already exists', 'value': username } if errors: raise InvalidInput("User data not valid", **errors) return User.objects.create(username=username, pass_hash=pass_hash)
def basic_register(username, password, password2): """ Register a user and session, and then return the session_key and user. """ if password != password2: raise InvalidInput(password={'message': "Passwords do not match"}, username={'value': username}) user = User.objects.create_user(username, password) return create_session(user.username, password)
def create_session(username, password): """ Create a session for the user, and then return the key. """ user = User.objects.get_user_by_password(username, password) auth_session_engine = get_config('auth_session_engine') if not user: raise InvalidInput('Username or password incorrect') session_key = random_string(15) while auth_session_engine.get(session_key): session_key = random_string(15) auth_session_engine.set(session_key, user.username, get_config('auth_session_expire')) return {'session_key': session_key, 'user': user}
def test_empty_template_safe(self): """ Undefined vars on the exception are treated as empty vars """ exc = InvalidInput("Message", x={'message': "too large", "value": 4}) self.assertEquals([], [x for x in exc['y']])
def test_complex_kwargs(self): exc = InvalidInput("Message", x={'message': "too large", "value": 4}) self.assertEquals(exc.x['message'], "too large") self.assertEquals(exc.x['value'], 4) self.assertEquals(str(exc), "Message")
def test_basic_kwargs(self): exc = InvalidInput("Message", x=3, y=2) self.assertEquals(exc.x['message'], '') self.assertEquals(exc.x['value'], 3)