<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.

from st2common.models.db.pack import PackDB
from st2common.persistence.pack import Pack
from tests import FunctionalTest


class PacksControllerTestCase(FunctionalTest):
    @classmethod
    def setUpClass(cls):
        super(PacksControllerTestCase, cls).setUpClass()

        cls.pack_db_1 = PackDB(name='pack1', description='foo', version='0.1.0', author='foo',
                               email='<EMAIL>', ref='pack1')
        cls.pack_db_2 = PackDB(name='pack2', description='foo', version='0.1.0', author='foo',
                               email='<EMAIL>', ref='pack2')
        Pack.add_or_update(cls.pack_db_1)
        Pack.add_or_update(cls.pack_db_2)

    def test_get_all(self):
        resp = self.app.get('/v1/packs')
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(len(resp.json), 2, '/v1/actionalias did not return all aliases.')

    def test_get_one(self):
        # Get by id
        resp = self.app.get('/v1/packs/%s' % (self.pack_db_1.id))
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json['name'], self.pack_db_1.name)

        # Get by name
        resp = self.app.get('/v1/packs/%s' % (self.pack_db_1.ref))
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json['ref'], self.pack_db_1.ref)
        self.assertEqual(resp.json['name'], self.pack_db_1.name)

    def test_get_one_doesnt_exist(self):
        resp = self.app.get('/v1/packs/doesntexistfoo', expect_errors=True)
        self.assertEqual(resp.status_int, 404)<commit_msg>API tests for pack management<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 mock

from st2common.models.api.execution import ActionExecutionAPI
from st2common.models.db.pack import PackDB
from st2common.persistence.pack import Pack
from st2common.services import packs as pack_service
from st2api.controllers.v1.actionexecutions import ActionExecutionsControllerMixin
from tests import FunctionalTest

PACK_INDEX = {
    "test": {
        "version": "0.4",
        "name": "test",
        "repo_url": "https://github.com/StackStorm-Exchange/stackstorm-test",
        "author": "st2-dev",
        "keywords": ["some", "search", "another", "terms"],
        "email": "<EMAIL>",
        "description": "st2 pack to test package management pipeline"
    },
    "test2": {
        "version": "0.5",
        "name": "test2",
        "repo_url": "https://github.com/StackStorm-Exchange/stackstorm-test2",
        "author": "stanley",
        "keywords": ["some", "special", "terms"],
        "email": "<EMAIL>",
        "description": "another st2 pack to test package management pipeline"
    }
}


class PacksControllerTestCase(FunctionalTest):
    @classmethod
    def setUpClass(cls):
        super(PacksControllerTestCase, cls).setUpClass()

        cls.pack_db_1 = PackDB(name='pack1', description='foo', version='0.1.0', author='foo',
                               email='<EMAIL>', ref='pack1')
        cls.pack_db_2 = PackDB(name='pack2', description='foo', version='0.1.0', author='foo',
                               email='<EMAIL>', ref='pack2')
        Pack.add_or_update(cls.pack_db_1)
        Pack.add_or_update(cls.pack_db_2)

    def test_get_all(self):
        resp = self.app.get('/v1/packs')
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(len(resp.json), 2, '/v1/actionalias did not return all aliases.')

    def test_get_one(self):
        # Get by id
        resp = self.app.get('/v1/packs/%s' % (self.pack_db_1.id))
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json['name'], self.pack_db_1.name)

        # Get by name
        resp = self.app.get('/v1/packs/%s' % (self.pack_db_1.ref))
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json['ref'], self.pack_db_1.ref)
        self.assertEqual(resp.json['name'], self.pack_db_1.name)

    def test_get_one_doesnt_exist(self):
        resp = self.app.get('/v1/packs/doesntexistfoo', expect_errors=True)
        self.assertEqual(resp.status_int, 404)

    @mock.patch.object(ActionExecutionsControllerMixin, '_handle_schedule_execution')
    def test_install(self, _handle_schedule_execution):
        _handle_schedule_execution.return_value = ActionExecutionAPI(id='123')
        payload = {'packs': ['some']}

        resp = self.app.post_json('/v1/packs/install', payload)

        self.assertEqual(resp.status_int, 202)
        self.assertEqual(resp.json, {'execution_id': '123'})

    @mock.patch.object(ActionExecutionsControllerMixin, '_handle_schedule_execution')
    def test_uninstall(self, _handle_schedule_execution):
        _handle_schedule_execution.return_value = ActionExecutionAPI(id='123')
        payload = {'packs': ['some']}

        resp = self.app.post_json('/v1/packs/uninstall', payload)

        self.assertEqual(resp.status_int, 202)
        self.assertEqual(resp.json, {'execution_id': '123'})

    @mock.patch.object(pack_service, 'fetch_pack_index', mock.MagicMock(return_value=PACK_INDEX))
    def test_search(self):
        resp = self.app.post_json('/v1/packs/search', {'query': 'test'})

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json, [PACK_INDEX['test'], PACK_INDEX['test2']])

        resp = self.app.post_json('/v1/packs/search', {'query': 'stanley'})

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json, [PACK_INDEX['test2']])

        resp = self.app.post_json('/v1/packs/search', {'query': 'special'})

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json, [PACK_INDEX['test2']])

    @mock.patch.object(pack_service, 'fetch_pack_index', mock.MagicMock(return_value=PACK_INDEX))
    def test_show(self):
        resp = self.app.post_json('/v1/packs/search', {'pack': 'test'})

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json, PACK_INDEX['test'])

        resp = self.app.post_json('/v1/packs/search', {'pack': 'test2'})

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.json, PACK_INDEX['test2'])
{'fraction_non_alphanumeric': 0.13017286537454165, 'fraction_numerical': 0.01898899947616553, 'mean_word_length': 3.409353348729792, 'pattern_counts': {'":': 16, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4618586', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2317, 'n_words': 539}