def login(self): if request.method == 'POST': form = LoginForm(request.form) if helpers.validate_form_on_submit(form): db_handler = DBHandler() admin_user = db_handler.get_admin_user(form.username.data) login_user(admin_user) return redirect(url_for('admin.index')) else: self._template_args['form'] = form else: self._template_args['form'] = LoginForm() self._template_args['form_header'] = 'Log in' return super(AdminHomeView, self).index()
def test_insert_admin_user(self): """ Test that the 'insert_admin_user' function can insert a new admin user when the parent user exists. """ with app.app_context(): # Assert that there are no admin users in the db self.assertFalse(AdminUser.query.all()) # Try to create a new admin user admin_user = {'user_id': 'johndoe', 'password': '******'} db_handler = DBHandler() # Assert that the admin user is not created since user with 'johndoe' does not exist self.assertFalse(db_handler.insert_admin_user(admin_user)) # Create a new user self.create_user(id=admin_user['user_id']) # Assert that the admin user is created self.assertTrue(db_handler.insert_admin_user(admin_user)) admin_user = db_handler.get_admin_user(admin_user['user_id']) self.assertTrue(admin_user) self.assertEqual(admin_user.user_id, 'johndoe') self.assertEqual(len(AdminUser.query.all()), 1)