Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin submission ID validation #435

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions ci/src/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,21 @@


def plugin_reader() -> P:
plugin_files = get_plugin_files()
plugin_file_paths = get_plugin_file_paths()

manifests = []

for plugin in plugin_files:
with open(plugin, "r", encoding="utf-8") as f:
manifest = json.load(f)
manifests.append(manifest)
for plugin_path in plugin_file_paths:
with open(plugin_path, "r", encoding="utf-8") as f:
manifests.append(json.load(f))

return manifests

def get_plugin_files() -> list[str]:
def get_plugin_file_paths() -> list[str]:
return [os.path.join(plugin_dir, filename) for filename in get_plugin_filenames()]

def get_plugin_filenames() -> list[str]:
return [file for file in os.listdir(plugin_dir)]
return os.listdir(plugin_dir)

def etag_reader() -> ETagsType:
with open(etag_file, "r", encoding="utf-8") as f:
Expand Down Expand Up @@ -91,3 +90,13 @@ def check_url(url: str) -> bool:
re.IGNORECASE,
)
return re.match(regex, url) is not None


def get_file_plugins_json_info(required_key: str = "") -> list[dict[str, str]]:
with open("plugins.json", "r", encoding="utf-8") as f:
data = json.load(f)

if not required_key:
return data

return [{required_key: plugin[required_key]} for plugin in data]
23 changes: 21 additions & 2 deletions ci/src/validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*-coding: utf-8 -*-
from _utils import clean, id_name, language_list, language_name, plugin_reader, check_url, icon_path, get_plugin_files, get_plugin_filenames
import uuid

from _utils import (check_url, clean, get_file_plugins_json_info, get_plugin_file_paths, get_plugin_filenames,
icon_path, id_name, language_list, language_name, plugin_reader)

plugin_infos = plugin_reader()

Expand All @@ -25,7 +28,7 @@ def test_valid_icon_url():
assert check_url(plugin[icon_path]), msg

def test_file_type_json():
incorrect_ext_files = [file for file in get_plugin_files() if not file.endswith(".json")]
incorrect_ext_files = [file_path for file_path in get_plugin_file_paths() if not file_path.endswith(".json")]

assert len(incorrect_ext_files) == 0, f"Expected the following file to be of .json extension: {incorrect_ext_files}"

Expand All @@ -35,3 +38,19 @@ def test_file_name_construct():
assert (
f"{info['Name']}-{info['ID']}.json" in filenames
), f"Plugin {info['Name']} with ID {info['ID']} does not have the correct filename. Make sure it's name + ID, i.e. {info['Name']}-{info['ID']}.json"

def test_submitted_plugin_id_is_valid_uuid():
plugins_json_ids = [item["ID"] for item in get_file_plugins_json_info("ID")]
existing_plugin_file_ids = [info["ID"] for info in plugin_infos]

for id in existing_plugin_file_ids:
if id in plugins_json_ids:
continue

try:
uuid.UUID(id, version=4)
outcome = True
except ValueError:
outcome = False

assert outcome is True, f"The submission plugin ID {id} is not a valid v4 UUID"
Loading