Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine set up a user when using testbed

I'm using the GAE testbed service and when I run users.get_current_user() I get None i.e.

>>> import sys
>>> sys.path.append("/usr/local/google_appengine") # for Mac OS X
>>> from google.appengine.api import users
>>> from google.appengine.ext import testbed
>>> testbed = testbed.Testbed()
>>> testbed.activate()
>>> testbed.init_user_stub()
>>> users.get_current_user() == None
True

This is the expected result. However, I'd like to log in a fake user when running some of my unit tests.

If it's possible (and I expect it would be), how can one log a user in when running testbed from the command line so that get_current_user() returns an actual user?

Thanks for reading.

like image 897
Brian M. Hunt Avatar asked Jun 03 '11 16:06

Brian M. Hunt


People also ask

What is the use of Appstats in Google App Engine?

Appstats allows you to trace all RPC calls for a given request and reports on the time and cost of each call. Optimizing your application's RPC usage may also reduce your bill. See Managing App Resources.

How do I run Python on Google App Engine?

Go to Google App Engine launcher and create a new application. Enter the project ID of your newly created app. Also, provide the folder (local destination) where you wish to store the app locally. Make sure you select the Python 2.7 as your runtime engine.


1 Answers

To simulate a sign in of an admin user, you can call anywhere in your test function:

self.testbed.setup_env(
    USER_EMAIL = '[email protected]',
    USER_ID = '123',
    USER_IS_ADMIN = '1',
    overwrite = True)

Note that you need to use the overwrite=True parameter!

like image 192
psmith Avatar answered Oct 04 '22 00:10

psmith