diff --git a/core/cat/routes/plugins.py b/core/cat/routes/plugins.py index 6d095000..929e5d01 100644 --- a/core/cat/routes/plugins.py +++ b/core/cat/routes/plugins.py @@ -83,13 +83,12 @@ async def install_plugin( ) log(f"Uploading {content_type} plugin {file.filename}", "INFO") - temp = NamedTemporaryFile(delete=False, suffix=file.filename) - contents = file.file.read() - with temp as f: - f.write(contents) + plugin_archive_path = f"/tmp/{file.filename}" + with open(plugin_archive_path, "wb+") as f: + f.write(file.file.read()) background_tasks.add_task( - ccat.mad_hatter.install_plugin, temp.name + ccat.mad_hatter.install_plugin, plugin_archive_path ) return { diff --git a/core/tests/mad_hatter/test_mad_hatter.py b/core/tests/mad_hatter/test_mad_hatter.py index 447fc141..2deb8382 100644 --- a/core/tests/mad_hatter/test_mad_hatter.py +++ b/core/tests/mad_hatter/test_mad_hatter.py @@ -116,9 +116,10 @@ def test_plugin_uninstall_non_existent(mad_hatter: MadHatter): assert active_plugins[0] == "core_plugin" -def test_plugin_uninstall(mad_hatter: MadHatter): +@pytest.mark.parametrize("plugin_is_flat", [True, False]) +def test_plugin_uninstall(mad_hatter: MadHatter, plugin_is_flat): # install plugin - new_plugin_zip_path = create_mock_plugin_zip() + new_plugin_zip_path = create_mock_plugin_zip(flat=plugin_is_flat) mad_hatter.install_plugin(new_plugin_zip_path) # uninstall diff --git a/core/tests/mad_hatter/test_plugin_extractor.py b/core/tests/mad_hatter/test_plugin_extractor.py index e36b9ca4..399955fd 100644 --- a/core/tests/mad_hatter/test_plugin_extractor.py +++ b/core/tests/mad_hatter/test_plugin_extractor.py @@ -1,31 +1,19 @@ import os import shutil +import pytest + from tests.utils import create_mock_plugin_zip from cat.mad_hatter.plugin_extractor import PluginExtractor -# zip file contains just one folder, inside that folder we find the plugin -def test_unpackage_nested_zip(client): - - plugins_folder = "tests/mocks/mock_plugin_folder" - - zip_path = create_mock_plugin_zip() - extractor = PluginExtractor(zip_path) - extracted = extractor.extract(plugins_folder) - assert extracted == plugins_folder + "/mock_plugin" - assert os.path.exists(f"{plugins_folder}/mock_plugin") - assert os.path.exists(f"{plugins_folder}/mock_plugin/mock_tool.py") - - os.remove(zip_path) - shutil.rmtree(f"{plugins_folder}/mock_plugin") - - -# zip file does not contain a folder, but the plugin files directly -def test_unpackage_flat_zip(client): +# plugin_is_flat is False: zip file contains just one folder, inside that folder we find the plugin +# plugin_is_flat is True: zip file does not contain a folder, but the plugin files directly +@pytest.mark.parametrize("plugin_is_flat", [True, False]) +def test_unpackage_zip(client, plugin_is_flat): plugins_folder = "tests/mocks/mock_plugin_folder" - zip_path = create_mock_plugin_zip(flat=True) + zip_path = create_mock_plugin_zip(flat=plugin_is_flat) extractor = PluginExtractor(zip_path) extracted = extractor.extract(plugins_folder) assert extracted == plugins_folder + "/mock_plugin" @@ -36,9 +24,10 @@ def test_unpackage_flat_zip(client): shutil.rmtree(f"{plugins_folder}/mock_plugin") -def test_get_id_and_extension(client): +@pytest.mark.parametrize("plugin_is_flat", [True, False]) +def test_get_id_and_extension(client, plugin_is_flat): - zip_path = create_mock_plugin_zip() + zip_path = create_mock_plugin_zip(flat=plugin_is_flat) extractor = PluginExtractor(zip_path) assert extractor.get_plugin_id() == "mock_plugin" assert extractor.get_extension() == "zip" diff --git a/core/tests/routes/plugins/fixture_just_installed_plugin.py b/core/tests/routes/plugins/fixture_just_installed_plugin.py index 05fed8d3..2cadb678 100644 --- a/core/tests/routes/plugins/fixture_just_installed_plugin.py +++ b/core/tests/routes/plugins/fixture_just_installed_plugin.py @@ -13,7 +13,7 @@ def just_installed_plugin(client): ### executed before each test function # create zip file with a plugin - zip_path = create_mock_plugin_zip() + zip_path = create_mock_plugin_zip(flat=True) zip_file_name = zip_path.split("/")[-1] # mock_plugin.zip in tests/mocks folder # upload plugin via endpoint diff --git a/core/tests/routes/plugins/test_plugins_install_uninstall.py b/core/tests/routes/plugins/test_plugins_install_uninstall.py index 899ac949..785c4404 100644 --- a/core/tests/routes/plugins/test_plugins_install_uninstall.py +++ b/core/tests/routes/plugins/test_plugins_install_uninstall.py @@ -2,7 +2,7 @@ import time import pytest import shutil -from tests.utils import create_mock_plugin_zip, get_embedded_tools +from tests.utils import get_embedded_tools from fixture_just_installed_plugin import just_installed_plugin diff --git a/core/tests/utils.py b/core/tests/utils.py index 3414e999..643d4618 100644 --- a/core/tests/utils.py +++ b/core/tests/utils.py @@ -37,7 +37,7 @@ def key_in_json(key, json): # create a plugin zip out of the mock plugin folder. # - Used to test plugin upload. # - zip can be created flat (plugin files in root dir) or nested (plugin files in zipped folder) -def create_mock_plugin_zip(flat: bool = False): +def create_mock_plugin_zip(flat: bool): if flat: root_dir = "tests/mocks/mock_plugin"