If i use signup function using real website i can pass to validate but, when i do test using unittest i cant pass validate function how can i do? almost all answers are i need to modify config setting about app.config['TESTING'] and app.config['WTF_CSRF_ENABLED'] but i cant work it out
controller
@mod_auth.route('/signup/', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if request.method == 'POST':
if not form.validate():
return render_template('/auth/signup.html', form=form)
else:
newuser = User(form.username.data, form.name.data, form.email.data, form.password.data)
db.session.add(newuser)
db.session.commit()
return "sign in the user and redirect to Home" # TODO
elif request.method == 'GET':
return render_template('/auth/signup.html', form=form)
Form
class SignupForm(Form):
username = StringField('Id', [DataRequired("Please enter your id")])
name = StringField('Name', [DataRequired("Please enter your name")])
email = StringField(
'Email',
[Email(),
DataRequired("Please enter your email address."), ], )
password = PasswordField(
'Password',
[DataRequired("Please enter your a password."),
EqualTo('confirm', message="Passwords must match"), ], )
confirm = PasswordField('Re-enter Password', [DataRequired("Repeat Password")])
submit = SubmitField("Register Now")
def __init__(self, *args, **kwargs):
Form.__init__(self, *args, **kwargs)
def validate(self):
if not Form.validate(self):
return False
user = User.query.filter_by(email=self.email.data.lower()).first()
username = User.query.filter_by(username=self.username.data.lower()).first()
if username:
self.username.errors.append("That username is already taken")
return False
if user:
self.email.errors.append("That email is already taken")
return False
else:
return True
Testcase
class TestCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(BASE_DIR, 'test.db')
self.client = app.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_sign_up(self):
id = 'flatcoke'
name = 'taeminkim'
email = 'flatcoke89@gmail.com'
password = 'qwer1234'
response = self.client.post('/auth/signup/',
data=dict(username=id,
name=name,
password=password,
email=email, ),
follow_redirects=True)
print User.query.all()
# None
Aucun commentaire:
Enregistrer un commentaire