vendredi 23 janvier 2015

Testing posting a WTForm into Flask, to be stored in a SQLAlchemy database

I am creating some unit-tests to ensure the functionality of my application is maintained when running on other machines (this project will be going to an examiner). However I am having some issues with testing the registration of a user.


Here are some of the relevant parts of the program:


test_flask.py



class ApplicationTest(unittest.TestCase):
def setUp(self):
app.config["SERVER_NAME"] = "TESTING"
app.config["TESTING"] = True
app.config["WTF_CSRF_ENABLED"] = False
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(basedir, "tests/test.db")
self.app_context = app.app_context()
self.app_context.push()
self.app = app.test_client()
db.create_all()

def test_Register(self):
response = self.app.post("/register", data=dict(
name="test er",
username="tester",
email="test@er.com",
password="test",
confirm="test",
phone_number="07905848677",
address="1 Fleet Street",
town="Billericay",
county="Essex",
postcode="CM12 0LE",
date_of_birth="16/08/77",
gender="M"), follow_redirects=True)
print(response.status_code)
print(response.data)
self.assertTrue(response.status_code == 302)




forms.py



class RegistrationForm(Form):
name = StringField("name")
username = StringField("Username")
email = StringField("Email")
password = PasswordField("Password")
confirm = PasswordField("Confirm Password")
phone_number = IntegerField("Phone Number")
address = StringField("Address")
town = StringField("Town")
county = StringField("County")
postcode = StringField("Postcode")
date_of_birth = DateField("Birthday", format="%d/%m/%Y")
gender = RadioField("Gender", choices=[("M", "Male"), ("F", "Female")])




views.py



@app.route("/register", methods=["GET", "POST"])
def register():
registration = forms.RegistrationForm()
if registration.validate_on_submit():
password_table = password.hash_password(registration.confirm.data) # Generates a secure hashed password.
registration.confirm.data = None # Removes the users password from the memory.
new = models.Runner(username=registration.username.data,
name=registration.name.data,
phone_number=registration.phone_number.data,
address=registration.address.data,
town=registration.town.data,
county=registration.county.data,
postcode=registration.postcode.data,
gender=registration.gender.data,
date_of_birth=registration.date_of_birth.data,
hashed_password=password_table[0],
salt=password_table[1],
first_login=True)
db.session.add(new)
db.session.commit()
flash("Account created, please login!", "success")
return redirect(url_for("login"))
else:
return render_template("pages/register.html", form=registration)




models.py



class Runner(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True)
name = db.Column(db.String)
phone_number = db.Column(db.Integer)
address = db.Column(db.String)
town = db.Column(db.String)
county = db.Column(db.String)
postcode = db.Column(db.String)
gender = db.Column(db.String)
date_of_birth = db.Column(db.String)
hashed_password = db.Column(db.String)
salt = db.Column(db.String)
first_login = db.Column(db.Boolean)




When I run the test_Register, it completes successfully however I receive a HTTP 200 response, instead of a HTTP 302 and a redirect to the login page. What am I doing wrong? (Ignore any indentation errors by the way, they are correct, but copy pasting the code in messed some of it up).


Aucun commentaire:

Enregistrer un commentaire