<commit_before># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import httplib

import six

from st2common.persistence.auth import User
from st2common.models.db.auth import UserDB
from st2tests.fixturesloader import FixturesLoader
from tests.base import APIControllerWithRBACTestCase

http_client = six.moves.http_client

__all__ = [
    'APIControllersRBACTestCase'
]

FIXTURES_PACK = 'generic'
TEST_FIXTURES = {
    'runners': ['testrunner1.yaml'],
    'sensors': ['sensor1.yaml'],
    'actions': ['action1.yaml', 'local.yaml'],
    'aliases': ['alias1.yaml'],
    'rules': ['rule1.yaml'],
    'triggers': ['trigger1.yaml'],
    'triggertypes': ['triggertype1.yaml'],
    'executions': ['execution1.yaml'],
    'liveactions': ['liveaction1.yaml', 'parentliveaction.yaml', 'childliveaction.yaml'],
    'enforcements': ['enforcement1.yaml']
}

MOCK_ACTION_1 = {
    'name': 'ma.dummy.action',
    'pack': 'examples',
    'description': 'test description',
    'enabled': True,
    'entry_point': '/tmp/test/action2.py',
    'runner_type': 'local-shell-script',
    'parameters': {
        'c': {'type': 'string', 'default': 'C1', 'position': 0},
        'd': {'type': 'string', 'default': 'D1', 'immutable': True}
    }
}

MOCK_ACTION_ALIAS_1 = {
    'name': 'alias3',
    'pack': 'aliases',
    'description': 'test description',
    'action_ref': 'core.local',
    'formats': ['a', 'b']
}

MOCK_RULE_1 = {
    'enabled': True,
    'name': 'st2.test.rule2',
    'pack': 'yoyohoneysingh',
    'trigger': {
        'type': 'wolfpack.triggertype-1'
    },
    'criteria': {
        'trigger.k1': {
            'pattern': 't1_p_v',
            'type': 'equals'
        }
    },
    'action': {
        'ref': 'sixpack.st2.test.action',
        'parameters': {
            'ip2': '{{rule.k1}}',
            'ip1': '{{trigger.t1_p}}'
        }
    },
    'description': ''
}


class APIControllersRBACTestCase(APIControllerWithRBACTestCase):
    """
    Test class which hits all the API endpoints which are behind the RBAC wall with a user which
    has no permissions and makes sure API returns access denied.
    """

    register_packs = True
    fixtures_loader = FixturesLoader()

    def setUp(self):
        super(APIControllersRBACTestCase, self).setUp()

        # Register packs
        if self.register_packs:
            self._register_packs()

        self.users = {}

        # Users
        user_1_db = UserDB(name='no_permissions')
        user_1_db = User.add_or_update(user_1_db)
        self.users['no_permissions'] = user_1_db

        # Insert mock objects - those objects are used to test get one, edit and delete operations
        self.models = self.fixtures_loader.save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                                               fixtures_dict=TEST_FIXTURES)

    def test_api_endpoints_behind_rbac_wall(self):
        #  alias_model = self.models['aliases']['alias1.yaml']
        sensor_model = self.models['sensors']['sensor1.yaml']
        rule_model = self.models['rules']['rule1.yaml']
        enforcement_model = self.models['enforcements']['enforcement1.yaml']
        execution_model = self.models['executions']['execution1.yaml']

        supported_endpoints = [
            # Packs
            {
                'path': '/v1/packs',
                'method': 'GET'
            },
            {<commit_msg>Add RBAC API endpoint tests for runner types.<commit_after># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import httplib

import six

from st2common.persistence.auth import User
from st2common.models.db.auth import UserDB
from st2tests.fixturesloader import FixturesLoader
from tests.base import APIControllerWithRBACTestCase

http_client = six.moves.http_client

__all__ = [
    'APIControllersRBACTestCase'
]

FIXTURES_PACK = 'generic'
TEST_FIXTURES = {
    'runners': ['testrunner1.yaml'],
    'sensors': ['sensor1.yaml'],
    'actions': ['action1.yaml', 'local.yaml'],
    'aliases': ['alias1.yaml'],
    'rules': ['rule1.yaml'],
    'triggers': ['trigger1.yaml'],
    'triggertypes': ['triggertype1.yaml'],
    'executions': ['execution1.yaml'],
    'liveactions': ['liveaction1.yaml', 'parentliveaction.yaml', 'childliveaction.yaml'],
    'enforcements': ['enforcement1.yaml']
}

MOCK_RUNNER_1 = {
    'name': 'test-runner-1',
    'description': 'test',
    'enabled': False
}

MOCK_ACTION_1 = {
    'name': 'ma.dummy.action',
    'pack': 'examples',
    'description': 'test description',
    'enabled': True,
    'entry_point': '/tmp/test/action2.py',
    'runner_type': 'local-shell-script',
    'parameters': {
        'c': {'type': 'string', 'default': 'C1', 'position': 0},
        'd': {'type': 'string', 'default': 'D1', 'immutable': True}
    }
}

MOCK_ACTION_ALIAS_1 = {
    'name': 'alias3',
    'pack': 'aliases',
    'description': 'test description',
    'action_ref': 'core.local',
    'formats': ['a', 'b']
}

MOCK_RULE_1 = {
    'enabled': True,
    'name': 'st2.test.rule2',
    'pack': 'yoyohoneysingh',
    'trigger': {
        'type': 'wolfpack.triggertype-1'
    },
    'criteria': {
        'trigger.k1': {
            'pattern': 't1_p_v',
            'type': 'equals'
        }
    },
    'action': {
        'ref': 'sixpack.st2.test.action',
        'parameters': {
            'ip2': '{{rule.k1}}',
            'ip1': '{{trigger.t1_p}}'
        }
    },
    'description': ''
}


class APIControllersRBACTestCase(APIControllerWithRBACTestCase):
    """
    Test class which hits all the API endpoints which are behind the RBAC wall with a user which
    has no permissions and makes sure API returns access denied.
    """

    register_packs = True
    fixtures_loader = FixturesLoader()

    def setUp(self):
        super(APIControllersRBACTestCase, self).setUp()

        # Register packs
        if self.register_packs:
            self._register_packs()

        self.users = {}

        # Users
        user_1_db = UserDB(name='no_permissions')
        user_1_db = User.add_or_update(user_1_db)
        self.users['no_permissions'] = user_1_db

        # Insert mock objects - those objects are used to test get one, edit and delete operations
        self.models = self.fixtures_loader.save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                                               fixtures_dict=TEST_FIXTURES)

    def test_api_endpoints_behind_rbac_wall(self):
        #  alias_model = self.models['aliases']['alias1.yaml']
        sensor_model = self.models['sensors']['sensor1.yaml']
        rule_model = self.models['rules']['rule1.yaml']
        enforcement_model = self.models['enforcements']['enforcement1.yaml']
        execution_model = self.models['executions']['execution1.yaml']

        supported_endpoints = [
            # Runners
            {
                'path': '/v1/runnertypes',
                'method': 'GET'
            },
            {
                'path': '/v1/runnertypes/test-runner-1',
                'method': 'GET'
            },
            {
                'path': '/v1/runnertypes/test-runner-1',
                'method': 'PUT',
                'payload': MOCK_RUNNER_1
            },
            # Packs
            {
                'path': '/v1/packs',
                'method': 'GET'
            },
            {
{'fraction_non_alphanumeric': 0.1330442720219078, 'fraction_numerical': 0.011182108626198083, 'mean_word_length': 2.7045646661031277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873566', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2685, 'n_words': 661}