Skip to content

Commit

Permalink
more tests for plugin zip uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
pieroit committed Sep 8, 2023
1 parent 64aedc3 commit 28d77fb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
9 changes: 4 additions & 5 deletions core/cat/routes/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions core/tests/mad_hatter/test_mad_hatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 10 additions & 21 deletions core/tests/mad_hatter/test_plugin_extractor.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion core/tests/routes/plugins/fixture_just_installed_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion core/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 28d77fb

Please sign in to comment.