def init_database(): # Create the database and the database table db.create_all() # Insert user data user1 = User(username='******') user1.set_hash('pass') user2 = User(username='******') user2.set_hash('pass') db.session.add(user1) db.session.add(user2) with open("config.json", "r") as config: config = json.load(config) for name, value in config["settings"].items(): s = Settings(setting=name, value=value) db.session.add(s) db.session.commit() # Commit the changes for the users db.session.commit() # initialise() yield db # this is where the testing happens!
def add_admin(username, password): with app.app_context(): user = User() user.username = username user.password = generate_password_hash(password, method="sha256") user.admin_user = True user.insert()
def test_successful_login(self): user = User() user.username = test_user2['username'] user.set_hash(test_user2['password']) user.admin_user = True user.insert() self.driver.get(self.url) self.driver.find_element_by_id("username").send_keys( test_user2['username']) self.driver.find_element_by_id("password").send_keys( test_user2['password']) self.driver.find_element_by_id("submit").click() time.sleep(3) self.assertEqual(self.driver.current_url, 'http://localhost:8943/mock_app/')
def user_add(): """ **Adds a User** adds a user to database. """ context = base_context() if request.method == "POST": username = request.form["name"] password = request.form["password"] admin_user = request.form.get("admin_user") if admin_user == "True": admin_user = True else: admin_user = False has_user = db.session.query( exists().where(User.username == username)).scalar() if has_user is False: new_user = User() new_user.username = username new_user.admin_user = admin_user new_user.set_hash(password) for key in request.form: if key.startswith('role_'): roleid = key.split('_')[1] role = Role.query.get(roleid) new_user.roles.append(role) new_user.insert() return redirect(url_for('admin.user_add')) context['roles'] = Role.query.all() return render_template("admin/add.html", **context)
def new_user(): user = User(username='******') user.set_hash('pass') return user