def _make_and_expr(self, check1, _and, check2): """Create an 'and_expr'. Join two checks by the 'and' operator. """ return [('and_expr', _checks.AndCheck([check1, check2]))]
def test_call_second_true(self): rules = [mock.Mock(return_value=False), mock.Mock(return_value=True)] check = _checks.AndCheck(rules) self.assertFalse(check('target', 'cred', None)) rules[0].assert_called_once_with('target', 'cred', None) self.assertFalse(rules[1].called)
def test_call_second_true(self): rules = [ _BoolCheck(False), _BoolCheck(True), ] check = _checks.AndCheck(rules) self.assertFalse(check('target', 'cred', None)) self.assertTrue(rules[0].called) self.assertFalse(rules[1].called)
def _mix_or_and_expr(self, or_expr, _and, check): """Modify the case 'A or B and C'""" or_expr, check1 = or_expr.pop_check() if isinstance(check1, _checks.AndCheck): and_expr = check1 and_expr.add_check(check) else: and_expr = _checks.AndCheck([check1, check]) return [('or_expr', or_expr.add_check(and_expr))]
def test_rule_does_not_take_current_rule(self): results = [] class TestCheck(object): def __call__(self, target, cred, enforcer): results.append((target, cred, enforcer)) return False check = _checks.AndCheck([TestCheck()]) self.assertFalse(check('target', 'cred', None, current_rule="a_rule")) self.assertEqual( [('target', 'cred', None)], results, )
def _parse_list_rule(rule): """Translates the old list-of-lists syntax into a tree of Check objects. Provided for backwards compatibility. """ # Empty rule defaults to True if not rule: return _checks.TrueCheck() # Outer list is joined by "or"; inner list by "and" or_list = [] for inner_rule in rule: # Skip empty inner lists if not inner_rule: continue # Handle bare strings if isinstance(inner_rule, six.string_types): inner_rule = [inner_rule] # Parse the inner rules into Check objects and_list = [_parse_check(r) for r in inner_rule] # Append the appropriate check to the or_list if len(and_list) == 1: or_list.append(and_list[0]) else: or_list.append(_checks.AndCheck(and_list)) # If we have only one check, omit the "or" if not or_list: return _checks.FalseCheck() elif len(or_list) == 1: return or_list[0] return _checks.OrCheck(or_list)
def test_str(self): check = _checks.AndCheck(['rule1', 'rule2']) self.assertEqual('(rule1 and rule2)', str(check))
def test_add_check(self): check = _checks.AndCheck(['rule1', 'rule2']) check.add_check('rule3') self.assertEqual(['rule1', 'rule2', 'rule3'], check.rules)
def test_init(self): check = _checks.AndCheck(['rule1', 'rule2']) self.assertEqual(['rule1', 'rule2'], check.rules)