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": "******", "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_config_format(self): print('----------------') print('验证 config.json 格式错误(花括号位置),是否结束执行并输出提示\n') # 跟 config.json 的字符串相比,字符串首行换行将增加空格符,导致 eval 无法将 string 转换为 json # ' # { # "username": "******", # "password": "******", # "local_dir": "/Users/deppwang/Documents/youdaonote-pull/deppwang2", # "ydnote_dir": "", # "smms_secret_token": "" # }' config_str = """ { "username": "******", "password": "******", "local_dir": "/Users/deppwang/Documents/youdaonote-pull/deppwang2", "ydnote_dir": "test", "smms_secret_token": "" }""" % (Passport.username, Passport.password) with open('config.json', 'wb') as f: f.write(config_str.encode('utf-8')) with self.assertRaises(SystemExit): pull.main()
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()
def refresh(): file_path = os.path.join(sys.path[0], f'.cache-{auth.USERNAME}') if os.path.exists(file_path): cache = json.load(open(file_path, 'r')) if True or time.time() > cache['expires_at']: logging.info( 'Refreshing Spotify data by pulling tracks, this may take a moment.' ) pull.main() else: logging.info( 'Spotify data deemed to be recent enough (under {} seconds old)' .format(cache['expires_in'])) else: 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_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_get_test_with_no_token(self): """ 是否可以抛出错误,但继续执行?不能抛出错误,只能打印错误 """ print('----------------') print('验证 smms_secret_token 为 null 时,是否能完整下载成功\n') local_dir = "/Users/deppwang/Documents/youdaonote-pull/test1" if os.path.exists(local_dir): shutil.rmtree(local_dir) config_str = """{ "username": "******", "password": "******", "local_dir": "%s", "ydnote_dir": "test", "smms_secret_token": "" }""" % (Passport.username, Passport.password, 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_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_config_format2(self): print('----------------') print('验证 config.json 格式错误(有注释),是否结束执行并输出提示\n') config_str = """{ "username": "******", "password": "******", // 添加注释 "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": "******", "password": "******", "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_key(self): print('----------------') print('测试 config 的 key 被修改时 \n') config_str = """{ "username": "******", "password": "******", "local_dir": "/Users/deppwang/Documents/youdaonote-pull/deppwang2", "ydaonote_dir": "", "smms_secret_token": "" }""" % (Passport.username, Passport.password) 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": "******", "password": "******", // 添加注释 "local_dir": "/Users/deppwang/Documents/youdaonote-pull/deppwang2", "ydnote_dir": "test", "smms_secret_token": "" }""" % (Passport.username, Passport.password) with open('config.json', 'wb') as f: f.write(config_str.encode('utf-8')) with self.assertRaises(SystemExit): pull.main()
def test_config_local_dir_err(self): print('----------------') print('当 local_dir 错误时(为相对路径),是否结束并提示\n') config_str = """{ "username": "******", "password": "******", "local_dir": "~/Dropbox/youdaonote", "ydnote_dir": "", "smms_secret_token": "" }""" % (Passport.username, Passport.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": "******", "password": "******", "local_dir": "/Users/deppwang/Documents/youdaonote-pull", "ydnote_dir": "test", "smms_secret_token": "" }""" % (Passport.username, Passport.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_cookies_login(self): print('----------------') print('验证使用 Cookies 是否能成功登录\m') remove_local_dir() set_right_config() set_right_cookies() self.assertIsNone(pull.main())
def test_config_ydnote_dir_err(self): print('----------------') print('当 ydnote_dir 不存在时,是否结束并提示\n') # self.cookie_login_and_get_all('', 'GitHub', '') config_str = """{ "username": "******", "password": "******", "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_ydnote_dir_err(self): print('----------------') print('当 ydnote_dir 不存在时,是否结束并提示\n') # self.cookie_login_and_get_all('', 'GitHub', '') config_str = """{ "username": "******", "password": "******", "local_dir": "/Users/deppwang/Documents/youdaonote-pull/deppwang2", "ydnote_dir": "GitHub", "smms_secret_token": "" }""" % (Passport.username, Passport.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_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_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()
def test_get_all_update(self): print('----------------') print('验证更新\n') config_str = """{ "username": "******", "password": "******", "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())
def test_get_all_update(self): print('----------------') print('验证更新\n') local_dir = "/Users/deppwang/Documents/youdaonote-pull/test1" config_str = """{ "username": "******", "password": "******", "local_dir": "%s", "ydnote_dir": "test", "smms_secret_token": "" }""" % (Passport.username, Passport.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_with_err_token(self): print('----------------') print('验证 smms_secret_token 错误时,是否能完整下载\n') remove_local_dir() config_str = """{ "username": "******", "password": "******", "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": "******", "password": "******", "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_with_chinese_dir(self): print('----------------') print('验证目录包含中文时,能否正常下载\n') local_dir = "/Users/deppwang/Documents/youdaonote-pull/测试" if os.path.exists(local_dir): shutil.rmtree(local_dir) config_str = """{ "username": "******", "password": "******", "local_dir": "%s", ∞: "test", "smms_secret_token": "" }""" % (Passport.username, Passport.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_with_err_token(self): print('----------------') print('验证 smms_secret_token 错误时,是否能完整下载\n') local_dir = "/Users/deppwang/Documents/youdaonote-pull/test1" if os.path.exists(local_dir): shutil.rmtree(local_dir) config_str = """{ "username": "******", "password": "******", "local_dir": "%s", "ydnote_dir": "test", "smms_secret_token": "SGSLk9yWcTe4RenXYqEPMkqVrx0Y8error" }""" % (Passport.username, Passport.password, local_dir) 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": "******", "password": "******", "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 f_test_local_config_format(self): """ 用于测试本地 config.json 文件出错的情况 """ with self.assertRaises(SystemExit): pull.main()