Exemplo n.º 1
0
    def _make_or_expr(self, check1, _or, check2):
        """Create an 'or_expr'.

        Join two checks by the 'or' operator.
        """

        return [('or_expr', _checks.OrCheck([check1, check2]))]
Exemplo n.º 2
0
    def test_call_second_true(self):
        rules = [mock.Mock(return_value=False), mock.Mock(return_value=True)]
        check = _checks.OrCheck(rules)

        self.assertTrue(check('target', 'cred', None))
        rules[0].assert_called_once_with('target', 'cred', None)
        rules[1].assert_called_once_with('target', 'cred', None)
Exemplo n.º 3
0
    def test_call_second_true(self):
        rules = [
            _BoolCheck(False),
            _BoolCheck(True),
        ]
        check = _checks.OrCheck(rules)

        self.assertTrue(check('target', 'cred', None))
        self.assertTrue(rules[0].called)
        self.assertTrue(rules[1].called)
Exemplo n.º 4
0
    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.OrCheck([TestCheck()])

        self.assertFalse(check('target', 'cred', None, current_rule="a_rule"))
        self.assertEqual(
            [('target', 'cred', None)],
            results,
        )
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
    def test_str(self):
        check = _checks.OrCheck(['rule1', 'rule2'])

        self.assertEqual('(rule1 or rule2)', str(check))
Exemplo n.º 7
0
    def test_pop_check(self):
        check = _checks.OrCheck(['rule1', 'rule2', 'rule3'])
        rules, check1 = check.pop_check()

        self.assertEqual(['rule1', 'rule2'], check.rules)
        self.assertEqual('rule3', check1)
Exemplo n.º 8
0
    def test_add_check(self):
        check = _checks.OrCheck(['rule1', 'rule2'])
        check.add_check('rule3')

        self.assertEqual(['rule1', 'rule2', 'rule3'], check.rules)
Exemplo n.º 9
0
    def test_init(self):
        check = _checks.OrCheck(['rule1', 'rule2'])

        self.assertEqual(['rule1', 'rule2'], check.rules)