Hi,
I'm trying build functional tests (with Flask-Testing) over my forms :
def test_register_empty_password(self):
response = self.client.get('/register')
self.assert200(response)
response = self.client.post('/register', data=dict(
email_address= '',
first_name = '',
last_name = '',
password = '',
password_again = ''
), follow_redirects=True)
assert 'Registration failed' in response.data
In need to send back csrf secret but don't know how to do...
Thanks in advance.
It's quite simple you parse the form on the page so that you get the name and value of every hidden field and you send them along with self.client.post in the data. You should be able to get the tags with an html parser like lxml.html with a simple xpath like //input[@type="hidden"]
Assuming you are using Flask-WTF, you don't need to pass the CSRF data as CSRF validation is disabled by default in testing mode: http://packages.python.org/Flask-WTF/#configuring-flask-wtf If you are doing your own CSRF validation I'd suggest a similar solution, except where you need to test the actual validation itself. 2010/8/31 Daniel Neuhäuser <dasdasich@googlemail.com>: > It's quite simple you parse the form on the page so that you get the > name and value of every hidden field and you send them along with > self.client.post in the data. > > You should be able to get the tags with an html parser like lxml.html > with a simple xpath like //input[@type="hidden"] > >
Hi, you should disable CSRF on testing environment [1] [1] http://packages.python.org/Flask-WTF/#configuring-flask-wtf Cheers, Francisco Souza Software developer at Giran and also full time Open source evangelist at full time English: http://www.franciscosouza.net Portuguese: http://www.franciscosouza.com.br Twitter: @franciscosouza +55 27 3026 0264 On Tue, Aug 31, 2010 at 12:44 PM, Jean-Philippe Serafin < jean-philippe.serafin@dev-solutions.fr> wrote: > Hi, > > I'm trying build functional tests (with Flask-Testing) over my forms : > > def test_register_empty_password(self): > response = self.client.get('/register') > self.assert200(response) > response = self.client.post('/register', data=dict( > email_address= '', > first_name = '', > last_name = '', > password = '', > password_again = '' > ), follow_redirects=True) > assert 'Registration failed' in response.data > > In need to send back csrf secret but don't know how to do... > > Thanks in advance. > > >
that's what I did, thanks all! Le mardi 31 août 2010 à 13:10 -0300, Francisco Souza a écrit : > Hi, > you should disable CSRF on testing environment [1] > > [1] http://packages.python.org/Flask-WTF/#configuring-flask-wtf > > Cheers, > Francisco Souza > Software developer at Giran and also full time > Open source evangelist at full time > > English: http://www.franciscosouza.net > Portuguese: http://www.franciscosouza.com.br > Twitter: @franciscosouza > +55 27 3026 0264 > > > On Tue, Aug 31, 2010 at 12:44 PM, Jean-Philippe Serafin > <jean-philippe.serafin@dev-solutions.fr> wrote: > Hi, > > I'm trying build functional tests (with Flask-Testing) over my > forms : > > def test_register_empty_password(self): > response = self.client.get('/register') > self.assert200(response) > response = self.client.post('/register', data=dict( > email_address= '', > first_name = '', > last_name = '', > password = '', > password_again = '' > ), follow_redirects=True) > assert 'Registration failed' in response.data > > In need to send back csrf secret but don't know how to do... > > Thanks in advance. > > >