def InitUser(refresh_token=None): """Initialize application user. Retrieve existing user credentials from datastore or add new user. If a refresh token is specified, it replaces the old refresh token and the date_acquired is updated as well. Args: refresh_token: str A new refresh token received from the auth flow. Defaults to None. Returns: AppUser instance of the application user. """ result = AppUser.query( AppUser.user == users.get_current_user()).fetch(limit=1) if result: # app_user exists app_user = result[0] if not app_user.refresh_token: # update refresh_token if provided in the arguments app_user.refresh_token = refresh_token app_user.date_acquired = datetime.datetime.now() else: app_user = AppUser( user=users.get_current_user(), email=users.get_current_user().email(), refresh_token=refresh_token) app_user.put() return app_user
def testAppUser(self, ident='123', email='*****@*****.**', is_admin='0'): print ' --- App users tests --- ' assert not users.get_current_user() self.testbed.setup_env( USER_EMAIL=email, USER_ID=ident, USER_IS_ADMIN=is_admin, overwrite=True) user = users.get_current_user() assert user.email() == email assert user.user_id() == ident app_user = AppUser(identity=user.user_id(), email=user.email()) assert app_user.identity == user.user_id() assert app_user.email == user.email() app_user_key = app_user.put().urlsafe() retrieved_app_user = get_entity_memcache(app_user_key) assert retrieved_app_user is not None assert retrieved_app_user.identity == user.user_id() assert retrieved_app_user.email == user.email() product1 = Product(name='product1', cost=1) product2 = Product(name='product2', cost=2) product3 = Product(name='product3', cost=3) product1_key = product1.put().urlsafe() product2_key = product2.put().urlsafe() product3_key = product3.put().urlsafe() retrieved_product1 = get_entity_memcache(product1_key) retrieved_product2 = get_entity_memcache(product2_key) retrieved_product3 = get_entity_memcache(product3_key) app_user.add_product(retrieved_product1) app_user.add_product(retrieved_product2) app_user.add_product(retrieved_product3) assert len(app_user.products) > 0 assert len(app_user.products) == 3 app_user.remove_product(retrieved_product1) assert len(app_user.products) > 0 assert len(app_user.products) == 2 app_user.remove_product(retrieved_product2) app_user.remove_product(retrieved_product3) assert len(app_user.products) == 0
def testAppUser(self, ident='123', email='*****@*****.**', is_admin='0'): print ' --- App users tests --- ' assert not users.get_current_user() self.testbed.setup_env(USER_EMAIL=email, USER_ID=ident, USER_IS_ADMIN=is_admin, overwrite=True) user = users.get_current_user() assert user.email() == email assert user.user_id() == ident app_user = AppUser(identity=user.user_id(), email=user.email()) assert app_user.identity == user.user_id() assert app_user.email == user.email() app_user_key = app_user.put().urlsafe() retrieved_app_user = get_entity_memcache(app_user_key) assert retrieved_app_user is not None assert retrieved_app_user.identity == user.user_id() assert retrieved_app_user.email == user.email() product1 = Product(name='product1', cost=1) product2 = Product(name='product2', cost=2) product3 = Product(name='product3', cost=3) product1_key = product1.put().urlsafe() product2_key = product2.put().urlsafe() product3_key = product3.put().urlsafe() retrieved_product1 = get_entity_memcache(product1_key) retrieved_product2 = get_entity_memcache(product2_key) retrieved_product3 = get_entity_memcache(product3_key) app_user.add_product(retrieved_product1) app_user.add_product(retrieved_product2) app_user.add_product(retrieved_product3) assert len(app_user.products) > 0 assert len(app_user.products) == 3 app_user.remove_product(retrieved_product1) assert len(app_user.products) > 0 assert len(app_user.products) == 2 app_user.remove_product(retrieved_product2) app_user.remove_product(retrieved_product3) assert len(app_user.products) == 0