Best Python code snippet using autotest_python
test_complete_rule.py
Source:test_complete_rule.py  
1import importlib.resources2import os3import lark4import pytest5import arpoc.ac6import arpoc.exceptions7from arpoc.ac.common import Effects8test_acls = 'arpoc.tests.resources.acl'9@pytest.fixture10def Container():11    Container = arpoc.ac.AC_Container()12    for f in importlib.resources.contents(test_acls):13        if f.endswith(".json"):14            with importlib.resources.path(test_acls, f) as path_p:15                Container.load_file(path_p)16    return Container17def test_only_init_lark(benchmark):18    benchmark(lark.Lark, arpoc.ac.parser.grammar, start="condition")19def test_alwaysGrant(benchmark, Container):20    context = {"subject": {}, "object": {}, "environment": {}}21    evaluation_result = benchmark(Container.evaluate_by_entity_id,22                                  "com.example.policysets.alwaysGrant",23                                  context)24    assert evaluation_result.results[25        "com.example.policysets.alwaysGrant"] == Effects.GRANT26def test_obligations(Container):27    context = {"subject": {}, "object": {}, "environment": {}}28    print(Container.policy_sets.keys())29    ps = Container.policy_sets["policyset_with_obligation"]30    res = ps.evaluate(context)31    assert res.obligations == ['obl_log_failed']32def test_missing_entities(Container):33    context = {"subject": {}, "object": {}, "environment": {}}34    # A Service with missing policy set should raise an exception35    with pytest.raises(arpoc.exceptions.ACEntityMissing):36        effect, _ = Container.evaluate_by_entity_id("not-existing", context)37    # Everything else should return none38    assert Container.policies["policy_with_missing_rule"].evaluate(39        context).results["policy_with_missing_rule"] is None40    assert Container.policy_sets["policyset_with_missing_policyset"].evaluate(41        context).results["policyset_with_missing_policyset"] is None42    assert Container.policy_sets["policyset_with_missing_policy"].evaluate(43        context).results["policyset_with_missing_policy"] is None44def test_container_to_string(Container):45    container_str = str(Container)46    assert "Policy(" in container_str47    assert "Policy_Set(" in container_str48    assert "Rule" in container_str49def test_broken_json(caplog, Container):50    with importlib.resources.path(test_acls, "broken_json") as path_p:51        Container.load_file(path_p)52    assert "JSON File" in caplog.text53    assert "is no valid json" in caplog.text54def test_broken_definitions(caplog, Container):55    with importlib.resources.path(test_acls, "broken_definitions") as path_p:56        Container.load_file(path_p)57    assert "Probably error in AC Entity Definition" in caplog.text58def test_json_no_ac_format(caplog, Container):59    with importlib.resources.path(test_acls, "json_no_ac_format") as path_p:60        Container.load_file(path_p)61    assert "Error handling file" in caplog.text62def test_loggedIn(benchmark, Container):63    context = {64        "subject": {65            'email': 'admin@example.com'66        },67        "object": {68            'url': 'notSecure'69        },70        "environment": {}71    }72    evaluation_result = benchmark(Container.evaluate_by_entity_id,73                                  "com.example.policysets.loggedIn", context)74    assert evaluation_result.results[75        "com.example.policysets.loggedIn"] == Effects.GRANT76def test_loggedInAdmin(benchmark, Container):77    context = {78        "subject": {79            'email': 'admin@example.com'80        },81        "object": {82            'url': 'admin'83        },84        "environment": {}85    }86    test = "com.example.policysets.loggedIn"87    evaluation_result = benchmark(Container.evaluate_by_entity_id, test,88                                  context)89    assert evaluation_result.results[test] == Effects.GRANT90def test_missing_target_attributes(Container):91    context = { "subject": { }, "object": { }, "environment": {} }92    test = "policy_target_missing_subject_attr"93    evaluation_result = Container.policies[test].evaluate(context)94    assert evaluation_result.results[test] is None95    assert "missing" in evaluation_result.missing_attr96    test = "policy_target_missing_object_attr"97    evaluation_result = Container.policies[test].evaluate(context)98    assert evaluation_result.results[test] is None99    test = "policy_target_missing_environment_attr"100    evaluation_result = Container.policies[test].evaluate(context)101    assert evaluation_result.results[test] is None102    test = "policyset_contain_missing_target_policy"103    evaluation_result = Container.policy_sets[test].evaluate(context)104    assert evaluation_result.results[test] is Effects.GRANT105    assert "missing" in evaluation_result.missing_attr106def test_missing_attributes_rules(Container):107    context = { "subject": { }, "object": { }, "environment": {} }108    test = "rule_target_missing_subject_attr"109    evaluation_result = Container.rules[test].evaluate(context)110    assert evaluation_result.results[test] is None111    assert "missing" in evaluation_result.missing_attr112    test = "rule_target_missing_object_attr"113    evaluation_result = Container.rules[test].evaluate(context)114    assert evaluation_result.results[test] is None115    test = "rule_target_missing_environment_attr"116    evaluation_result = Container.rules[test].evaluate(context)117    assert evaluation_result.results[test] is None118    test = "rule_condition_missing_subject_attr"119    evaluation_result = Container.rules[test].evaluate(context)120    assert evaluation_result.results[test] is Effects.DENY121    assert "missing" in evaluation_result.missing_attr122    test = "rule_condition_missing_object_attr"123    evaluation_result = Container.rules[test].evaluate(context)124    assert evaluation_result.results[test] is Effects.DENY125    test = "rule_condition_missing_environment_attr"126    evaluation_result = Container.rules[test].evaluate(context)127    assert evaluation_result.results[test] is Effects.DENY128    test = "policy_contain_rule_missing_subject_attr_condition"129    evaluation_result = Container.policies[test].evaluate(context)130    assert evaluation_result.results[test] is Effects.DENY131    assert "missing" in evaluation_result.missing_attr132    test = "policy_contain_rule_missing_subject_attr_target"133    evaluation_result = Container.policies[test].evaluate(context)134    assert evaluation_result.results[test] is Effects.GRANT135    assert "missing" in evaluation_result.missing_attr136def test_check(Container):137    assert not Container.check()138def test_normalUser_wants_admin(benchmark, Container):139    context = {140        "subject": {141            'email': 'normaluser@example.com'142        },143        "object": {144            'url': 'admin'145        },146        "environment": {}147    }148    test = "com.example.policysets.loggedIn"149    evaluation_result = benchmark(Container.evaluate_by_entity_id, test,150                                  context)...test_acl_algebra.py
Source:test_acl_algebra.py  
1from .. import algebra2def test_greatest_value_is_returned():3    assert algebra.greater(1, 3) == 34    assert algebra.greater(4, 2) == 45    assert algebra.greater(2, 2) == 26    assert algebra.greater(True, False) is True7def test_greatest_or_zero_value_is_returned():8    assert algebra.greater_or_zero(1, 3) == 39    assert algebra.greater_or_zero(4, 2) == 410    assert algebra.greater_or_zero(2, 2) == 211    assert algebra.greater_or_zero(True, False) is False12    assert algebra.greater_or_zero(2, 0) == 013    assert algebra.greater_or_zero(0, 0) == 014    assert algebra.greater_or_zero(0, 120) == 015def test_lower_value_is_returned():16    assert algebra.lower(1, 3) == 117    assert algebra.lower(4, 2) == 218    assert algebra.lower(2, 2) == 219    assert algebra.lower(True, False) is False20def test_lowest_non_zero_value_is_returned():21    assert algebra.lower_non_zero(1, 3) == 122    assert algebra.lower_non_zero(0, 2) == 223    assert algebra.lower_non_zero(1, 2) == 124    assert algebra.lower_non_zero(0, 0) == 025def test_acls_are_be_added_together():26    test_acls = [27        {28            "can_see": 0,29            "can_hear": 0,30            "max_speed": 10,31            "min_age": 16,32            "speed_limit": 50,33        },34        {"can_see": 1, "can_hear": 0, "max_speed": 40, "min_age": 20, "speed_limit": 0},35        {36            "can_see": 0,37            "can_hear": 1,38            "max_speed": 80,39            "min_age": 18,40            "speed_limit": 40,41        },42    ]43    defaults = {44        "can_see": 0,45        "can_hear": 0,46        "max_speed": 30,47        "min_age": 18,48        "speed_limit": 60,49    }50    acl = algebra.sum_acls(51        defaults,52        acls=test_acls,53        can_see=algebra.greater,54        can_hear=algebra.greater,55        max_speed=algebra.greater,56        min_age=algebra.lower,57        speed_limit=algebra.greater_or_zero,58    )59    assert acl["can_see"] == 160    assert acl["can_hear"] == 161    assert acl["max_speed"] == 8062    assert acl["min_age"] == 16...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
