[google-appengine] Re: How to set Current User in code?
Before you run your test runner you can set it as an environment variable: USER_MAIL = u...@test.com export USER_MAIL If you are running webtest you can inject a cookie into the environment that will do the same thing as well: env['HTTP_COOKIE'] = 'dev_appserver_login="u...@test.com:False"' False/True denotes if user admin rights. On Thu, Apr 9, 2009 at 5:55 PM, slatvick wrote: > > I need to set a user (or login) automatically in test classes. > > users.User("email") does not work properly and returns new user each > time as I understand. > > Thanks in advance. > PS. Have not found an answer in posts. > > > -- 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 -~--~~~~--~~--~--~---
[google-appengine] Re: How to set Current User in code?
Here is what I do in unit tests: import os def login_as(email): """ Logs in as the user identified by the email. @param email: The email address of the user you want to login as. """ # This is how you login os.environ['USER_EMAIL'] = email On Apr 9, 11:55 am, slatvick wrote: > I need to set a user (or login) automatically in test classes. > > users.User("email") does not work properly and returns new user each > time as I understand. > > Thanks in advance. > PS. Have not found an answer in posts. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: How to set Current User in code?
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. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: How to set Current User in code?
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 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 -~--~~~~--~~--~--~---
[google-appengine] Re: How to set Current User in code?
thanks guys. I do: os.environ['USER_EMAIL'] = "t...@example.com" user = users.get_current_user() and even user.nickname returns right string. BUT, when i want to create connected data in model it does not work. Creating model: .. player = Player(user=users.get_current_user(), ...) # <<- wrong right here player.put() .. it returns: Traceback (most recent call last): File "/base/python_lib/versions/1/google/appengine/ext/webapp/ __init__.py", line 501, in __call__ handler.get(*groups) File "/base/data/home/apps/gpsball/1.332716226397017546/ helloworld.py", line 272, in get player = game.Player() File "/base/data/home/apps/gpsball/1.332716226397017546/ helloworld.py", line 23, in Play er player = Player(user=users.get_current_user(), ... ) File "/base/python_lib/versions/1/google/appengine/ext/db/ __init__.py", line 596, in __i nit__ prop.__set__(self, value) File "/base/python_lib/versions/1/google/appengine/ext/db/ __init__.py", line 396, in __s et__ value = self.validate(value) File "/base/python_lib/versions/1/google/appengine/ext/db/ __init__.py", line 2305, in va lidate value = super(UserProperty, self).validate(value) File "/base/python_lib/versions/1/google/appengine/ext/db/ __init__.py", line 423, in val idate raise BadValueError('Property %s is required' % self.name) BadValueError: Property user is required What's wrong? is this way logining not fully? On Apr 10, 2:56 pm, Alkis Evlogimenos ('Αλκης Ευλογημένος) wrote: > 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 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 -~--~~~~--~~--~--~---
[google-appengine] Re: How to set Current User in code?
Hi slatvick, Could we see the class declaration for Player? It seems like user should be set to a db.UserProperty. Thank you, Jeff 2009/4/11 slatvick > > thanks guys. > > I do: > os.environ['USER_EMAIL'] = "t...@example.com" > user = users.get_current_user() > > and even user.nickname returns right string. > > BUT, when i want to create connected data in model it does not work. > Creating model: > .. > player = Player(user=users.get_current_user(), ...) # <<- wrong right > here > player.put() > .. > > it returns: > Traceback (most recent call last): > File "/base/python_lib/versions/1/google/appengine/ext/webapp/ > __init__.py", line 501, in > __call__ >handler.get(*groups) > File "/base/data/home/apps/gpsball/1.332716226397017546/ > helloworld.py", line 272, in get >player = game.Player() > File "/base/data/home/apps/gpsball/1.332716226397017546/ > helloworld.py", line 23, in Play > er >player = Player(user=users.get_current_user(), ... ) > File "/base/python_lib/versions/1/google/appengine/ext/db/ > __init__.py", line 596, in __i > nit__ >prop.__set__(self, value) > File "/base/python_lib/versions/1/google/appengine/ext/db/ > __init__.py", line 396, in __s > et__ >value = self.validate(value) > File "/base/python_lib/versions/1/google/appengine/ext/db/ > __init__.py", line 2305, in va > lidate >value = super(UserProperty, self).validate(value) > File "/base/python_lib/versions/1/google/appengine/ext/db/ > __init__.py", line 423, in val > idate >raise BadValueError('Property %s is required' % self.name) > BadValueError: Property user is required > > > What's wrong? is this way logining not fully? > > On Apr 10, 2:56 pm, Alkis Evlogimenos ('Αλκης Ευλογημένος) > wrote: > > 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 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 -~--~~~~--~~--~--~---
[google-appengine] Re: How to set Current User in code?
Yeah, sure. How to set it? On Apr 13, 8:31 pm, Jeff S wrote: > Hi slatvick, > > Could we see the class declaration for Player? It seems like user should be > set to a db.UserProperty. > > Thank you, > > Jeff > > 2009/4/11 slatvick > > > > > thanks guys. > > > I do: > > os.environ['USER_EMAIL'] = "t...@example.com" > > user = users.get_current_user() > > > and even user.nickname returns right string. > > > BUT, when i want to create connected data in model it does not work. > > Creating model: > > .. > > player = Player(user=users.get_current_user(), ...) # <<- wrong right > > here > > player.put() > > .. > > > it returns: > > Traceback (most recent call last): > > File "/base/python_lib/versions/1/google/appengine/ext/webapp/ > > __init__.py", line 501, in > > __call__ > > handler.get(*groups) > > File "/base/data/home/apps/gpsball/1.332716226397017546/ > > helloworld.py", line 272, in get > > player = game.Player() > > File "/base/data/home/apps/gpsball/1.332716226397017546/ > > helloworld.py", line 23, in Play > > er > > player = Player(user=users.get_current_user(), ... ) > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > __init__.py", line 596, in __i > > nit__ > > prop.__set__(self, value) > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > __init__.py", line 396, in __s > > et__ > > value = self.validate(value) > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > __init__.py", line 2305, in va > > lidate > > value = super(UserProperty, self).validate(value) > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > __init__.py", line 423, in val > > idate > > raise BadValueError('Property %s is required' % self.name) > > BadValueError: Property user is required > > > > > What's wrong? is this way logining not fully? > > > On Apr 10, 2:56 pm, Alkis Evlogimenos ('Αλκης Ευλογημένος) > > wrote: > > > 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 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 -~--~~~~--~~--~--~---
[google-appengine] Re: How to set Current User in code?
ok, problem is obsolete. On Apr 13, 11:39 pm, slatvick wrote: > Yeah, sure. How to set it? > > On Apr 13, 8:31 pm, Jeff S wrote: > > > Hi slatvick, > > > Could we see the class declaration for Player? It seems like user should be > > set to a db.UserProperty. > > > Thank you, > > > Jeff > > > 2009/4/11 slatvick > > > > thanks guys. > > > > I do: > > > os.environ['USER_EMAIL'] = "t...@example.com" > > > user = users.get_current_user() > > > > and even user.nickname returns right string. > > > > BUT, when i want to create connected data in model it does not work. > > > Creating model: > > > .. > > > player = Player(user=users.get_current_user(), ...) # <<- wrong right > > > here > > > player.put() > > > .. > > > > it returns: > > > Traceback (most recent call last): > > > File "/base/python_lib/versions/1/google/appengine/ext/webapp/ > > > __init__.py", line 501, in > > > __call__ > > > handler.get(*groups) > > > File "/base/data/home/apps/gpsball/1.332716226397017546/ > > > helloworld.py", line 272, in get > > > player = game.Player() > > > File "/base/data/home/apps/gpsball/1.332716226397017546/ > > > helloworld.py", line 23, in Play > > > er > > > player = Player(user=users.get_current_user(), ... ) > > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > > __init__.py", line 596, in __i > > > nit__ > > > prop.__set__(self, value) > > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > > __init__.py", line 396, in __s > > > et__ > > > value = self.validate(value) > > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > > __init__.py", line 2305, in va > > > lidate > > > value = super(UserProperty, self).validate(value) > > > File "/base/python_lib/versions/1/google/appengine/ext/db/ > > > __init__.py", line 423, in val > > > idate > > > raise BadValueError('Property %s is required' % self.name) > > > BadValueError: Property user is required > > > > > > > What's wrong? is this way logining not fully? > > > > On Apr 10, 2:56 pm, Alkis Evlogimenos ('Αλκης Ευλογημένος) > > > wrote: > > > > 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 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 -~--~~~~--~~