def test_create_new_api_key(self): self.mox.StubOutWithMock(db_util, 'get_api_key') self.mox.StubOutWithMock(db_util, 'delete_api_key') self.mox.StubOutWithMock(db_util, 'create_new_api_key') db_util.get_api_key('123').AndReturn(True) db_util.delete_api_key('123') db_util.get_api_key(mox.IsA(basestring)).AndReturn(None) db_util.create_new_api_key('123', mox.IsA(basestring)) self.mox.ReplayAll() api_key_util.create_new_api_key('123')
def create_new_api_key(user_id): """Create a new API key and assign it to the given user. Create a new randomly generated API key for the given user, assigning the newly generated API key to the user with the given user_id. @param user_id: The ID of the user to generate the key for. @type user_id: int @return: The newly generated API key. @rtype: str """ if get_api_key(user_id): db_util.delete_api_key(user_id) api_key = generate_new_api_key() return db_util.create_new_api_key(user_id, api_key)