I am using the Google App Engine testbed
framework to write test cases with mock objects. This is documented here. I've got my datastore tests working nicely using the mock database (Testbed.init_datastore_v3_stub
), and this lets my test cases run over a fast, fresh database which is re-initialised for each test case. Now I want to test functionality that depends on the current user.
There is another testbed service called Testbed.init_user_stub
, which I can activate to get the "fake" user service. Unfortunately, there doesn't seem to be any documentation for this one. I am activating and using it like this:
import unittest
from google.appengine.ext import testbed
from google.appengine.api import users
class MyTest(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_user_stub()
def testUser(self):
u = users.get_current_user()
self.assertNotEqual(u, None)
The problem is that I haven't found any way to tell the "fake" user service to authenticate a "fake" user. So running that test, I (predictably) get
AssertionError: None == None
meaning the fake user service is telling my app that the current user is not logged in. How can I tell the fake user service to pretend that a user is logged in? Ideally, I'd like to be able to specify the fake user's nickname, email, user_id and whether or not they are an admin. It seems like this would be quite a common thing to want (since you need to test how the app behaves when a) nobody is logged in, b) a user is logged in, and c) an admin is logged in), but googling "init_user_stub" returns almost nothing.
Note: If you want to test the above program, you need to add this to the top:
import sys
sys.path.append('/PATH/TO/APPENGINE/SDK')
import dev_appserver
dev_appserver.fix_sys_path()
and this to the bottom:
if __name__ == '__main__':
unittest.main()