Spaces:
Sleeping
Sleeping
Download tests/test_file_tools.py from KeyBow/MediaMaster-v2: direct link, hf CLI and curl.
- Browser
- Download file 4.64 kB
-
https://huggingface.co/spaces/KeyBow/MediaMaster-v2/resolve/main/tests/test_file_tools.py
- Command line
-
hf download hf://spaces/KeyBow/MediaMaster-v2/tests/test_file_tools.py
-
curl -L -o test_file_tools.py https://huggingface.co/spaces/KeyBow/MediaMaster-v2/resolve/main/tests/test_file_tools.py
4.64 kB
| """ | |
| test_file_tools.py | |
| file_tools.py のユニットテスト | |
| """ | |
| import os | |
| import zipfile | |
| import pytest | |
| import tools.file_tools as ft | |
| # ------------------------------------------------------------------ # | |
| # verify_password | |
| # ------------------------------------------------------------------ # | |
| def test_verify_password_correct(monkeypatch): | |
| """正しいパスワードで True を返すことを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_PASSWORD", "secret123") | |
| assert ft.verify_password("secret123") is True | |
| def test_verify_password_wrong(monkeypatch): | |
| """誤ったパスワードで False を返すことを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_PASSWORD", "secret123") | |
| assert ft.verify_password("wrongpass") is False | |
| def test_verify_password_empty_input(monkeypatch): | |
| """空文字入力で False を返すことを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_PASSWORD", "secret123") | |
| assert ft.verify_password("") is False | |
| def test_verify_password_not_configured(monkeypatch): | |
| """DOWNLOAD_PASSWORD 未設定時は常に False を返すことを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_PASSWORD", "") | |
| assert ft.verify_password("anything") is False | |
| # ------------------------------------------------------------------ # | |
| # list_files | |
| # ------------------------------------------------------------------ # | |
| def test_list_files_returns_sorted_list(tmp_path, monkeypatch): | |
| """ファイル一覧がソートされて返ることを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", str(tmp_path)) | |
| (tmp_path / "c.txt").write_text("") | |
| (tmp_path / "a.txt").write_text("") | |
| (tmp_path / "b.txt").write_text("") | |
| result = ft.list_files() | |
| assert result == ["a.txt", "b.txt", "c.txt"] | |
| def test_list_files_excludes_directories(tmp_path, monkeypatch): | |
| """ディレクトリはファイル一覧に含まれないことを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", str(tmp_path)) | |
| (tmp_path / "file.txt").write_text("") | |
| (tmp_path / "subdir").mkdir() | |
| result = ft.list_files() | |
| assert result == ["file.txt"] | |
| def test_list_files_nonexistent_dir(monkeypatch): | |
| """ディレクトリが存在しない場合は空リストを返すことを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", "/nonexistent/path") | |
| assert ft.list_files() == [] | |
| # ------------------------------------------------------------------ # | |
| # build_zip | |
| # ------------------------------------------------------------------ # | |
| def test_build_zip_creates_valid_zip(tmp_path, monkeypatch): | |
| """ZIPが正常に作成されることを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", str(tmp_path)) | |
| monkeypatch.setattr(ft, "_TMP_DIR", str(tmp_path / "tmp")) | |
| (tmp_path / "sample.txt").write_text("hello") | |
| zip_path = ft.build_zip(["sample.txt"]) | |
| assert os.path.isfile(zip_path) | |
| with zipfile.ZipFile(zip_path, "r") as z: | |
| assert "sample.txt" in z.namelist() | |
| def test_build_zip_multiple_files(tmp_path, monkeypatch): | |
| """複数ファイルのZIPが正常に作成されることを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", str(tmp_path)) | |
| monkeypatch.setattr(ft, "_TMP_DIR", str(tmp_path / "tmp")) | |
| (tmp_path / "a.txt").write_text("aaa") | |
| (tmp_path / "b.txt").write_text("bbb") | |
| zip_path = ft.build_zip(["a.txt", "b.txt"]) | |
| with zipfile.ZipFile(zip_path, "r") as z: | |
| names = z.namelist() | |
| assert "a.txt" in names | |
| assert "b.txt" in names | |
| def test_build_zip_empty_list(monkeypatch, tmp_path): | |
| """空リスト指定時に ValueError が発生することを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", str(tmp_path)) | |
| with pytest.raises(ValueError, match="ファイルが選択されていません"): | |
| ft.build_zip([]) | |
| def test_build_zip_file_not_found(tmp_path, monkeypatch): | |
| """存在しないファイル指定時に ValueError が発生することを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", str(tmp_path)) | |
| monkeypatch.setattr(ft, "_TMP_DIR", str(tmp_path / "tmp")) | |
| with pytest.raises(ValueError, match="ファイルが見つかりません"): | |
| ft.build_zip(["ghost.txt"]) | |
| def test_build_zip_path_traversal(tmp_path, monkeypatch): | |
| """パストラバーサル攻撃が無効化されることを確認""" | |
| monkeypatch.setattr(ft, "DOWNLOAD_FILES_DIR", str(tmp_path)) | |
| monkeypatch.setattr(ft, "_TMP_DIR", str(tmp_path / "tmp")) | |
| with pytest.raises(ValueError, match="ファイルが見つかりません"): | |
| ft.build_zip(["../../etc/passwd"]) | |