Spaces:
Sleeping
Sleeping
File size: 4,635 Bytes
aadb6d9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | """
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"])
|