Sign in

Google Cookbook - Google App Engine

Testing apps with authentication
Posted by sudhir.j on Sun 07 Dec 2008 in Users API
User ratings:
I've needed to test my apps with authentication... and the combination of GAE Unit and Webtest is great, but doesn't have this feature. So here's what I do.

def login(self, user = "sudhir@xmail.com"):
users.get_current_user = lambda user=user : users.User(user) if user else None
def logout(self):
self.login(None)



This lets you call
login('me@gmail.com')
# run tests that need to work with a user logged in and tests that make
# sure the correct data is stored for each user
logout()



Also, I've noticed this actually overrides the server code itself, if you're using GAEUnit, so you'll have to keep restarting it to check your site in a browser. Avoiding this is easy enough... Just add these in your setUp and tearDown:

def setUp(self):
self.temp_gcu = users.get_current_user
def tearDown(self):
users.get_current_user = self.temp_gcu



This should let you test login with impunity :D

Enjoy.
http://corn-man.blogspot.com/2008/11/testing-app-engine-authentication-with.html

Comments (0)

Sign in to add comment.