'Test accessible_query with record approval'

def testAccessibleQueryWithRecordApproval(self):

    db = current.db
    auth = current.auth
    acl = auth.permission
    s3db = current.s3db
    settings = current.deployment_settings
    accessible_query = auth.s3_accessible_query
    session = current.session
    table = s3db.pr_person
    otable = s3db.org_organisation
    approval = settings.get_auth_record_approval()
    approval_required = settings.get_auth_record_approval_required_for()
    settings.auth.record_approval = True
    settings.auth.record_approval_required_for = []
    assertEqual = self.assertEqual
    assertNotEqual = self.assertNotEqual
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    try:
        AUTHENTICATED = auth.get_system_roles().AUTHENTICATED
        auth.s3_impersonate('admin@example.com')
        query = accessible_query('read', table, c='pr', f='person')
        expected = (table.id > 0)
        assertEqual(str(query), str(expected))
        auth.s3_impersonate('normaluser@example.com')
        query = accessible_query('read', table, c='pr', f='person')
        assertFalse(('approved_by' in str(query)))
        table = s3db.org_organisation
        auth.s3_impersonate('normaluser@example.com')
        query = accessible_query('read', table, c='org', f='organisation')
        expected = (table.id > 0)
        assertEqual(str(query), str(expected))
        settings.auth.record_approval_required_for = ['org_organisation']
        auth.s3_impersonate('admin@example.com')
        query = accessible_query('read', table, c='org', f='organisation')
        expected = ((table.approved_by != None) | (table.owned_by_user == auth.user.id))
        assertEqual(str(query), str(expected))
        query = accessible_query('review', table, c='org', f='organisation')
        expected = (table.approved_by == None)
        assertEqual(str(query), str(expected))
        query = accessible_query(['read', 'review'], table, c='org', f='organisation')
        expected = (table.id > 0)
        assertEqual(str(query), str(expected))
        auth.s3_impersonate('normaluser@example.com')
        query = accessible_query('read', table, c='org', f='organisation')
        expected = ((table.approved_by != None) | (table.owned_by_user == auth.user.id))
        assertEqual(str(query), str(expected))
        query = accessible_query('review', table, c='org', f='organisation')
        expected = (table.id == 0)
        assertEqual(str(query), str(expected))
        query = accessible_query(['read', 'review'], table, c='org', f='organisation')
        expected = ((table.approved_by != None) | (table.owned_by_user == auth.user.id))
        assertEqual(str(query), str(expected))
        acl.update_acl(AUTHENTICATED, c='org', uacl=((acl.READ | acl.REVIEW) | acl.APPROVE), oacl=(((acl.READ | acl.UPDATE) | acl.REVIEW) | acl.APPROVE))
        acl.update_acl(AUTHENTICATED, t='org_organisation', uacl=(((acl.READ | acl.CREATE) | acl.REVIEW) | acl.APPROVE), oacl=(((acl.READ | acl.UPDATE) | acl.REVIEW) | acl.APPROVE))
        auth.s3_impersonate('normaluser@example.com')
        query = accessible_query('read', table, c='org', f='organisation')
        expected = ((table.approved_by != None) | (table.owned_by_user == auth.user.id))
        assertTrue((str(expected) in str(query)))
        query = accessible_query('review', table, c='org', f='organisation')
        expected = (table.approved_by != None)
        assertFalse((str(expected) in str(query)))
        expected = (table.approved_by == None)
        assertTrue((str(expected) in str(query)))
        query = accessible_query(['read', 'approve'], table, c='org', f='organisation')
        expected = ((table.approved_by != None) | (table.owned_by_user == auth.user.id))
        assertTrue((str(expected) in str(query)))
        expected = (table.approved_by == None)
        assertTrue((str(expected) in str(query)))
        settings.auth.record_approval = False
        query = accessible_query('read', table, c='org', f='organisation')
        expected = (table.id > 0)
        assertEqual(str(query), str(expected))
    finally:
        settings.auth.record_approval = approval
        settings.auth.record_approval_required_for = approval_required
        auth.s3_impersonate(None)
