I have a function that receives data from a form via POST. The function works fine bud I don't know how to test it.
I don't know how to send my function an image through the test.
Im guessing I have to create a temporary file but haven't been able to make it work. Maybe there is a better way of doing this?
This is the function I wanna test (app.py):
@app.route('/registering', methods=['GET', 'POST'])
def registering():
if request.method == 'POST':
userCheck = request.form['username']
userCheck2 = request.form['email']
userCheck3 = request.form['password']
userCheck4 = request.form['passwordCheck']
userCheck5 = request.form['phone']
if userCheck == "":
flash('"Username required"')
return render_template('register.html')
userName = users.query.filter_by(userName=userCheck).first()
if userName:
flash('"Username Taken'"")
return render_template('register.html')
if userCheck2 == "":
flash('"Email required"')
return render_template('register.html')
if userCheck5 == "":
flash('"Phone required"')
return render_template('register.html')
if userCheck3 == "":
flash('"Password required"')
return render_template('register.html')
if userCheck4 == "":
flash('"Confirm password"')
return render_template('register.html')
file = request.files['file']
if file and allowed_file(file.filename):
if userCheck3 == userCheck4:
newUser = users(userCheck, userCheck2, userCheck5, userCheck3)
db.session.add(newUser)
db.session.commit()
userName = users.query.filter_by(
userName=userCheck, userPass=userCheck3).first()
session['logged_in'] = True
session['user_id'] = userName.id
#image part
filename = str(userName.id)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename + ".jpg"))
flash('"Registered Successfully"')
return redirect(url_for('friendList'))
else:
flash('"Retype passwords"')
return render_template('register.html')
else:
flash('"Add an image (.jpg)"')
return render_template('register.html')
else:
return render_template('register.html')
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
Here is my test (app_tests.py):
import os
import app
import unittest
import tempfile
class AppTestCase(unittest.TestCase):
def setUp(self):
self.db_fd, app.app.config['DATABASE'] = tempfile.mkstemp()
app.app.config['TESTING'] = True
self.app = app.app.test_client()
def tearDown(self):
os.close(self.db_fd)
os.unlink(app.app.config['DATABASE'])
def registering(self, username, email, password, passwordCheck, phone, file):
return self.app.post('/registering', data=dict(
username=username,
email=email,
password=password,
passwordCheck=passwordCheck,
phone=phone,
file=file
), follow_redirects=True)
def test_registering(self):
#successfully registered
rv = self.registering('TestUser', 'test@test.com', 'passwordTest', 'passwordTest', '900102030')
assert 'Registered Successfully' in rv.data
#existing username/user
rv = self.registering('Hulda', 'hulda@hulda.com', 'passwordHulda', 'passwordHulda', '900102030')
assert 'Username Taken' in rv.data
#no username
rv = self.registering('', 'santana@santana.com', 'passwordSantana', 'passwordSantana', '900102030')
assert 'Username required' in rv.data
#no email
rv = self.registering('Santana', '', 'passwordSantana', 'passwordSantana', '900102030')
assert 'Email required' in rv.data
#no password
rv = self.registering('Santana', 'santana@santana.com', '', 'passwordSantana', '900102030')
assert 'Password required' in rv.data
#no password confirmation
rv = self.registering('Santana', 'santana@santana.com', 'passwordSantana', '', '900102030')
assert 'Confirm password' in rv.data
#no password match
rv = self.registering('Santana', 'santana@santana.com', 'passwordSantana', 'passwordSsssssntana', '900102030')
assert 'Retype passwords' in rv.data
#no phone
rv = self.registering('Santana', 'santana@santana.com', 'passwordSantana', 'passwordSantana', '')
assert 'Phone required' in rv.data
Aucun commentaire:
Enregistrer un commentaire