'Test role lookup for a user'

def testGetRoles(self):

    auth = current.auth
    UUID = 'TESTAUTOCREATEDROLE'
    role_id = auth.s3_create_role(UUID, uid=UUID)
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    try:
        auth.s3_impersonate('normaluser@example.com')
        user_id = auth.user.id
        auth.s3_assign_role(user_id, role_id, for_pe=None)
        roles = auth.s3_get_roles(user_id)
        assertTrue((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=None)
        assertTrue((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=0)
        assertFalse((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=1)
        assertFalse((role_id in roles))
        auth.s3_withdraw_role(user_id, role_id, for_pe=None)
        auth.s3_assign_role(user_id, role_id, for_pe=0)
        roles = auth.s3_get_roles(user_id)
        assertTrue((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=None)
        assertFalse((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=0)
        assertTrue((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=1)
        assertFalse((role_id in roles))
        auth.s3_withdraw_role(user_id, role_id, for_pe=0)
        auth.s3_assign_role(user_id, role_id, for_pe=1)
        roles = auth.s3_get_roles(user_id)
        assertTrue((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=None)
        assertFalse((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=0)
        assertFalse((role_id in roles))
        roles = auth.s3_get_roles(user_id, for_pe=1)
        assertTrue((role_id in roles))
        auth.s3_withdraw_role(user_id, role_id, for_pe=1)
    finally:
        auth.s3_delete_role(UUID)
        auth.s3_impersonate(None)
