Unit Testing Controllers with auth.requires_login

226 views
Skip to first unread message

Dmitri Zagidulin

unread,
Oct 5, 2009, 10:00:22 AM10/5/09
to web2py-users
The article on Unit Test Controllers using unittest
(http://www.web2py.com/AlterEgo/default/show/213 )
gives some examples on how to unit-test web2py controllers.

However, how do I test controller functions that are decorated with
auth.requires_login()? When I tried running a test similar to that of
the article, I got a:
NameError: name 'auth' is not defined

Ok, makes sense (although I was surprised that the model, in which
auth
is defined, is not auto-loaded).
I tried putting a
self.model = exec_environment('applications/myapp/models/db.py')
line before the
self.controller =
exec_environment('applications/myapp/controllers/default.py',
request=self.request)
but got an error:
SyntaxError: not enough information to build the url

So, what's the right way to test auth-protected controller functions?
Thanks!

Dmitri Zagidulin

unread,
Oct 5, 2009, 10:34:12 PM10/5/09
to web2py-users
Actually, my main question is simply - how does one use/instantiate
Model code from external files? (Like, if I want to do outsource some
business logic to a module, so that several controllers can reuse it?)

mdipierro

unread,
Oct 5, 2009, 10:59:57 PM10/5/09
to web2py-users
If a function needs to be used by multiple controllers in the same
app, the easy solution is to put it into a model file.

If a function needs to be used by multiple controllers in different
applications than it needs to be defined in a module, imported, passed
the required global variables and called by the controllers.

Although code reuse is good and important it breaks the principle that
each web2py applications should be stand alone (if two apps depend on
each other, they they should be the same app).

One repetition that you cannot avoid is listing in controllers the
functions that you want to expose. This is unavoidable because by
forcing you to code this we try prevent you from having two
controllers expose the same functionality and we prevent hidden
controller actions (expose functions by mistake).


Massimo

Dmitri Zagidulin

unread,
Oct 5, 2009, 11:03:10 PM10/5/09
to web...@googlegroups.com
Ok, I think I understand, so far.

My main question still stands, though - how can I import or
instantiate a model file, externally (say, for unit testing)? And do
you have any suggestions on how to unit test auth-protected controller
functions?

mdipierro

unread,
Oct 5, 2009, 11:34:22 PM10/5/09
to web2py-users
we normally suggest using doctests and int this case you can use
web2py.py -T

if you want to use unittest you should look into

gluon/shell.py exec_environment(...)

The problem is that you are not just testing functions that take an
input and return an output. They needs to have an environment to
work.

Massimo


On Oct 5, 10:03 pm, Dmitri Zagidulin <dzagidu...@gmail.com> wrote:
> Ok, I think I understand, so far.
>
> My main question still stands, though - how can I import or
> instantiate a model file, externally (say, for unit testing)? And do
> you have any suggestions on how to unit test auth-protected controller
> functions?
>
Reply all
Reply to author
Forward
0 new messages