Unit testing and faking authentication

324 views
Skip to first unread message

Keith Edmunds

unread,
Apr 7, 2012, 4:11:53 AM4/7/12
to web...@googlegroups.com
I've been looking that the unit testing page
(http://www.web2py.com/AlterEgo/default/show/260), which is very helpful.
However, most of my controller functions are decorated with
'@auth.requires_login()', and thus if I call them from the test suite,
they fail (issuing a redirect):

File "/home/kae/hg/kae/web2py/gluon/tools.py", line 934, in <lambda>
settings.on_failed_authentication = lambda x: redirect(x)
File "/home/kae/hg/kae/web2py/gluon/http.py", line 128, in redirect
Location=location)
HTTP: 303 SEE OTHER

One could argue that's a "correct" failure because the test suite isn't
authenticated, but I'd like to use unit tests to test functions that need
authentication. I don't want to test the authentication mechanism itself.

Is there a way to fake authentication to allow unit testing? Do others
have a better way of doing this?
--
"You can have everything in life you want if you help enough other people
get what they want" - Zig Ziglar.

Who did you help today?

Anthony

unread,
Apr 7, 2012, 11:24:09 AM4/7/12
to web...@googlegroups.com
For @auth.requires_login() to pass, I think you just need auth.user to exist (and not be None). If your layout includes auth.navbar(), I think the auth.user object at least needs to include an "id" attribute as well (i.e., auth.user.id should be some integer). So maybe add something like this in your test code:

from gluon.storage import Storage
auth
.user = Storage(dict(id=1))

Anthony

Yang

unread,
Nov 6, 2015, 11:17:25 AM11/6/15
to web2py-users
Hello, is there a way to fake the user group and membership as well? Thanks! 
Reply all
Reply to author
Forward
0 new messages