librelist archives

« back to archive

how to do unitest for flask with g and session object

how to do unitest for flask with g and session object

From:
justy
Date:
2013-05-07 @ 09:06
Hi:

I build a simple flask application with some ext module,such as
flask-login,flask-wtform,redis and want to do some basic test for login

in the view.py

    @app.route('/login', methods = ['GET', 'POST'])
    def login():
    form = LoginForm()
    if form.validate_on_submit():
    user = User.query.filter_by(email = form.email.data ).first()
    if user is not None:
    if user.verify_passwrod(form.password.data):
    "> session['user'] = user
    .> login_user(user)
    flash ("Logged in successfully")
    return redirect(url_for('index'))
    else:
    flash ("Authenticate account faield")

    return render_template('login.html',
    title = 'Sign In',
    form = form)


in the index.html

    {% if g.user.is_authenticated()%}
    {% endif %}

test.py
self.app = app.test_client()
def login(self, email, password):
return self.app.post('/login', data=dict( email=email,
password=password), follow_redirects=True)
def test_login_logout(self):
res = self.login('test@test.com', 'test')
assert session.get("user") is not None

When I run this test.py
it may rasie exception for it can't find the g.user object
Is there any alternative method to do this test job?

if I change the test.py like this

from app import app
from flash.ext.login import current_user
self.app=app #app is myproject name

def test_login(self):
with

self.app.test_request_context('/login',method="POST",data={"eamil":"test@test.com","password":"test"}):
assert session.get('user') is not None
# assert current_user.is_authenticated()


the first assert cause failed,dont'know why
and I 'm confused how to get the g ,sessiong and current_user object
with self.app.test_request_context blocks

Any help is appreciated

Best Regards


Re: [flask] how to do unitest for flask with g and session object

From:
dorian i
Date:
2013-05-07 @ 10:18
http://flask.pocoo.org/docs/testing/#other-testing-tricks (read the whole
page, but that link is relevant to your problem)


On Tue, May 7, 2013 at 11:06 AM, justy <justy@show-wise.com> wrote:

>  Hi:
>
> I build a simple flask application with some ext module,such as
> flask-login,flask-wtform,redis and want to do some basic test for login
>
> in the view.py
>
> @app.route('/login', methods = ['GET', 'POST'])
> def login():
>       form = LoginForm()
>       if form.validate_on_submit():
>           user = User.query.filter_by(email = form.email.data ).first()
>           if user is not  None:
>               if  user.verify_passwrod(form.password.data):
> ">                session['user'] = user
> .>                login_user(user)
>                   flash ("Logged in successfully")
>                   return redirect(url_for('index'))
>               else:
>                   flash ("Authenticate account faield")
>
>       return render_template('login.html',
>           title = 'Sign In',
>           form = form)
>
>
> in the index.html
>
> {% if g.user.is_authenticated()%}
> {% endif %}
>
>  test.py
>       self.app = app.test_client()
>       def login(self, email, password):
>             return self.app.post('/login', data=dict( email=email,
> password=password), follow_redirects=True)
>       def test_login_logout(self):
>             res = self.login('test@test.com', 'test')
>             assert session.get("user") is not None
>
> When I run this test.py
>      it may rasie exception for it can't find the g.user object
>      Is there any alternative method to do this test job?
>
> if I change the test.py like this
>
>       from app import app
>       from flash.ext.login import current_user
>       self.app=app #app is myproject name
>
>       def test_login(self):
>       with
> self.app.test_request_context('/login',method="POST",data={"eamil":
> "test@test.com" <test@test.com>,"password":"test"}):
>               assert session.get('user') is not None
>              # assert current_user.is_authenticated()
>
>
> the first assert cause failed,dont'know why
> and I 'm confused how to get the g ,sessiong and current_user object with
> self.app.test_request_context blocks
>
> Any help is appreciated
>
> Best Regards
>
>
>
>

Re: [flask] how to do unitest for flask with g and session object

From:
justy
Date:
2013-05-07 @ 11:27
hi,dorian:

Thanks for your reply..
I have read that article several times before,there are 2 questions 
puzzled me cause I want to test the route and view functions like a web 
browser

1.
    app.test_client().post method

    Can't check the g,and session object

2. with
    app.test_request_context('route',method="POST",data=somedata)
    self.app.dispatch_request()
    Got the same problem as 1

3. Is it wrong to refer the session and g object from the template ?


于 2013-5-7 18:18, dorian i 写道:
> http://flask.pocoo.org/docs/testing/#other-testing-tricks (read the 
> whole page, but that link is relevant to your problem)
>
>
> On Tue, May 7, 2013 at 11:06 AM, justy <justy@show-wise.com 
> <mailto:justy@show-wise.com>> wrote:
>
>     Hi:
>
>     I build a simple flask application with some ext module,such as
>     flask-login,flask-wtform,redis and want to do some basic test for
>     login
>
>     in the view.py
>
>         @app.route('/login', methods = ['GET', 'POST'])
>         def login():
>               form = LoginForm()
>               if form.validate_on_submit():
>                   user = User.query.filter_by(email = form.email.data
>         ).first()
>                   if user is not  None:
>                       if  user.verify_passwrod(form.password.data):
>         ">                session['user'] = user
>         .>                login_user(user)
>                           flash ("Logged in successfully")
>                           return redirect(url_for('index'))
>                       else:
>                           flash ("Authenticate account faield")
>
>               return render_template('login.html',
>                   title = 'Sign In',
>                   form = form)
>
>
>     in the index.html
>
>         {% if g.user.is_authenticated()%}
>         {% endif %}
>
>     test.py
>           self.app = app.test_client()
>           def login(self, email, password):
>                 return self.app.post('/login', data=dict( email=email,
>     password=password), follow_redirects=True)
>           def test_login_logout(self):
>                 res = self.login('test@test.com
>     <mailto:test@test.com>', 'test')
>                 assert session.get("user") is not None
>
>     When I run this test.py
>          it may rasie exception for it can't find the g.user object
>          Is there any alternative method to do this test job?
>
>     if I change the test.py like this
>
>           from app import app
>           from flash.ext.login import current_user
>           self.app=app #app is myproject name
>
>           def test_login(self):
>           with
>     
self.app.test_request_context('/login',method="POST",data={"eamil":"test@test.com"
>     <mailto:test@test.com>,"password":"test"}):
>                   assert session.get('user') is not None
>                  # assert current_user.is_authenticated()
>
>
>     the first assert cause failed,dont'know why
>     and I 'm confused how to get the g ,sessiong and current_user
>     object with self.app.test_request_context blocks
>
>     Any help is appreciated
>
>     Best Regards
>
>
>
>

Re: [flask] how to do unitest for flask with g and session object

From:
dorian i
Date:
2013-05-07 @ 13:30
3. You can use the session/g/request/current_user in templates, but i don't
think it's a good idea for testing (that's why they are avaible in the
template context).

What about this:
http://stackoverflow.com/questions/3697648/how-to-test-the-login-func-in-flask


On Tue, May 7, 2013 at 1:27 PM, justy <justy@show-wise.com> wrote:

>  hi,dorian:
>
> Thanks for your reply..
> I have read that article several times before,there are 2 questions
> puzzled me cause I want to test the route and view functions like a web
> browser
>
> 1.
>    app.test_client().post method
>
>    Can't check the g,and session object
>
> 2. with
>    app.test_request_context('route',method="POST",data=somedata)
>    self.app.dispatch_request()
>    Got the same problem as 1
>
> 3. Is it wrong to refer the session and g object from the template ?
>
>
> 于 2013-5-7 18:18, dorian i 写道:
>
> http://flask.pocoo.org/docs/testing/#other-testing-tricks (read the whole
> page, but that link is relevant to your problem)
>
>
> On Tue, May 7, 2013 at 11:06 AM, justy <justy@show-wise.com> wrote:
>
>>  Hi:
>>
>> I build a simple flask application with some ext module,such as
>> flask-login,flask-wtform,redis and want to do some basic test for login
>>
>> in the view.py
>>
>> @app.route('/login', methods = ['GET', 'POST'])
>> def login():
>>       form = LoginForm()
>>       if form.validate_on_submit():
>>           user = User.query.filter_by(email = form.email.data ).first()
>>           if user is not  None:
>>               if  user.verify_passwrod(form.password.data):
>> ">                session['user'] = user
>> .>                login_user(user)
>>                   flash ("Logged in successfully")
>>                   return redirect(url_for('index'))
>>               else:
>>                   flash ("Authenticate account faield")
>>
>>       return render_template('login.html',
>>           title = 'Sign In',
>>           form = form)
>>
>>
>> in the index.html
>>
>> {% if g.user.is_authenticated()%}
>> {% endif %}
>>
>>  test.py
>>       self.app = app.test_client()
>>       def login(self, email, password):
>>             return self.app.post('/login', data=dict( email=email,
>> password=password), follow_redirects=True)
>>       def test_login_logout(self):
>>             res = self.login('test@test.com', 'test')
>>             assert session.get("user") is not None
>>
>> When I run this test.py
>>      it may rasie exception for it can't find the g.user object
>>      Is there any alternative method to do this test job?
>>
>> if I change the test.py like this
>>
>>       from app import app
>>       from flash.ext.login import current_user
>>       self.app=app #app is myproject name
>>
>>       def test_login(self):
>>       with
>> self.app.test_request_context('/login',method="POST",data={"eamil":
>> "test@test.com" <test@test.com>,"password":"test"}):
>>               assert session.get('user') is not None
>>              # assert current_user.is_authenticated()
>>
>>
>> the first assert cause failed,dont'know why
>> and I 'm confused how to get the g ,sessiong and current_user object with
>> self.app.test_request_context blocks
>>
>> Any help is appreciated
>>
>> Best Regards
>>
>>
>>
>>
>
>