# Path: monasca_api/common/policy/policy_engine.py
# CONF = cfg.CONF
# LOG = logging.getLogger(__name__)
# POLICIES = None
# USER_BASED_RESOURCES = ['os-keypairs']
# KEY_EXPR = re.compile(r'%\((\w+)\)s')
# _ENFORCER = None
# DEFAULT_POLICY_FILE = 'policy.yaml'
#         _ENFORCER = None
#         _ENFORCER = policy.Enforcer(CONF,
#                                     policy_file=policy_file,
#                                     rules=rules,
#                                     default_rule=default_rule,
#                                     use_conf=use_conf
#                                     )
# def reset():
# def init(policy_file=None, rules=None, default_rule=None, use_conf=True):
# def _serialize_rules(rules):
# def _warning_for_deprecated_user_based_rules(rules):
# def register_rules(enforcer):
# def authorize(context, action, target, do_raise=True):
# def check_is_admin(context):
# def set_rules(rules, overwrite=True, use_conf=False):  # pragma: no cover
# def verify_deprecated_policy(old_policy, new_policy, default_rule, context):
# def get_rules():
# def get_enforcer():
#     def __init__(self, kind, match):
#     def __call__(self, target, creds, enforcer):
# class IsAdminCheck(policy.Check):


from falcon import testing
from oslo_context import context
from oslo_policy import policy as os_policy
from monasca_api.api.core import request
from monasca_api.common.policy import policy_engine as policy
from monasca_api.policies import roles_list_to_check_str
from monasca_api.tests import base


class TestPolicyCase(base.BaseTestCase):
    def setUp(self):
        super(TestPolicyCase, self).setUp()
        rules = [
            os_policy.RuleDefault("true", "@"),
            os_policy.RuleDefault("example:allowed", "@"),
            os_policy.RuleDefault("example:denied", "!"),
            os_policy.RuleDefault("example:lowercase_monasca_user",
                                  "role:monasca_user or role:sysadmin"),
            os_policy.RuleDefault("example:uppercase_monasca_user",
                                  "role:MONASCA_USER or role:sysadmin"),
        ]
        policy.reset()
        policy.init()
        policy._ENFORCER.register_defaults(rules)

    def test_authorize_nonexist_action_throws(self):
        action = "example:noexist"
        ctx = request.Request(
            testing.create_environ(
                path="/",
                headers={
                    "X_USER_ID": "fake",
                    "X_PROJECT_ID": "fake",
                    "X_ROLES": "member"
                }
            )
        )

# Path: monasca_api/common/policy/policy_engine.py
# CONF = cfg.CONF
# LOG = logging.getLogger(__name__)
# POLICIES = None
# USER_BASED_RESOURCES = ['os-keypairs']
# KEY_EXPR = re.compile(r'%\((\w+)\)s')
# _ENFORCER = None
# DEFAULT_POLICY_FILE = 'policy.yaml'
#         _ENFORCER = None
#         _ENFORCER = policy.Enforcer(CONF,
#                                     policy_file=policy_file,
#                                     rules=rules,
#                                     default_rule=default_rule,
#                                     use_conf=use_conf
#                                     )
# def reset():
# def init(policy_file=None, rules=None, default_rule=None, use_conf=True):
# def _serialize_rules(rules):
# def _warning_for_deprecated_user_based_rules(rules):
# def register_rules(enforcer):
# def authorize(context, action, target, do_raise=True):
# def check_is_admin(context):
# def set_rules(rules, overwrite=True, use_conf=False):  # pragma: no cover
# def verify_deprecated_policy(old_policy, new_policy, default_rule, context):
# def get_rules():
# def get_enforcer():
#     def __init__(self, kind, match):
#     def __call__(self, target, creds, enforcer):
# class IsAdminCheck(policy.Check):


# Path: monasca_api/tests/test_policy.py
from falcon import testing
from oslo_context import context
from oslo_policy import policy as os_policy
from monasca_api.api.core import request
from monasca_api.common.policy import policy_engine as policy
from monasca_api.policies import roles_list_to_check_str
from monasca_api.tests import base



class TestPolicyCase(base.BaseTestCase):
    def setUp(self):
        super(TestPolicyCase, self).setUp()
        rules = [
            os_policy.RuleDefault("true", "@"),
            os_policy.RuleDefault("example:allowed", "@"),
            os_policy.RuleDefault("example:denied", "!"),
            os_policy.RuleDefault("example:lowercase_monasca_user",
                                  "role:monasca_user or role:sysadmin"),
            os_policy.RuleDefault("example:uppercase_monasca_user",
                                  "role:MONASCA_USER or role:sysadmin"),
        ]
        policy.reset()
        policy.init()
        policy._ENFORCER.register_defaults(rules)

    def test_authorize_nonexist_action_throws(self):
        action = "example:noexist"
        ctx = request.Request(
            testing.create_environ(
                path="/",
                headers={
                    "X_USER_ID": "fake",
                    "X_PROJECT_ID": "fake",
                    "X_ROLES": "member"
                }
            )
        )

        self.assertRaises(os_policy.PolicyNotRegistered, policy.authorize,