def users(db): """ Create user fixtures. They reset per test. :param db: Pytest fixture :return: SQLAlchemy database session """ db.session.query(User).delete() users = [{ 'role': 'admin', 'email': '*****@*****.**', 'password': '******' }, { 'active': False, 'email': '*****@*****.**', 'password': '******' }] for user in users: db.session.add(User(**user)) db.session.commit() return db
def test_data(db): db.session.query(User).delete() users = [ { 'username': '******', 'password': '******' }, { 'username': '******', 'password': '******' } ] for user in users: db.session.add(User(**user)) db.session.commit() _users = User.query.all() for _u in _users: params = { 'user_id': _u.id, 'topic': random.choice(Comment.TOPICS.keys()), 'text': "A fake comment." } comment = Comment(**params) db.session.add(comment) db.session.commit() return db
def post(): content = request.form user = User() user.role = content.get('role', None) user.username = content.get('username', '') user.password = content.get('password', '') user.email = content.get('email', '') user.first_name = " ".join(word.capitalize() for word in content.get('firstName', '').split()) user.middle_name = " ".join(word.capitalize() for word in content.get('middleName', '').split()) user.last_name = " ".join(word.capitalize() for word in content.get('lastName', '').split()) user.phone_number = content.get('phoneNumber', '') user.unit_number = content.get('unitNumber', '') user.street_address = content.get('streetAddress', '') user.suburb = content.get('suburb', '') user.postcode = content.get('postcode', '') user.state = content.get('state', '') user.country = content.get('country', '') try: user.save() except: return render_json(500, {'message': "An error occurred."}) return render_json(200, {'user': user.to_json()})
def post(self): args = self.reqparse.parse_args() user = User(username=args['username'], password=args['password']) try: user.save() except IntegrityError as err: return {'error_message': err.message}, 409 return {'user': user.serialize()}, 201
def signup(): form = SignupForm() if form.validate_on_submit(): u = User() form.populate_obj(u) u.password = User.encrypt_password(request.form.get('password')) u.save() if login_user(u): flash('Awesome, thanks for signing up!', 'success') return redirect(url_for('user.welcome')) return render_template('user/signup.html', form=form)
def seed(): """ Seed the database with an initial user. :return: User instance """ if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None: return None params = { 'role': 'admin', 'username': app.config['SEED_ADMIN_USERNAME'], 'first_name': app.config['SEED_ADMIN_FIRST_NAME'], 'last_name': app.config['SEED_ADMIN_LAST_NAME'], 'email': app.config['SEED_ADMIN_EMAIL'], 'password': app.config['SEED_ADMIN_PASSWORD'], 'phone_number': app.config['SEED_ADMIN_PHONE_NUMBER'] } return User(**params).save()
def db(app): """ Setup database, executed once per session. :param app: Pytest fixture :return: SQLAlchemy database session """ _db.drop_all() _db.create_all() # Create a single user because a lot of tests do not mutate this user. params = { 'role': 'admin', 'email': '*****@*****.**', 'password': '******', 'coins': 100 } admin = User(**params) _db.session.add(admin) _db.session.commit() return _db