I switched to using this decorator for my test methods using webtest:
from nose.tools import make_decorator

def credentials(user=False, admin=False):
  def _decorator(func):
    def _wrapper(*args, **kwds):
      old_user = os.environ.get('USER_EMAIL', '')
      old_admin = os.environ.get('USER_IS_ADMIN', '0')

      if user or admin:
        os.environ['USER_EMAIL'] = 'u...@test.com'
      else:
        os.environ['USER_EMAIL'] = ''

      if admin:
        os.environ['USER_IS_ADMIN'] = '1'
      else:
        os.environ['USER_IS_ADMIN'] = '0'

      try:
        func(*args, **kwds)
      finally:
        os.environ['USER_EMAIL'] = old_user
        os.environ['USER_IS_ADMIN'] = old_admin

    _wrapper = make_decorator(func)(_wrapper)
    return _wrapper
  return _decorator

Usage is:

class MyTest(unittest.TestCase):
  def setUp(self):
    self.app = ... make your app here using webtest or any other webtesting
framework

  @credentials
  def testIndexPublic(self):
    response = self.app.get('/')
    self.assertTrue('some string only found when noone has logged in' in
response)

  @credentials(user=True)
  def testIndexUser(self):
    response = self.app.get('/')
    self.assertTrue('some string only found in user logged in rendering' in
response)

  @credentials(admin=True)
  def testIndexAdmin(self):
    response = self.app.get('/')
    self.assertTrue('some string only found in admin rendering' in response)

I hope this helps.

On Fri, Apr 10, 2009 at 12:30 PM, slatvick <slatv...@gmail.com> wrote:

>
> Mahmoud:
> Thanks, but this method does not do loging as the standard login form
> does.
> I already have data connected to "t...@example.com"-user in my model
> and this:
> os.environ['USER_EMAIL'] ="t...@example.com"
> does not logins properly, but redirects to the login-form.
>
> Alkis Evlogimenos:
> Sorry, but where is "env" contains? Because i have "global name 'env'
> is not defined" and cannot find in docs.
> thanks in advance.
>
> >
>


-- 

Alkis

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to