'Test requires_approval settings'

def testRequiresApproval(self):

    s3db = current.s3db
    settings = current.deployment_settings
    approval = settings.get_auth_record_approval()
    tables = settings.get_auth_record_approval_required_for()
    org_approval = s3db.get_config('org_organisation', 'requires_approval')
    approval_required = current.auth.permission.requires_approval
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    try:
        settings.auth.record_approval = False
        settings.auth.record_approval_required_for = []
        s3db.configure('org_organisation', requires_approval=True)
        assertFalse(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
        settings.auth.record_approval = True
        settings.auth.record_approval_required_for = []
        s3db.configure('org_organisation', requires_approval=False)
        assertFalse(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
        settings.auth.record_approval = True
        settings.auth.record_approval_required_for = []
        s3db.configure('org_organisation', requires_approval=True)
        assertFalse(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
        settings.auth.record_approval = True
        settings.auth.record_approval_required_for = None
        s3db.configure('org_organisation', requires_approval=True)
        assertTrue(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
        settings.auth.record_approval = True
        settings.auth.record_approval_required_for = ['project_project']
        s3db.configure('org_organisation', requires_approval=True)
        assertFalse(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
        settings.auth.record_approval = True
        settings.auth.record_approval_required_for = ['org_organisation']
        s3db.configure('org_organisation', requires_approval=False)
        assertTrue(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
        settings.auth.record_approval = True
        settings.auth.record_approval_required_for = None
        s3db.configure('org_organisation', requires_approval=False)
        assertFalse(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
        settings.auth.record_approval = True
        settings.auth.record_approval_required_for = None
        s3db.clear_config('org_organisation', 'requires_approval')
        assertFalse(approval_required('org_organisation'))
        s3db.clear_config('org_organisation', 'requires_approval')
    finally:
        settings.auth.record_approval = approval
        settings.auth.record_approval_required_for = tables
        if (org_approval is not None):
            s3db.configure('org_organisation', requires_approval=org_approval)
        current.auth.s3_impersonate(None)
