How do you mock the User service in App Engine?
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 t开发者_JAVA百科o 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()
Well I don't think there is an official way to do it, but I have been reading the source code and I found a "hack" way to do it that is working well so far. (Normally I'd be worried about using undocumented behaviour, but it's a test suite so it only matters if it works on the dev server.)
The dev server figures out the currently logged-in user based on three environment variables:
- USER_EMAIL: The user's email address, and the user's nickname.
- USER_ID: The user's unique Google ID (string).
- USER_IS_ADMIN: "0" if the user is non-admin, "1" if the user is an admin.
You can use os.environ
to set these as you would any other environment variable, and they take immediate effect (obviously this won't work on the production server). But you can use them with testbed's user_stub and they will be reset when you deactivate the testbed (which you should do on tearDown
, so you get a fresh environment for each test case).
Since setting environment variables is a bit unwieldy, I wrote some wrapper functions to package them up:
import os
def setCurrentUser(email, user_id, is_admin=False):
os.environ['USER_EMAIL'] = email or ''
os.environ['USER_ID'] = user_id or ''
os.environ['USER_IS_ADMIN'] = '1' if is_admin else '0'
def logoutCurrentUser():
setCurrentUser(None, None)
Here is what worked for me to simulate a logged in user:
self.testbed.setup_env(USER_EMAIL='usermail@gmail.com',USER_ID='1', USER_IS_ADMIN='0')
self.testbed.init_user_stub()
In addition to Bijan's answer:
The actual check in google.appengine.api.users
looks like this:
def is_current_user_admin():
return (os.environ.get('USER_IS_ADMIN', '0')) == '1'
The key is thus to set the environment variable USER_IS_ADMIN
to '1'
. This can be done in multiple ways, but do note that you're modifying a global variable and thus this might affect other code. The key is to do a proper cleanup.
One could use the Mock library to patch os.environ
, use Testbed
or roll their own creative way. I prefer to use Testbed
as it hints that the hack is appengine related. Mock is not included in Python versions before 3.3 so this adds an extra test dependency.
Extra note: When using the unittest
module I prefer to use addCleanup
instead of tearDown
since cleanups are also called when setUp
fails.
Example test:
import unittest
from google.appengine.api import users
from google.appengine.ext import testbed
class AdminTest(unittest.TestCase):
def setUp(self):
tb = testbed.Testbed()
tb.activate()
# ``setup_env`` takes care of the casing ;-)
tb.setup_env(user_is_admin='1')
self.addCleanup(tb.deactivate)
def test_is_current_user_admin(self):
self.assertTrue(users.is_current_user_admin())
Note: Testbed.setup_env
should be called after Testbed.activate
. Testbed
takes a snapshot of os.environ
upon activation, that snapshot is restored upon deactivation. If Testbed.setup_env
is called before activation the real os.environ
is modified instead of the temporary instance, thus effectively polluting the environment.
This behaves as it should:
>>> import os
>>> from google.appengine.ext import testbed
>>>
>>> tb = testbed.Testbed()
>>> tb.activate()
>>> tb.setup_env(user_is_admin='1')
>>> assert 'USER_IS_ADMIN' in os.environ
>>> tb.deactivate()
>>> assert 'USER_IS_ADMIN' not in os.environ
>>>
This pollutes the environment:
>>> import os
>>> from google.appengine.ext import testbed
>>>
>>> tb = testbed.Testbed()
>>> tb.setup_env(user_is_admin='1')
>>> tb.activate()
>>> assert 'USER_IS_ADMIN' in os.environ
>>> tb.deactivate()
>>> assert 'USER_IS_ADMIN' not in os.environ
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
Here's a couple helper functions I created for my tests based on answers here. I stuck them in a test_helper
module:
# tests/test_helper.py
import hashlib
def mock_user(testbed, user_email='test@example.com', is_admin=False):
user_id = hashlib.md5(user_email).hexdigest()
is_admin = str(int(is_admin))
testbed.setup_env(USER_EMAIL=user_email,
USER_ID=user_id,
USER_IS_ADMIN=is_admin,
overwrite=True)
testbed.init_user_stub()
def mock_admin_user(testbed, user_email='admin@example.com'):
mock_user(testbed, user_email, True)
Sample usage (with NoseGAE):
import unittest
from google.appengine.ext import ndb, testbed
from google.appengine.api import users
from tests.test_helper import mock_user, mock_admin_user
class MockUserTest(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
ndb.get_context().clear_cache()
def tearDown(self):
self.testbed.deactivate()
def test_should_mock_user_login(self):
self.assertIsNone(users.get_current_user())
self.assertFalse(users.is_current_user_admin())
mock_user(self.testbed)
user = users.get_current_user()
self.assertEqual(user.email(), 'test@example.com')
self.assertFalse(users.is_current_user_admin())
mock_admin_user(self.testbed)
admin = users.get_current_user()
self.assertEqual(admin.email(), 'admin@example.com')
self.assertTrue(users.is_current_user_admin())
精彩评论