def test_viewer_has_access(self):
        """Test checking user access control permissions.""" 
        user_1 = FakeUser("*****@*****.**")
        user_2 = FakeUser("*****@*****.**")
        profile_email_1 = util.get_safe_email(user_1)
        profile_email_2 = util.get_safe_email(user_2)

        user_info_1 = models.UserInfo()
        user_info_1.email = user_1.email()
        user_info_1.safe_email = util.get_safe_email(user_1)
        user_info_1.is_reviewer = False
        user_info_1.put()

        user_info_2 = models.UserInfo()
        user_info_2.email = user_2.email()
        user_info_2.safe_email = util.get_safe_email(user_2)
        user_info_2.is_reviewer = True
        user_info_2.put()

        self.assertTrue(
            account_facade.viewer_has_access(user_1, user_1.email())
        )
        self.assertTrue(
            account_facade.viewer_has_access(user_2, user_1.email())
        )
        self.assertFalse(
            account_facade.viewer_has_access(user_1, user_2.email())
        )
    def test_is_reviewer(self):
        """Test recording / reporting user access control permissions."""
        user_1 = FakeUser("*****@*****.**")
        user_2 = FakeUser("*****@*****.**")

        user_info_1 = models.UserInfo()
        user_info_1.email = user_1.email()
        user_info_1.safe_email = util.get_safe_email(user_1)
        user_info_1.is_reviewer = False
        user_info_1.put()

        user_info_2 = models.UserInfo()
        user_info_2.email = user_2.email()
        user_info_2.safe_email = util.get_safe_email(user_2)
        user_info_2.is_reviewer = True
        user_info_2.put()

        self.assertFalse(account_facade.is_reviewer(user_1))
        self.assertTrue(account_facade.is_reviewer(user_2))
    def test_user_info(self):
        """Test the models.UserInfo data model."""
        user_1 = FakeUser("*****@*****.**")
        user_2 = FakeUser("*****@*****.**")

        user_info_1 = models.UserInfo()
        user_info_1.email = user_1.email()
        user_info_1.safe_email = util.get_safe_email(user_1)
        user_info_1.is_reviewer = False
        user_info_1.put()

        user_info_2 = models.UserInfo()
        user_info_2.email = user_2.email()
        user_info_2.safe_email = util.get_safe_email(user_2)
        user_info_2.is_reviewer = True
        user_info_2.put()

        ret_info_1 = models.UserInfo.get_for_user(user_1)
        ret_info_2 = models.UserInfo.get_for_email(user_2.email())

        assert_user_info_equal(self, ret_info_1, user_info_1)
        assert_user_info_equal(self, ret_info_2, user_info_2)
    def test_ensure_user_info(self):
        """Test creation logic for the a models.UserInfo data model."""
        user_1 = FakeUser("*****@*****.**")
        user_2 = FakeUser("*****@*****.**")

        user_info_1 = models.UserInfo()
        user_info_1.email = user_1.email()
        user_info_1.safe_email = util.get_safe_email(user_1)
        user_info_1.is_reviewer = True
        user_info_1.put()

        ret_info_1 = account_facade.ensure_user_info(user_1)
        ret_info_2 = account_facade.ensure_user_info(user_2)

        self.assertEqual(ret_info_1.email, user_1.email())
        self.assertTrue(ret_info_1.is_reviewer)
        self.assertEqual(ret_info_2.email, user_2.email())
        self.assertFalse(ret_info_2.is_reviewer)
def ensure_user_info(target_user):
    """
    Ensure that a models.UserInfo record exists for the target user.

    Ensure that a models.UserInfo record exists for the target user, creating it
    if it does not exist and returning it regardless.

    @param target_user: The user to ensure a UserInfo record exists for.
    @type target_user: google.appengine.api.users.User
    @return: The UserInfo record for the given user.
    @rtype: models.UserInfo
    """
    user_info = models.UserInfo.get_for_user(target_user)
    if not user_info:
        user_info = models.UserInfo()
        user_info.email = target_user.email()
        user_info.safe_email = util.get_safe_email(target_user)
        user_info.is_reviewer = False
        user_info.is_admin = False
        name_parts = util.get_full_name_from_email(target_user.email())
        user_info.first_name = name_parts[0]
        user_info.last_name = name_parts[1]
        user_info.put()
    return user_info