Hi Massimo :)

Thanks to your message I could code a tiny python decorator to bypass
auth.requires_login(),
when the code gets tested in web2py (i.e request.function = "_TEST").

There is the code:

def if_test_no_auth():

    def decorator(f):
        if request.function != '_TEST':
            f = auth.requires_login()(f)
        return f

    return decorator

And then use it this way:

@if_test_no_auth()
def index():
    ...

I'm currently working on the environment issue, but your code
eems to suit well :)

Concerning the bu itself, I'm on a tight schedule this month,
but I guess I could take some time to look deeper into web2py's code.

Thanks again :)

--
Romain Boissat
chrooted-universe.org

On Oct 16, 3:45 pm, mdipierro <[email protected]> wrote:
> On Oct 16, 4:32 am, rboissat <[email protected]> wrote:
>
> > Hi,
>
> > I'm using web2py v1.66.2 and I have some issues to run doctest within
> > the admin app.
>
> > Indeed, when a function in a controller is decorated, let's say with
> > @auth.requires_login(),
> > then the tests from doctests are not running and are marked as "[no
> > doctest]".
>
> Yes this is a known bug. I do not have a solution. Any idea?
>
> > I assume this is due to the lack of a running environment while
> > running the test.
>
> > If not decorated, test are running fine. Test are somewhat basic, like>>> 
> > index().has_key('form')
>
> > True
>
> > So, how could I instantiate a dummy environment,
>
> yes:
>
>      from gluon.globals import Request, Response, Session
>      from gluon.compileapp import build_environment
>      env = build_environment(Request(),Response(),Session())
>
> > or maybe simpler, have a conditional decorator
> > ( if request.application != "admin", then decorate the function, else
> > leave undecorated), since my tests
> > are basic and do not require an auth user to call the function.
>
> Yes, you can do this. In controller instead of
>
>     @auth.requires_login()
>     def index(): return dict()
>
> you can do
>
>     def index(): return dict()
>     if (condition):
>           index=auth.requires_login()(index)
>
>
>
> > Thanks for any thoughts about this :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to