librelist archives

« back to archive

Testing with Flask-WTF

Testing with Flask-WTF

From:
Alex Ezell
Date:
2010-11-16 @ 16:58
I'm running into some troubles trying to write tests that post to URLs where
I have a form using the CSRF implementation in Flask-WTF. It's not clear to
me how I'm supposed to be able to pass the correct value of the CSRF field.
My instinct is to try to override the csrf_enabled attribute of the form
inside the test, but it seems like I'm not really testing the form that my
view is using at that point.

Has anyone tried doing this?

Alex

Re: [flask] Testing with Flask-WTF

From:
Thadeus Burgess
Date:
2010-11-16 @ 17:34
Just set the CSRF_ENABLED config variable to false in your testing
configuration setup pyfile.

Alternatively you can add this to the top of your test...

from flask import current_app

current_app.config['CSRF_ENABLED'] = False


The following settings are used with *Flask-WTF*:


   - CSRF_ENABLED default True




--
Thadeus




On Tue, Nov 16, 2010 at 10:58 AM, Alex Ezell <aezell@gmail.com> wrote:

> I'm running into some troubles trying to write tests that post to URLs
> where I have a form using the CSRF implementation in Flask-WTF. It's not
> clear to me how I'm supposed to be able to pass the correct value of the
> CSRF field. My instinct is to try to override the csrf_enabled attribute of
> the form inside the test, but it seems like I'm not really testing the form
> that my view is using at that point.
>
> Has anyone tried doing this?
>
> Alex
>

Re: [flask] Testing with Flask-WTF

From:
Alex Ezell
Date:
2010-11-16 @ 17:38
Thanks Thadeus. I knew there had to be an easy way. I was just banging my
head against the wall.

Alex

On Tue, Nov 16, 2010 at 11:34 AM, Thadeus Burgess <thadeusb@thadeusb.com>wrote:

> Just set the CSRF_ENABLED config variable to false in your testing
> configuration setup pyfile.
>
> Alternatively you can add this to the top of your test...
>
> from flask import current_app
>
> current_app.config['CSRF_ENABLED'] = False
>
>
> The following settings are used with *Flask-WTF*:
>
>
>    - CSRF_ENABLED default True
>
>
>
>
> --
> Thadeus
>
>
>
>
>
> On Tue, Nov 16, 2010 at 10:58 AM, Alex Ezell <aezell@gmail.com> wrote:
>
>> I'm running into some troubles trying to write tests that post to URLs
>> where I have a form using the CSRF implementation in Flask-WTF. It's not
>> clear to me how I'm supposed to be able to pass the correct value of the
>> CSRF field. My instinct is to try to override the csrf_enabled attribute of
>> the form inside the test, but it seems like I'm not really testing the form
>> that my view is using at that point.
>>
>> Has anyone tried doing this?
>>
>> Alex
>>
>
>

Re: [flask] Testing with Flask-WTF

From:
Francisco Souza
Date:
2010-11-16 @ 23:42
>
> On Tue, Nov 16, 2010 at 3:38 PM, Alex Ezell <aezell@gmail.com> wrote:
> Thanks Thadeus. I knew there had to be an easy way. I was just banging my
> head against the wall.
>

If you are using nose [1], you can disable CSRF in the global function of
your tests (__init__.py file of your tests package):

from flask import current_app

def setup():
>     current_app.config['CSRF_ENABLED'] = False
>

If you are not using nose, I do recommend you use it =P

[1] http://code.google.com/p/python-nose/

Happy hacking,
Francisco Souza
Software developer at Giran and also full time
Open source evangelist at full time

English: http://www.franciscosouza.com
Portuguese: http://www.franciscosouza.com.br
Twitter: @franciscosouza
+55 27 3026 0264


On Tue, Nov 16, 2010 at 3:38 PM, Alex Ezell <aezell@gmail.com> wrote:

> Thanks Thadeus. I knew there had to be an easy way. I was just banging my
> head against the wall.
>
> Alex
>
>
> On Tue, Nov 16, 2010 at 11:34 AM, Thadeus Burgess <thadeusb@thadeusb.com>wrote:
>
>> Just set the CSRF_ENABLED config variable to false in your testing
>> configuration setup pyfile.
>>
>> Alternatively you can add this to the top of your test...
>>
>> from flask import current_app
>>
>> current_app.config['CSRF_ENABLED'] = False
>>
>>
>> The following settings are used with *Flask-WTF*:
>>
>>
>>    - CSRF_ENABLED default True
>>
>>
>>
>>
>> --
>> Thadeus
>>
>>
>>
>>
>>
>> On Tue, Nov 16, 2010 at 10:58 AM, Alex Ezell <aezell@gmail.com> wrote:
>>
>>> I'm running into some troubles trying to write tests that post to URLs
>>> where I have a form using the CSRF implementation in Flask-WTF. It's not
>>> clear to me how I'm supposed to be able to pass the correct value of the
>>> CSRF field. My instinct is to try to override the csrf_enabled attribute of
>>> the form inside the test, but it seems like I'm not really testing the form
>>> that my view is using at that point.
>>>
>>> Has anyone tried doing this?
>>>
>>> Alex
>>>
>>
>>
>

Re: [flask] Testing with Flask-WTF

From:
Alex Ezell
Date:
2010-11-16 @ 23:54
On Tue, Nov 16, 2010 at 5:42 PM, Francisco Souza <
francisco@franciscosouza.net> wrote:

> On Tue, Nov 16, 2010 at 3:38 PM, Alex Ezell <aezell@gmail.com> wrote:
>> Thanks Thadeus. I knew there had to be an easy way. I was just banging my
>> head against the wall.
>>
>
> If you are using nose [1], you can disable CSRF in the global function of
> your tests (__init__.py file of your tests package):
>
>
If you are not using nose, I do recommend you use it =P
>
>
I am using nose and ended up pretty much this exact thing. nose is pretty
great.

Thanks for the feedback.

Alex


> [1] http://code.google.com/p/python-nose/
>
> Happy hacking,
> Francisco Souza
>
>
> On Tue, Nov 16, 2010 at 3:38 PM, Alex Ezell <aezell@gmail.com> wrote:
>
>> Thanks Thadeus. I knew there had to be an easy way. I was just banging my
>> head against the wall.
>>
>> Alex
>>
>>
>> On Tue, Nov 16, 2010 at 11:34 AM, Thadeus Burgess <thadeusb@thadeusb.com>wrote:
>>
>>> Just set the CSRF_ENABLED config variable to false in your testing
>>> configuration setup pyfile.
>>>
>>> Alternatively you can add this to the top of your test...
>>>
>>> from flask import current_app
>>>
>>> current_app.config['CSRF_ENABLED'] = False
>>>
>>>
>>> The following settings are used with *Flask-WTF*:
>>>
>>>
>>>    - CSRF_ENABLED default True
>>>
>>>
>>>
>>>
>>> --
>>> Thadeus
>>>
>>>
>>>
>>>
>>>
>>> On Tue, Nov 16, 2010 at 10:58 AM, Alex Ezell <aezell@gmail.com> wrote:
>>>
>>>> I'm running into some troubles trying to write tests that post to URLs
>>>> where I have a form using the CSRF implementation in Flask-WTF. It's not
>>>> clear to me how I'm supposed to be able to pass the correct value of the
>>>> CSRF field. My instinct is to try to override the csrf_enabled attribute of
>>>> the form inside the test, but it seems like I'm not really testing the form
>>>> that my view is using at that point.
>>>>
>>>> Has anyone tried doing this?
>>>>
>>>> Alex
>>>>
>>>
>>>
>>
>