Skip to content

Commit

Permalink
ADD:增加测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
ShilongLee committed Jul 22, 2024
1 parent 99e7bd6 commit b1592e3
Show file tree
Hide file tree
Showing 12 changed files with 1,608 additions and 19 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ install:

clean:
rm -rf .venv
rm -rf .cache
rm -rf .log
find ./data -name "*.db" -type f -delete
find . -depth -name "__pycache__" -type d -exec rm -rf {} \;

module ?= ''
test: venv install
. .venv/bin/activate; python3 test/main.py -m $(module)

port ?= 8080
run: venv install
Expand All @@ -17,5 +25,6 @@ help:
@echo "Available targets:"
@echo " venv Create a virtual environment"
@echo " install Install dependencies"
@echo " clean Remove the virtual environment"
@echo " clean Remove all build artifacts、venv、 cache、logs and database"
@echo " test Run test cases"
@echo " run Run the application"
19 changes: 1 addition & 18 deletions lib/js/xhs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3532,21 +3532,4 @@ function searchId() {
, r = BigInt(Math.ceil(2147483646 * Math.random()));
return e <<= BigInt(64),
(e += r).toString(36)
}

c = '/api/sns/web/v1/feed'
i = {
"source_note_id": "6671a6c8000000001c022da6",
"image_formats": [
"jpg",
"webp",
"avif"
],
"extra": {
"need_body_topic": "1"
},
"xsec_source": "pc_feed",
"xsec_token": "ABGUiSqwoUQV9De3Q-8fv3Ia43yldx4TE3_XvZtw3UHzY="
}
cookie = 'abRequestId=f4480816-cfc8-515d-a216-1fe55f6f1545; a1=18febcfb0d32wi0kup9zn6f7et1yfssrsmvte9iv530000369192; webId=1b036b4337d43ff92eb070d48d8fb3f4; gid=yYidDSiDW0MyyYidDSiD86j3fqJE38T7UjuIhh2KiWd9y9q8viMMFd888qKjyjJ8KJfddJq8; xsecappid=xhs-pc-web; webBuild=4.21.0; unread={%22ub%22:%226666bce1000000000e032ae6%22%2C%22ue%22:%2266713484000000001c020dd7%22%2C%22uc%22:25}; websectiga=7750c37de43b7be9de8ed9ff8ea0e576519e8cd2157322eb972ecb429a7735d4; sec_poison_id=ec9efd9a-2dac-4d0b-aff1-e25c8bf50286'
console.log(sign(c, i, cookie))
}
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ anyio==4.4.0
beautifulsoup4==4.12.3
bs4==0.0.2
certifi==2024.7.4
charset-normalizer==3.3.2
click==8.1.7
dnspython==2.6.1
email_validator==2.2.0
Expand All @@ -27,6 +28,7 @@ Pygments==2.18.0
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
requests==2.32.3
rich==13.7.1
shellingham==1.5.4
six==1.16.0
Expand All @@ -37,6 +39,7 @@ typer==0.12.3
typing_extensions==4.12.2
ujson==5.10.0
unicorn==2.0.1.post1
urllib3==2.2.2
uvicorn==0.30.1
uvloop==0.19.0
watchfiles==0.22.0
Expand Down
285 changes: 285 additions & 0 deletions test/bilibili.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
import requests
from cookie import HOST, BILIBILI_COOKIE
import unittest
import time

class TestModule(unittest.TestCase):
# 添加账户接口
def test_add_account(self):
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 账户列表接口
def test_account_list(self):
# 添加账户
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 获取账户列表
response = requests.get(f'{HOST}/bilibili/account_list')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']), 0)

# 过期账户接口
def test_expire_account(self):
# 添加账户
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 过期账户
data = {
"id": "test",
}
response = requests.post(f'{HOST}/bilibili/expire_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 获取详情接口
def test_detail(self):
# 添加账户
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 获取详情
param = {
"id" : 'BV1KJ4m137jv'# 抖音官方视频
}
response = requests.get(f'{HOST}/bilibili/detail', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertEqual(response.json()['data']['bvid'], param['id'])

# 获取评论接口
def test_comments(self):
# 添加账户
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 获取评论
param = {
"id" : 'BV1KJ4m137jv'# 抖音官方视频
}
response = requests.get(f'{HOST}/bilibili/comments', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)

time.sleep(1)
# 测试翻页 page_size = 20
offset = 15
limit = 5
param = {
"id" : 'BV1KJ4m137jv', # 抖音官方视频
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/comments', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)
first_page = [comment['rpid'] for comment in response.json()['data']['comments']]

time.sleep(1)
offset = 20
limit = 5
param = {
"id" : 'BV1KJ4m137jv', # 抖音官方视频
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/comments', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)
second_page = [comment['rpid'] for comment in response.json()['data']['comments']]

time.sleep(1)
offset = 15
limit = 10
param = {
"id" : 'BV1KJ4m137jv', # 抖音官方视频
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/comments', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)
self.assertSequenceEqual([comment['rpid'] for comment in response.json()['data']['comments']], first_page + second_page)

# 获取评论回复接口
def test_reply(self):
# 添加账户
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 获取评论回复
param = {
"id" : 'BV1KJ4m137jv', # 抖音官方视频
"comment_id": '228310046880'
}
response = requests.get(f'{HOST}/bilibili/replys', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)

time.sleep(1)
# 测试翻页 page_size = 20
offset = 5
limit = 5
param = {
"id" : 'BV1KJ4m137jv', # 抖音官方视频
"comment_id": '228310046880',
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/replys', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)
first_page = [comment['rpid'] for comment in response.json()['data']['comments']]

time.sleep(1)
offset = 10
limit = 5
param = {
"id" : 'BV1KJ4m137jv', # 抖音官方视频
"comment_id": '228310046880',
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/replys', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)
second_page = [comment['rpid'] for comment in response.json()['data']['comments']]

time.sleep(1)
offset = 5
limit = 10
param = {
"id" : 'BV1KJ4m137jv', # 抖音官方视频
"comment_id": '228310046880',
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/replys', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['comments']), 0)
self.assertSequenceEqual([comment['rpid'] for comment in response.json()['data']['comments']], first_page + second_page)

# 搜索接口
def test_search(self):
# 添加账户
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 搜索
param = {
"keyword" : "白丝"
}
response = requests.get(f'{HOST}/bilibili/search', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']), 0)

# 用户接口
def test_user(self):
# 添加账户
data = {
"id": "test",
"cookie": BILIBILI_COOKIE
}
response = requests.post(f'{HOST}/bilibili/add_account', json=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)

# 获取用户信息
mid = '8047632'
param = {
"id" : mid # bilibili弹幕网官方账号
}
response = requests.get(f'{HOST}/bilibili/user', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertNotEqual(response.json()['data']['mid'], mid)

time.sleep(1)
# 测试翻页 page_size = 10
offset = 25
limit = 5
param = {
"id" : mid, # bilibili弹幕网官方账号
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/user', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['videos']['list']), 0)
first_page = [video['bvid'] for video in response.json()['data']['videos']['list']]

time.sleep(1)
offset = 30
limit = 5
param = {
"id" : mid, # bilibili弹幕网官方账号
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/user', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['videos']['list']), 0)
second_page = [video['bvid'] for video in response.json()['data']['videos']['list']]

time.sleep(1)
offset = 25
limit = 10
param = {
"id" : mid, # bilibili弹幕网官方账号
"offset": offset,
"limit": limit
}
response = requests.get(f'{HOST}/bilibili/user', params=param)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['code'], 0)
self.assertGreater(len(response.json()['data']['videos']['list']), 0)
self.assertSequenceEqual([video['bvid'] for video in response.json()['data']['videos']['list']], first_page + second_page)
9 changes: 9 additions & 0 deletions test/cookie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HOST = 'http://127.0.0.1:8080'

DY_COOKIE = ''
BILIBILI_COOKIE = ""
JD_COOKIE = ''
KS_COOKIE = ''
TB_COOKIE = ''
WEIBO_COOKIE = ''
XHS_COOKIE = ''
Loading

0 comments on commit b1592e3

Please sign in to comment.