forked from DeppWang/youdaonote-pull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
608 lines (456 loc) · 19.9 KB
/
test.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
import unittest
import pull
import os
import shutil
import sys
import logging
from markdownify import markdownify as md
# 级别由低到高 DEBUG、INFO,WARN、ERROR
# 设置为
# logging.basicConfig(level=logging.INFO)
__description__ = """
目的,确保 *pull.py* 在各种条件下运行通过
所有测试方法均通过时,才 push
先运行 test_login_right 和 test_cookies_login,再运行 python3 test.py
原来针对每个方法写测试用例,还是太麻烦。
"""
class UserDefine:
username = 'your_youdao_username'
password = 'your_youdao_password'
smms_secret_token = 'your_smms_secret_token'
local_dir = '/Users/deppwang/Documents/youdaonote-pull/test1' # 你本地存放有道云笔记文件的位置
local_zh_Hans_dir = '/Users/deppwang/Documents/youdaonote-pull/测试' # 用于验证目录包含中文时,能否正常下载
def set_right_cookies():
with open('cookies-right.json', 'rb') as f:
byte_cookies = f.read()
with open('cookies.json', 'wb') as f2:
f2.write(byte_cookies)
def set_right_config():
with open('config-right.json', 'rb') as f:
byte_config = f.read()
with open('config.json', 'wb') as f:
f.write(byte_config)
def remove_local_dir():
if os.path.exists(UserDefine.local_dir):
shutil.rmtree(UserDefine.local_dir)
class Test(unittest.TestCase):
def test_html_to_markdown(self):
new_content = md(
f"""<div><span style='color: rgb(68, 68, 68); line-height: 1.5; font-family: "Monaco","Consolas","Lucida Console","Courier New","serif"; font-size: 12px; background-color: rgb(247, 247, 247);'><a href="http://bbs.pcbeta.com/viewthread-1095891-1-1.html">http://bbs.pcbeta.com/viewthread-1095891-1-1.html</a><br></span></div><span style='color: rgb(68, 68, 68); line-height: 1.5; font-family: "Monaco","Consolas","Lucida Console","Courier New","serif"; font-size: 12px; background-color: rgb(247, 247, 247);'><div><span style='color: rgb(68, 68, 68); line-height: 1.5; font-family: "Monaco","Consolas","Lucida Console","Courier New","serif"; font-size: 12px; background-color: rgb(247, 247, 247);'><br></span></div>sudo perl -pi -e 's|\x75\x30\x89\xd8|\xeb\x30\x89\xd8|' /System/Library/Extensions/AppleRTC.kext/Contents/MacOS/AppleRTC</span>
""")
print(new_content)
class TestAPI(unittest.TestCase):
def test_get_dir_api(self):
print('----------------')
# print('验证使用 Cookies 是否能成功登录\m')
session = pull.YoudaoNoteSession()
cookies_dict = pull.covert_json_str_to_dict('cookies.json')
logging.info(cookies_dict)
session.cookies_login(cookies_dict['cookies'])
# root_id = session.get_root_id()
# print(root_id)
url = 'https://note.youdao.com/yws/api/personal/file?method=listPath&fileId=9d8a2385eeec77338211b4f04bbf844d&keyfrom=web&cstk=01PvSwwu'
data = {
'fileId': 'WEB8bcb99a589ab9660a10aa5f87ca61675',
'cstk': '01PvSwwu'
}
content = session.post(url, data).content.decode('utf-8')
logging.info(content)
def test_get_file_api(self):
print('----------------')
# print('验证使用 Cookies 是否能成功登录\m')
session = pull.YoudaoNoteSession()
cookies_dict = pull.covert_json_str_to_dict('cookies.json')
logging.info(cookies_dict)
session.cookies_login(cookies_dict['cookies'])
# root_id = session.get_root_id()
# print(root_id)
url = 'https://note.youdao.com/yws/api/personal/sync?method=download&keyfrom=web&cstk=01PvSwwu'
data = {
'fileId': 'WEB4aa8bf8074d61befea1dd20f5593f01c',
'version': -1,
'convert': 'true',
'editorType': 1,
'cstk': '01PvSwwu'
}
content = session.post(url, data).content.decode('utf-8')
logging.info(content)
def test_get_note_file_api(self):
print('----------------')
# print('验证使用 Cookies 是否能成功登录\m')
session = pull.YoudaoNoteSession()
cookies_dict = pull.covert_json_str_to_dict('cookies.json')
# logging.info(cookies_dict)
session.cookies_login(cookies_dict['cookies'])
# root_id = session.get_root_id()
# print(root_id)
url = 'https://note.youdao.com/yws/api/personal/resource?method=listThumbsInfo&keyfrom=web&cstk=ZzXatKpy'
data = {
'fileId': 'WEB8bcb99a589ab9660a10aa5f87ca61675',
'cstk': 'ZzXatKpy'
}
content = session.post(url, data)
logging.info(content)
def test_image_url(self):
image_url = '![%s](https://www.zhihu.com/equation?tex=+k+%3D+%5Cleft%5B%5Cfrac%7Blog%28-z_%7Bvs%7D+%2F+near%29%7D%7Blog%281+%2B+%5Cfrac%7B2tan%5Ctheta%7D%7BS_y%7D%29%7D+%5Cright%5D+)' % 'test'
print(image_url)
class TestErrorExit(unittest.TestCase):
""" 测试 config.json """
# def test_local_config_format_pull_py(self):
# """ """
# pull.main()
def f_test_local_config_format(self):
""" 用于测试本地 config.json 文件出错的情况 """
with self.assertRaises(SystemExit):
pull.main()
def test_config_format(self):
print('----------------')
print('验证 config.json 格式错误(花括号位置),是否结束执行并输出提示\n')
# 跟 config.json 的字符串相比,字符串首行换行将增加空格符,导致 eval 无法将 string 转换为 json
# '
# {
# "username": "***",
# "password": "***",
# "local_dir": "***",
# "ydnote_dir": "",
# "smms_secret_token": ""
# }'
config_str = """
{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
with self.assertRaises(SystemExit):
pull.main()
def test_config_format2(self):
print('----------------')
print('验证 config.json 格式错误(有注释),是否结束执行并输出提示\n')
config_str = """{
"username": "%s",
"password": "%s", // 添加注释
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
with self.assertRaises(SystemExit):
pull.main()
def test_config_key(self):
print('----------------')
print('测试 config 的 key 被修改时 \n')
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydaonote_dir": "",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
with self.assertRaises(SystemExit):
pull.main()
def test_config_username_password(self):
print('----------------')
print('验证账号密码为空时,是否结束执行并输出提示\n')
config_str = """{
"username": "",
"password": "",
"local_dir": "",
"ydnote_dir": "test",
"smms_secret_token": ""
}"""
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
with self.assertRaises(SystemExit):
pull.main()
def test_config_ydnote_dir_err(self):
print('----------------')
print('当 ydnote_dir 不存在时,是否结束并提示\n')
# self.cookie_login_and_get_all('', 'GitHub', '')
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "GitHub",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
set_right_cookies()
with self.assertRaises(SystemExit):
pull.main()
def test_config_local_dir_err(self):
print('----------------')
print('当 local_dir 错误时(为相对路径),是否结束并提示\n')
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "~/Documents/youdaonote",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
set_right_cookies()
with self.assertRaises(SystemExit):
pull.main()
def test_config_local_dir_err2(self):
print('----------------')
print('当 local_dir 错误时(不存在上层文件夹),是否结束并提示\n')
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "/Users/deppwang/Documents2/youdaonote-pull",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
set_right_cookies()
with self.assertRaises(SystemExit):
pull.main()
def test_get_all_youdao_url_change(self):
print('----------------')
print('有道云笔记修改接口,是否提示\n')
# 修改了将不变?
pull.YoudaoNoteSession.DIR_MES_URL = 'https://note.youdao.com/yws/api/personal2/file/%s?all=true&f=true&len=30&sort=1&isReverse=false&method=listPageByParentId&keyfrom=web&cstk=%s'
with open('config-right.json', 'rb') as f:
byte_config = f.read()
with open('config.json', 'wb') as f:
f.write(byte_config)
with self.assertRaises(SystemExit):
pull.main()
pull.YoudaoNoteSession.DIR_MES_URL = 'https://note.youdao.com/yws/api/personal/file/%s?all=true&f=true&len=30&sort=1&isReverse=false&method=listPageByParentId&keyfrom=web&cstk=%s'
def test_get_all_network_err(self):
print('----------------')
print('网络未连接,是否提示\n')
with open('config-right.json', 'rb') as f:
byte_config = f.read()
with open('config.json', 'wb') as f:
f.write(byte_config)
with self.assertRaises(SystemExit):
pull.main()
class TestDownLoad(unittest.TestCase):
def test_get_all_with_no_token(self):
"""
是否可以抛出错误,但继续执行?不能抛出错误,只能打印错误
"""
print('----------------')
print('验证 smms_secret_token 为 null 时,是否能完整下载成功\n')
remove_local_dir()
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir)
set_right_cookies()
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
self.assertIsNone(pull.main())
def test_get_test_with_no_token(self):
"""
是否可以抛出错误,但继续执行?不能抛出错误,只能打印错误
"""
print('----------------')
print('验证 smms_secret_token 为 null 时,是否能完整下载成功\n')
remove_local_dir()
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir)
set_right_cookies()
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
self.assertIsNone(pull.main())
def test_get_all_with_token(self):
print('----------------')
print('验证 smms_secret_token 不为 null 时,是否能完整下载\n')
remove_local_dir()
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": "%s"
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir, UserDefine.smms_secret_token)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
self.assertIsNone(pull.main())
def test_get_all_with_err_token(self):
print('----------------')
print('验证 smms_secret_token 错误时,是否能完整下载\n')
remove_local_dir()
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": "%s"
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir, UserDefine.smms_secret_token)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
self.assertIsNone(pull.main())
def test_get_all_with_chinese_dir(self):
print('----------------')
print('验证目录包含中文时,能否正常下载\n')
local_dir = UserDefine.local_zh_Hans_dir
if os.path.exists(local_dir):
shutil.rmtree(local_dir)
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, local_dir)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
self.assertIsNone(pull.main())
def test_get_all_update(self):
print('----------------')
print('验证更新\n')
config_str = """{
"username": "%s",
"password": "%s",
"local_dir": "%s",
"ydnote_dir": "test",
"smms_secret_token": ""
}""" % (UserDefine.username, UserDefine.password, UserDefine.local_dir)
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
self.assertIsNone(pull.main())
class LoginTest(unittest.TestCase):
def test_login_right(self):
"""
验证账号密码登录
"""
remove_local_dir()
set_right_config()
if os.path.exists('cookies.json'):
os.remove('cookies.json')
self.assertIsNone(pull.main())
def test_cookies_login(self):
print('----------------')
print('验证使用 Cookies 是否能成功登录\m')
remove_local_dir()
set_right_config()
set_right_cookies()
self.assertIsNone(pull.main())
def test_login_forbid(self):
"""
验证账号被封时,当运行测试失败时运行
需要是账号密码登录
ip 被封、没有 Cookie、账号密码密码正确。么有提示?
"""
remove_local_dir()
set_right_config()
if os.path.exists('cookies.json'):
os.remove('cookies.json')
with self.assertRaises(SystemExit):
pull.main()
def test_username_password_err(self):
""" 验证账号密码错误,无 Cookies,是否登录失败
需要账号密码登录,此测试容易封 ip,
将保存错误 Cookies 到 cookies.json
账号密码错误时的提示:{'canTryAgain': False, 'scope': 'SECURITY', 'error': '207', 'message': 'Message[AUTHENTICATION_FAILURE]: User token must be authenticated.', 'objectUser': None}
验证码错误时的提示:{'canTryAgain': False, 'scope': 'SECURITY', 'error': '207', 'message': 'Message[AUTHENTICATION_FAILURE]: User token must be authenticated.', 'objectUser': None}"
"""
config_str = """{
"username": "error_username",
"password": "error_password",
"local_dir": "/Users/deppwang/Documents/youdaonote-pull/test1",
"ydnote_dir": "test",
"smms_secret_token": ""
}"""
with open('config.json', 'wb') as f:
f.write(config_str.encode('utf-8'))
if os.path.exists('cookies.json'):
os.remove('cookies.json')
with self.assertRaises(SystemExit):
pull.main()
def test_cookies_error(self):
print('----------------')
print('验证人为修改,导致 cookies.json 格式错误,是否输出提示。不影响运行\n')
set_right_config()
conkies_str = """{
cookies: [[],[
}"""
with open('cookies.json', 'wb') as f:
f.write(conkies_str.encode('utf-8'))
with self.assertRaises(SystemExit):
pull.main()
def test_cookies_error2(self):
print('----------------')
print('验证人为修改,导致 cookies.json 格式错误,是否输出提示。\n')
set_right_config()
conkies_str = """{}"""
with open('cookies.json', 'wb') as f:
f.write(conkies_str.encode('utf-8'))
self.assertIsNone(pull.main())
set_right_cookies()
# config = TestErrorExit
def error_exit_suite():
suite = unittest.TestSuite()
# suite.addTest(TestErrorExit('test_local_config_format'))
suite.addTest(TestErrorExit('test_config_format'))
suite.addTest(TestErrorExit('test_config_format2'))
suite.addTest(TestErrorExit('test_config_key'))
suite.addTest(TestErrorExit('test_config_username_password'))
suite.addTest(TestErrorExit('test_config_ydnote_dir_err'))
suite.addTest(TestErrorExit('test_config_local_dir_err'))
suite.addTest(TestErrorExit('test_config_local_dir_err2'))
suite.addTest(TestErrorExit('test_get_all_youdao_url_change'))
return suite
def download_suite():
suite = unittest.TestSuite()
suite.addTest(TestDownLoad('test_get_all_with_no_token'))
suite.addTest(TestDownLoad('test_get_all_with_token'))
suite.addTest(TestDownLoad('test_get_all_with_err_token'))
suite.addTest(TestDownLoad('test_get_all_with_chinese_dir'))
suite.addTest(TestDownLoad('test_get_all_update'))
return suite
def login_suite():
suite = unittest.TestSuite()
suite.addTest(LoginTest('test_login_right'))
return suite
if __name__ == '__main__':
# 如何模拟 Windows 环境与 Unix 环境运行脚本?在本地是没法做到的
# 如何检查 logging 级别?测试级别改为 Debug
runner = unittest.TextTestRunner()
error = error_exit_suite()
download = download_suite()
login = login_suite()
logging.info(len(sys.argv))
if len(sys.argv) == 1:
print('--------------------------------')
print('运行错误退出测试\n')
runner.run(error)
print('--------------------------------')
print('运行下载测试\n')
runner.run(download)
# print('--------------------------------')
# print('测试是否能正常登录\n')
# runner.run(login)
sys.exit(1)
suite = eval(sys.argv[1])
# 因为容易封 ip,单独测试 login
if suite is login:
with open('cookies.json', 'rb') as f:
cookies = f.read().decode('utf-8')
# print(cookies)
runner.run(suite)
with open('cookies.json', 'wb') as f:
f.write(cookies.encode('utf-8'))
sys.exit(1)
runner.run(suite)