Пример #1
0
def test_and_or_permitted():
    """Test evaluating three expressions joined by and and or."""
    expression = 'obj allow user edit and user has_role admin or user has_role superuser'
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(True, 'admin')
    })
    assert result
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'admin')
    })
    assert result is False
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(True, 'superuser')
    })
    assert result
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'superuser')
    })
    assert result
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'nobody')
    })
    assert result is False
Пример #2
0
def test_basic_permitted():
    """Test evaluating a basic single expression."""
    result = permitted('obj allow user edit', {
        'obj': ExampleObject(),
        'user': ExampleUser(True, 'admin')
    })
    assert result
    result = permitted('obj allow user edit', {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'admin')
    })
    assert result is False
Пример #3
0
def test_and_evalute():
    """Test evaluating two expressions joined by and."""
    expression = 'obj allow user edit and user has_role admin'
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(True, 'admin')
    })
    assert result
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'admin')
    })
    assert result is False
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(True, 'superuser')
    })
    assert result is False
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'superuser')
    })
    assert result is False
Пример #4
0
def test_bracket_permitted():
    """Test evaluating three expressions in a complex structure using a bracket."""
    expression = 'obj allow user edit and (user has_role admin or user has_role superuser)'
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(True, 'admin')
    })
    assert result
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(True, 'superuser')
    })
    assert result
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'admin')
    })
    assert result is False
    result = permitted(expression, {
        'obj': ExampleObject(),
        'user': ExampleUser(False, 'superuser')
    })
    assert result is False