Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# RESTful API For JMComic-Crawler-Python

[![Docker Image CI](https://github.com/qwasd7680/JMComic_Server_API/actions/workflows/docker-image.yml/badge.svg)](https://github.com/qwasd7680/JMComic_Server_API/actions/workflows/docker-image.yml)
[![Pytest](https://github.com/qwasd7680/JMComic_Server_API/actions/workflows/python-app.yml/badge.svg)](https://github.com/qwasd7680/JMComic_Server_API/actions/workflows/python-app.yml)

## 项目介绍

Expand Down
73 changes: 73 additions & 0 deletions tests/test_jmcomic_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os
from pathlib import Path
import jmcomic

current_dir = os.getcwd()
FILE_PATH = Path(f"{current_dir}/temp")
os.makedirs(FILE_PATH, exist_ok=True)

def test_get_comic_info():
testClient = jmcomic.JmOption.default().new_jm_client()
page = testClient.search_site(search_query="1225432")
album: jmcomic.JmAlbumDetail = page.single_album
assert album.title == "[酸菜鱼ゅ°]ヒルチャールに败北した胡桃 表情、台词差分"
assert album.tags == ["全彩","贫乳","调教","中文"]
assert album.views is not None
assert album.likes is not None

def test_rank_comic():
client = jmcomic.JmOption.default().new_jm_client()
page1: jmcomic.JmCategoryPage = client.month_ranking(1)
page2: jmcomic.JmCategoryPage = client.week_ranking(1)
page3: jmcomic.JmCategoryPage = client.day_ranking(1)
assert page1.page_size > 0
assert page2.page_size > 0
assert page3.page_size > 0

def test_comic_download():
optionStr = f"""
client:
cache: null
domain: []
impl: api
postman:
meta_data:
headers: null
impersonate: chrome
proxies: {{}}
type: curl_cffi
retry_times: 5
dir_rule:
base_dir: {FILE_PATH}
rule: Bd_Pname
Comment on lines +41 to +42
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of os.getcwd() combined with Path to construct FILE_PATH and its subsequent use in a configuration string (optionStr) raises concerns about potential directory traversal vulnerabilities. This approach allows for external influence on directory paths, which can be exploited if not properly sanitized.

Recommendation:

  • Use a more controlled method of defining base directories, such as setting a fixed path or using environment variables that are validated against a whitelist of allowed paths. Additionally, ensure that any user input or variable data that forms part of file paths is strictly validated to prevent traversal attacks.

download:
cache: true
image:
decode: true
suffix: null
threading:
image: 30
photo: 8
log: true
plugins:
valid: log
after_album:
- plugin: zip
kwargs:
level: photo
filename_rule: Ptitle
zip_dir: {FILE_PATH}
delete_original_file: true
version: '2.1'
"""
option = jmcomic.create_option_by_str(optionStr)
jmcomic.JmModuleConfig.CLASS_DOWNLOADER = jmcomic.JmDownloader
album_list = jmcomic.download_album(1225432, option)
if not album_list:
raise Exception("Album download failed or returned no results.")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling in test_comic_download is minimal and uses a generic exception without providing detailed error information. This approach can make debugging difficult and does not cater to different failure scenarios that might occur during the download or file operations.

Recommendation:

  • Implement more specific exception handling that can differentiate between various types of errors (e.g., network issues, file access errors). Use custom exceptions or error messages that provide more context about the failure. Additionally, consider logging errors for further analysis.

file_title = album_list[0].title
zip_file_name = f"{file_title}.zip"
zip_file_path = FILE_PATH / zip_file_name
assert zip_file_path.exists() == True
zip_file_path.unlink()
assert zip_file_path.exists() == False