def register(): form = RegistrationForm() if form.validate_on_submit(): new_user = User(form.username.data, form.email.data, form.password.data) new_user.save() if login_user(new_user): return jsonify(new_user) return jsonify(anonymous_user_data) form.errors['_status_code'] = 400 return jsonify(**form.errors)
def test_login(self): test_user = User(**self.user_data) test_user.save() self.assert200(self.login(self.user_data['username'], self.user_data['password'])) self.assert400(self.login(self.user_data['username'] + 'x', self.user_data['password'])) self.assert400(self.login(self.user_data['username'], self.user_data['password'] + 'x')) self.assert400(self.login(self.user_data['username'] + 'x', self.user_data['password'] + 'x'))
def test_registration(self): post_data = self.user_data post_data['confirm'] = post_data['password'] self.assert200(self.client.post('/register', data=post_data)) assert len(User.all()) == 1; self.assert400(self.client.post('/register', data=post_data)) self.assert200(self.login(self.user_data['username'], self.user_data['password']))
def login(): form = LoginForm() if form.validate_on_submit(): form_user = User.get_user(form.username.data) if form_user and form_user.check_password(form.password.data): if login_user(form_user): return jsonify(form_user) else: return jsonify(username=['Your account is currently disabled.'], _status_code=400) else: return jsonify(username=['Invalid username, email or password.'], _status_code=400) return '' form.errors['_status_code'] = 400 return jsonify(**form.errors)
def validate_email(form, field): if User.get_user_by_email(field.data): raise ValidationError('Email already registered.')
def validate_username(form, field): if User.get_user_by_username(field.data): raise ValidationError('Username already registered.');
def load_user(userid): return User.get(userid)