102 lines
3.5 KiB
Python
102 lines
3.5 KiB
Python
# 登录缓存权限和商城账号来源。
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
import stat
|
||
|
||
import requests
|
||
|
||
from auth_login import load_cookie_file, load_secrets, save_cookie_file, save_yaoex_auth
|
||
from http_util import cookies_for_http, set_session_cookies
|
||
from search_yaoex import YaoexAuth
|
||
|
||
|
||
# 写出的 1 号药城缓存权限为 0o600(Windows 只断言文件存在)。
|
||
def test_save_yaoex_auth_sets_mode_600(tmp_path):
|
||
path = tmp_path / "yaoex.json"
|
||
save_yaoex_auth(YaoexAuth(token="t", user_id="u", buyer_code="b"), path)
|
||
assert path.is_file()
|
||
if os.name == "nt":
|
||
return
|
||
assert path.stat().st_mode & 0o777 == 0o600
|
||
|
||
|
||
# 写出的药帮忙 cookie 权限为 0o600(Windows 只断言文件存在)。
|
||
def test_save_cookie_file_sets_mode_600(tmp_path):
|
||
path = tmp_path / "ybm.json"
|
||
save_cookie_file({"sid": "1"}, path)
|
||
assert path.is_file()
|
||
if os.name == "nt":
|
||
return
|
||
assert stat.S_IMODE(path.stat().st_mode) == 0o600
|
||
|
||
|
||
# 旁路 secrets.json 不读。没有环境变量时账号为空。
|
||
def test_load_secrets_ignores_secrets_json(tmp_path, monkeypatch):
|
||
monkeypatch.setattr("auth_login.PROJECT_ROOT", tmp_path)
|
||
(tmp_path / "secrets.json").write_text(
|
||
json.dumps({"yaoex": {"username": "from-file", "password": "p"}}),
|
||
encoding="utf-8",
|
||
)
|
||
monkeypatch.delenv("YAOEX_USERNAME", raising=False)
|
||
monkeypatch.delenv("YAOEX_PASSWORD", raising=False)
|
||
monkeypatch.delenv("YBM_USERNAME", raising=False)
|
||
monkeypatch.delenv("YBM_PASSWORD", raising=False)
|
||
secrets = load_secrets()
|
||
assert secrets["yaoex"]["username"] == ""
|
||
assert secrets["yaoex"]["password"] == ""
|
||
|
||
|
||
# 写进 requests 会话时中文 cookie 不触发 latin-1 编码错误。
|
||
def test_set_session_cookies_skips_unicode_values():
|
||
session = requests.Session()
|
||
set_session_cookies(
|
||
session,
|
||
{"SESSION": "abc", "ycenterpriseName": "洁达药业"},
|
||
)
|
||
assert session.cookies.get("SESSION") == "abc"
|
||
|
||
|
||
# 中文企业名等展示字段丢掉,会话 id 留下。换账号打开浏览器同样适用。
|
||
def test_cookies_for_http_keeps_ascii_and_drops_unicode():
|
||
got = cookies_for_http(
|
||
{
|
||
"SESSION": "abc",
|
||
"ycenterpriseName": "洁达药业",
|
||
"siteName": "总部",
|
||
"ycstationName": "广州",
|
||
}
|
||
)
|
||
assert got == {"SESSION": "abc"}
|
||
|
||
|
||
# 写出缓存时不把中文 cookie 写进文件,读回来也能进 HTTP 头。
|
||
def test_save_cookie_file_drops_unicode_values(tmp_path):
|
||
path = tmp_path / "ybm.json"
|
||
save_cookie_file(
|
||
{"sid": "1", "ycenterpriseName": "洁达药业"},
|
||
path,
|
||
)
|
||
loaded = load_cookie_file(path)
|
||
assert loaded == {"sid": "1"}
|
||
|
||
|
||
# 账号只认环境变量。旁边的 json 即使写了别的用户名也不采用。
|
||
def test_load_secrets_reads_env_not_json(tmp_path, monkeypatch):
|
||
monkeypatch.setattr("auth_login.PROJECT_ROOT", tmp_path)
|
||
(tmp_path / "secrets.json").write_text(
|
||
json.dumps({"yaoex": {"username": "from-file", "password": "p"}}),
|
||
encoding="utf-8",
|
||
)
|
||
monkeypatch.setenv("YAOEX_USERNAME", "from-env")
|
||
monkeypatch.setenv("YAOEX_PASSWORD", "env-pass")
|
||
monkeypatch.delenv("YBM_USERNAME", raising=False)
|
||
monkeypatch.delenv("YBM_PASSWORD", raising=False)
|
||
secrets = load_secrets()
|
||
assert secrets["yaoex"]["username"] == "from-env"
|
||
assert secrets["yaoex"]["password"] == "env-pass"
|
||
assert secrets["ybm"]["username"] == ""
|
||
assert secrets["ybm"]["password"] == ""
|