def app(): """Create and configure a new app instance for each test.""" # create a temporary file to isolate the database for each test # db_fd, db_path = tempfile.mkstemp(dir=os.environ['PYTHONPATH']) # create the app with common test config # app = create_app({"TESTING": True, "DATABASE": db_path}) app = create_app({ "TESTING": True, "SQLALCHEMY_DATABASE_URI": os.environ.get('TEST_DATABASE_URL'), }) # create the database and load test data with app.app_context(): init_db() # - get_db().executescript(_data_sql) # INSERT INTO user (username, password) # VALUES # ('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'), # ('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79'); # # INSERT INTO post (title, body, author_id, created) # VALUES # ('test title', 'test' || x'0a' || 'body', 1, '2018-01-01 00:00:00'); db = get_db() test_user = models.User( username='******', password='******') other_user = models.User( username='******', password='******') db.session.add(test_user) db.session.add(other_user) db.session.flush() post = models.Post( title='test title', body='test' + '\n' + 'body', author_id=test_user.id, created='2018-01-01 00:00:00') db.session.add(post) db.session.commit() yield app
def register(): """Register a new user. Validates that the username is not already taken. Hashes the password for security. """ if request.method == "POST": username = request.form["username"] password = request.form["password"] db = get_db() error = None if not username: error = "Username is required." elif not password: error = "Password is required." elif ( # - db.execute("SELECT id FROM user WHERE username = ?", (username,)).fetchone() models.User.query.filter_by(username=username).first() is not None): error = "User {0} is already registered.".format(username) print('error', error) if error is None: # the name is available, store it in the database and go to # the login page # - db.execute( # - "INSERT INTO user (username, password) VALUES (?, ?)", # - (username, generate_password_hash(password)), # - ) # - db.commit() user = models.User(username=username, password=generate_password_hash(password)) db.session.add(user) db.session.commit() return redirect(url_for("auth.login")) flash(error) return render_template("auth/register.html")
def signup(): tmpNickname = "" error = None g.title = "Sign up" if request.method == 'POST': if len(request.form['username']) == 0 or len( request.form['password1']) == 0 or len( request.form['password2']) == 0: error = 'Those information shall not be empty' elif request.form['password1'] != request.form['password2']: error = 'password1 cannot match the password2' elif re.match(re_password_str, request.form['password1']) is None: error = 'The password length should satisfield the range [6-16]' elif re.search(re_uppercase, request.form['password1']) is None: error = 'The password should satisfield at least one capital letter' elif re.search(re_number, request.form['password1']) is None: error = 'The password should satisfield at least one digit' elif re.match(re_email_str, request.form['email']) is None: error = 'The email name is illegal' elif uniqueMail(request.form['email']): newuser = models.User(nickname=request.form['username'], password=request.form['password1'], email=request.form['email']) db.session.add(newuser) db.session.commit() db.session.add(newuser.follow(newuser)) db.session.commit() flash('New account has been successfully signed up') return redirect(url_for('login')) else: tmpNickname = request.form['username'] error = 'The email has been registered' return render_template('signup.html', tmpNickname=tmpNickname, error=error, providers=app.config['OPENID_PROVIDERS'])
def befort_request(): g.search_form = SearchForm() g.user = models.User(nickname="", password="", email="") g.user = current_user g.user.last_seen = datetime.utcnow()
def new_User(test_client): user = models.User(name="Sun") return user