# 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):


import requests_mock
from unittest import mock
from oslo_context import context
from oslo_policy import policy as os_policy
from monasca_api.common.policy import policy_engine
from monasca_api.tests.policy import base
        result = policy_engine.authorize(self.context, action, target)
        self.assertTrue(result)

    @requests_mock.mock()
    def test_authorize_http_false(self, req_mock):
        req_mock.post('http://www.example.com/',
                      text='False')
        action = "example:get_http"
        target = {}
        self.assertRaises(os_policy.PolicyNotAuthorized, policy_engine.authorize,
                          self.context, action, target)

    def test_templatized_authorization(self):
        target_mine = {'project_id': 'fake'}
        target_not_mine = {'project_id': 'another'}
        action = "example:my_file"
        policy_engine.authorize(self.context, action, target_mine)
        self.assertRaises(os_policy.PolicyNotAuthorized, policy_engine.authorize,
                          self.context, action, target_not_mine)

    def test_early_AND_authorization(self):
        action = "example:early_and_fail"
        self.assertRaises(os_policy.PolicyNotAuthorized, policy_engine.authorize,
                          self.context, action, self.target)

    def test_early_OR_authorization(self):
        action = "example:early_or_success"
        policy_engine.authorize(self.context, action, self.target)

    def test_ignore_case_role_check(self):

# 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/policy/test_policy.py
import requests_mock
from unittest import mock
from oslo_context import context
from oslo_policy import policy as os_policy
from monasca_api.common.policy import policy_engine
from monasca_api.tests.policy import base

        result = policy_engine.authorize(self.context, action, target)
        self.assertTrue(result)

    @requests_mock.mock()
    def test_authorize_http_false(self, req_mock):
        req_mock.post('http://www.example.com/',
                      text='False')
        action = "example:get_http"
        target = {}
        self.assertRaises(os_policy.PolicyNotAuthorized, policy_engine.authorize,
                          self.context, action, target)

    def test_templatized_authorization(self):
        target_mine = {'project_id': 'fake'}
        target_not_mine = {'project_id': 'another'}
        action = "example:my_file"
        policy_engine.authorize(self.context, action, target_mine)
        self.assertRaises(os_policy.PolicyNotAuthorized, policy_engine.authorize,
                          self.context, action, target_not_mine)

    def test_early_AND_authorization(self):
        action = "example:early_and_fail"
        self.assertRaises(os_policy.PolicyNotAuthorized, policy_engine.authorize,
                          self.context, action, self.target)

    def test_early_OR_authorization(self):
        action = "example:early_or_success"
        policy_engine.authorize(self.context, action, self.target)

    def test_ignore_case_role_check(self):

        lowercase_action = "example:lowercase_admin"