@mrrisega/dsh-remote 0.7.12-beta.1 → 0.7.12-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/clients/dsh-remote/dingtalk-bot.mjs +1361 -0
- package/clients/dsh-remote/docs/feishu-bot-contract.md +217 -0
- package/clients/dsh-remote/dsh-bridge.mjs +404 -90
- package/clients/dsh-remote/feishu-bot.mjs +1852 -0
- package/clients/dsh-remote/mirror-diag-script.js +53 -14
- package/clients/dsh-remote/mobile-adapter.mjs +355 -40
- package/clients/dsh-remote/test/bridge-http-abort.test.mjs +143 -0
- package/clients/dsh-remote/test/bridge-slow-response.test.mjs +107 -0
- package/clients/dsh-remote/test/bridge-tunnel-receiver.test.mjs +135 -0
- package/clients/dsh-remote/test/dingtalk-bot.test.mjs +630 -0
- package/clients/dsh-remote/test/feishu-bot.test.mjs +1089 -0
- package/clients/dsh-remote/test/mirror-diag.test.mjs +47 -2
- package/clients/dsh-remote/test/mobile-adapter-load-watch.test.mjs +208 -9
- package/dsh-setup.mjs +1 -1
- package/package.json +2 -2
- package/packages/dsh-remote-web/lib/client.js +500 -4
- package/packages/dsh-remote-web/lib/index.js +581 -27
- package/packages/dsh-remote-web/package.json +1 -1
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/dingtalk-bot.mjs +1318 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/docs/feishu-bot-contract.md +177 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/dsh-bridge.mjs +404 -84
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/feishu-bot.mjs +1668 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/im-format.mjs +372 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/mirror-diag-script.js +53 -14
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/mobile-adapter.mjs +355 -40
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/upstream-discovery.mjs +495 -43
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/wecom-bot.mjs +238 -12
- package/packages/dsh-remote-web/runtime/dsh-setup.mjs +78 -7
- package/packages/dsh-remote-web/test/announcements-proxy.test.mjs +14 -1
- package/packages/dsh-remote-web/test/im-bots.test.mjs +376 -2
- package/packages/dsh-remote-web/test/panel-ia.test.mjs +397 -103
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// 暴露给任何能打开本机 dsh web 页面的人(哪怕它只是一台共享电脑)。
|
|
13
13
|
import assert from "node:assert/strict";
|
|
14
14
|
import http from "node:http";
|
|
15
|
-
import { mkdtemp, readFile, stat, writeFile } from "node:fs/promises";
|
|
15
|
+
import { mkdir, mkdtemp, readFile, stat, writeFile } from "node:fs/promises";
|
|
16
16
|
import os from "node:os";
|
|
17
17
|
import path from "node:path";
|
|
18
18
|
import test from "node:test";
|
|
@@ -199,7 +199,7 @@ test("POST /test:桥写的状态显示已连接 → {ok:true, connected:true,
|
|
|
199
199
|
await writeStatus(relayDir, { enabled: true, connected: true, authenticated: true, lastError: "" });
|
|
200
200
|
const r = await postTest(base); // 不传 body = 测已保存的配置
|
|
201
201
|
assert.equal(r.status, 200);
|
|
202
|
-
assert.deepEqual(r.body, { ok: true, connected: true, authenticated: true, lastError: "" });
|
|
202
|
+
assert.deepEqual(r.body, { ok: true, platform: "wecom", connected: true, authenticated: true, lastError: "" });
|
|
203
203
|
});
|
|
204
204
|
});
|
|
205
205
|
|
|
@@ -398,3 +398,377 @@ test("GET:tier 只认三个枚举值;remainingMessages=null 表示不限/未
|
|
|
398
398
|
assert.equal(st.tier, "pro_max");
|
|
399
399
|
});
|
|
400
400
|
});
|
|
401
|
+
|
|
402
|
+
// ─────────────────── 钉钉 / 飞书通道(2026-11-08) ───────────────────
|
|
403
|
+
|
|
404
|
+
const putPlatform = (base, platform, config) => json(`${base}/dsh-remote/im-bots`, {
|
|
405
|
+
method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ platform, config }),
|
|
406
|
+
});
|
|
407
|
+
const postPlatformTest = (base, platform, config) => json(`${base}/dsh-remote/im-bots/test`, {
|
|
408
|
+
method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(config ? { platform, config } : { platform }),
|
|
409
|
+
});
|
|
410
|
+
const getDeps = async (base) => (await json(`${base}/dsh-remote/im-deps`)).body;
|
|
411
|
+
const post = (base, url) => json(url, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
|
|
412
|
+
|
|
413
|
+
test("GET:dingtalk / feishu 两段都只回脱敏视图(有 hasSecret,绝无 clientSecret/appSecret 值)", async () => {
|
|
414
|
+
const relayDir = await tmpRelay();
|
|
415
|
+
await withHost(relayDir, {}, async (base) => {
|
|
416
|
+
await putPlatform(base, "dingtalk", { clientId: "ding-id", clientSecret: "DING-SECRET", robotCode: "ding-robot" });
|
|
417
|
+
await putPlatform(base, "feishu", { appId: "cli_1", appSecret: "FS-SECRET", receiveId: "ou_1", receiveIdType: "open_id" });
|
|
418
|
+
const r = await json(`${base}/dsh-remote/im-bots`);
|
|
419
|
+
assert.equal(r.body.dingtalk.clientId, "ding-id");
|
|
420
|
+
assert.equal(r.body.dingtalk.robotCode, "ding-robot");
|
|
421
|
+
assert.equal(r.body.dingtalk.hasSecret, true);
|
|
422
|
+
assert.equal(r.body.feishu.appId, "cli_1");
|
|
423
|
+
assert.equal(r.body.feishu.receiveIdType, "open_id");
|
|
424
|
+
assert.equal(r.body.feishu.hasSecret, true);
|
|
425
|
+
const raw = JSON.stringify(r.body);
|
|
426
|
+
assert.ok(!raw.includes("DING-SECRET"), "★ 钉钉 secret 绝不能出现");
|
|
427
|
+
assert.ok(!raw.includes("FS-SECRET"), "★ 飞书 secret 绝不能出现");
|
|
428
|
+
assert.ok(!raw.includes("clientSecret") && !raw.includes("appSecret"), "连字段名都不出现");
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
test("PUT:保存钉钉配置(0600 原子写 + 保留未知字段 + 空 secret 不覆盖);缺字段给人话错误", async () => {
|
|
433
|
+
const relayDir = await tmpRelay();
|
|
434
|
+
await withHost(relayDir, {}, async (base) => {
|
|
435
|
+
// 先写一份带未知字段的旧文件:插件不认识,但不能删掉
|
|
436
|
+
await writeFile(path.join(relayDir, "im-bots.json"), JSON.stringify({ version: 1, future: { keep: 1 } }));
|
|
437
|
+
let r = await putPlatform(base, "dingtalk", { clientId: "id1", clientSecret: "sec1", robotCode: "rc1" });
|
|
438
|
+
assert.equal(r.status, 200);
|
|
439
|
+
let cfg = JSON.parse(await readFile(path.join(relayDir, "im-bots.json"), "utf8"));
|
|
440
|
+
assert.equal(cfg.dingtalk.clientId, "id1");
|
|
441
|
+
assert.equal(cfg.dingtalk.clientSecret, "sec1");
|
|
442
|
+
assert.equal(cfg.dingtalk.robotCode, "rc1");
|
|
443
|
+
assert.equal(cfg.dingtalk.enabled, true, "首次保存凭据 = 想让它生效");
|
|
444
|
+
assert.deepEqual(cfg.future, { keep: 1 }, "未知字段必须保留");
|
|
445
|
+
const mode = (await stat(path.join(relayDir, "im-bots.json"))).mode & 0o777;
|
|
446
|
+
assert.equal(mode, 0o600, "配置文件必须 0600");
|
|
447
|
+
|
|
448
|
+
// 空 secret = 不修改
|
|
449
|
+
r = await putPlatform(base, "dingtalk", { clientId: "id2", clientSecret: "", robotCode: "rc2" });
|
|
450
|
+
assert.equal(r.status, 200);
|
|
451
|
+
cfg = JSON.parse(await readFile(path.join(relayDir, "im-bots.json"), "utf8"));
|
|
452
|
+
assert.equal(cfg.dingtalk.clientId, "id2");
|
|
453
|
+
assert.equal(cfg.dingtalk.clientSecret, "sec1", "空 secret 表示不修改");
|
|
454
|
+
assert.equal(cfg.dingtalk.robotCode, "rc2");
|
|
455
|
+
|
|
456
|
+
// 缺字段
|
|
457
|
+
r = await putPlatform(base, "dingtalk", { clientId: "id3", clientSecret: "sec3", robotCode: "" });
|
|
458
|
+
assert.equal(r.status, 400);
|
|
459
|
+
assert.equal(r.body.error, "missing_robot_code", "三个字段都要:缺 Robot Code 要报出来");
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
test("PUT:保存飞书配置(字段锁死 + receiveIdType 校验 + 空 secret 不覆盖)", async () => {
|
|
464
|
+
const relayDir = await tmpRelay();
|
|
465
|
+
await withHost(relayDir, {}, async (base) => {
|
|
466
|
+
// ★ 2026-11-08 业主:接收人不需要用户填(桥侧自动发现)→ 不传 receiveId 也必须能保存
|
|
467
|
+
let r = await putPlatform(base, "feishu", { appId: "cli_a", appSecret: "fs_a" });
|
|
468
|
+
assert.equal(r.status, 200, "只有 App ID + Secret 就必须能保存");
|
|
469
|
+
let cfg = JSON.parse(await readFile(path.join(relayDir, "im-bots.json"), "utf8"));
|
|
470
|
+
assert.equal(cfg.feishu.appId, "cli_a");
|
|
471
|
+
assert.equal(cfg.feishu.receiveId, "", "接收人留空 = 交给桥自动发现");
|
|
472
|
+
assert.equal(cfg.feishu.receiveIdType, "open_id", "默认 open_id(桥可能在自动发现后回写)");
|
|
473
|
+
r = await putPlatform(base, "feishu", { receiveIdType: "chat_id" });
|
|
474
|
+
assert.equal(r.status, 200);
|
|
475
|
+
assert.equal(cfg.feishu.domain, "https://open.feishu.cn", "domain 默认官方");
|
|
476
|
+
r = await putPlatform(base, "feishu", { appId: "cli_b", appSecret: "", receiveId: "ou_b" });
|
|
477
|
+
cfg = JSON.parse(await readFile(path.join(relayDir, "im-bots.json"), "utf8"));
|
|
478
|
+
assert.equal(cfg.feishu.appSecret, "fs_a", "空 secret 不覆盖");
|
|
479
|
+
assert.equal(cfg.feishu.receiveIdType, "chat_id", "显式传 receiveIdType 时可改(高级/兼容用)");
|
|
480
|
+
// 非法 receiveIdType
|
|
481
|
+
r = await putPlatform(base, "feishu", { appId: "x", appSecret: "y", receiveId: "z", receiveIdType: "userid" });
|
|
482
|
+
assert.equal(r.status, 400);
|
|
483
|
+
assert.equal(r.body.error, "invalid_receive_id_type");
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
test("POST /test:platform=dingtalk / feishu 各自轮询对应状态(不再混用 wecom 状态)", async () => {
|
|
488
|
+
const relayDir = await tmpRelay();
|
|
489
|
+
await withHost(relayDir, {}, async (base) => {
|
|
490
|
+
await putPlatform(base, "dingtalk", { clientId: "i", clientSecret: "s", robotCode: "r" });
|
|
491
|
+
await putPlatform(base, "feishu", { appId: "cli_i", appSecret: "fs_s", receiveId: "ou_i" });
|
|
492
|
+
await writeStatus(relayDir, { connected: false, authenticated: false, lastError: "" }, {
|
|
493
|
+
dingtalk: { enabled: true, connected: true, authenticated: true, lastError: "" },
|
|
494
|
+
feishu: { enabled: true, connected: true, authenticated: true, lastError: "" },
|
|
495
|
+
});
|
|
496
|
+
let r = await postPlatformTest(base, "dingtalk");
|
|
497
|
+
assert.equal(r.status, 200);
|
|
498
|
+
assert.equal(r.body.platform, "dingtalk");
|
|
499
|
+
assert.equal(r.body.connected, true);
|
|
500
|
+
r = await postPlatformTest(base, "feishu");
|
|
501
|
+
assert.equal(r.body.platform, "feishu");
|
|
502
|
+
assert.equal(r.body.connected, true);
|
|
503
|
+
// 未保存 secret 的平台:给明确错误,而不是"连不上"
|
|
504
|
+
await withHost(await tmpRelay(), {}, async (base2) => {
|
|
505
|
+
const rr = await postPlatformTest(base2, "dingtalk");
|
|
506
|
+
assert.equal(rr.status, 400);
|
|
507
|
+
assert.equal(rr.body.error, "missing_secret");
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
test("GET 状态白名单:status.dingtalk / status.feishu 按契约透出(含 tier/caps/usage/state),状态文件敏感字段不外泄", async () => {
|
|
513
|
+
const relayDir = await tmpRelay();
|
|
514
|
+
await withHost(relayDir, {}, async (base) => {
|
|
515
|
+
await writeStatus(relayDir, { connected: false }, {
|
|
516
|
+
dingtalk: {
|
|
517
|
+
enabled: true, state: "connected", connected: true, authenticated: true, lastError: "", boundChatId: "chat-9",
|
|
518
|
+
tier: "pro", caps: ["dingtalk"], capsCount: 1, bindingGated: false, messagesPerMonth: 0, remainingMessages: null,
|
|
519
|
+
usage: { month: "2026-11", messages: 5 }, clientSecret: "LEAK-D", token: "LEAK-T",
|
|
520
|
+
credentialSource: "scan", robotCodeConfigured: true, capabilities: { openapi: true, card: false }, internal: "LEAK-C",
|
|
521
|
+
},
|
|
522
|
+
feishu: {
|
|
523
|
+
enabled: true, state: "failed", connected: false, authenticated: false, lastError: "鉴权失败", boundChatId: "",
|
|
524
|
+
tier: "free", caps: [], capsCount: 0, bindingGated: true, messagesPerMonth: 20, remainingMessages: 19,
|
|
525
|
+
usage: { month: "2026-11", messages: 1 }, boundChatType: "p2p", receiveIdSource: "auto", appSecret: "LEAK-F",
|
|
526
|
+
},
|
|
527
|
+
});
|
|
528
|
+
const r = await json(`${base}/dsh-remote/im-bots`);
|
|
529
|
+
const d = r.body.status.dingtalk;
|
|
530
|
+
assert.equal(d.state, "connected");
|
|
531
|
+
assert.equal(d.tier, "pro");
|
|
532
|
+
assert.deepEqual(d.caps, ["dingtalk"]);
|
|
533
|
+
assert.equal(d.remainingMessages, null, "null 必须保持 null(≠0)");
|
|
534
|
+
assert.deepEqual(d.usage, { month: "2026-11", messages: 5 });
|
|
535
|
+
assert.equal(d.credentialSource, "scan", "凭据来源要透出(扫码/手动)");
|
|
536
|
+
assert.equal(d.robotCodeConfigured, true, "Robot Code 是否配置要透出(面板据此置灰能力)");
|
|
537
|
+
assert.deepEqual(d.capabilities, { openapi: true, card: false }, "能力开关只透出 openapi/card 两个布尔");
|
|
538
|
+
const f = r.body.status.feishu;
|
|
539
|
+
assert.equal(f.state, "failed");
|
|
540
|
+
assert.equal(f.tier, "free");
|
|
541
|
+
assert.equal(f.bindingGated, true);
|
|
542
|
+
assert.equal(f.remainingMessages, 19);
|
|
543
|
+
assert.equal(f.boundChatType, "p2p", "飞书自动绑定类型要透出(面板显示「已自动绑定:私聊」)");
|
|
544
|
+
assert.equal(f.receiveIdSource, "auto", "自动发现来源要透出");
|
|
545
|
+
assert.ok(!JSON.stringify(r.body).includes("LEAK"), "★ 桥写状态里的敏感字段必须被白名单挡掉");
|
|
546
|
+
// wecom 老字段仍在(向后兼容)
|
|
547
|
+
assert.equal(typeof r.body.status.connected, "boolean");
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
// 假子进程:测试注入 opts.imDepsSpawn,绝不真的联网 npm install
|
|
552
|
+
function fakeSpawnChild(installed, stderrText) {
|
|
553
|
+
const handlers = {};
|
|
554
|
+
return {
|
|
555
|
+
stdout: { on() {} },
|
|
556
|
+
stderr: { on(_e, cb) { handlers.data = cb; } },
|
|
557
|
+
on(ev, cb) { handlers[ev] = cb; },
|
|
558
|
+
_finish() {
|
|
559
|
+
if (installed) handlers.installed = true;
|
|
560
|
+
setTimeout(() => {
|
|
561
|
+
if (stderrText && handlers.data) handlers.data(Buffer.from(stderrText));
|
|
562
|
+
if (handlers.exit) handlers.exit(installed ? 0 : 1);
|
|
563
|
+
}, 20);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
test("GET /im-deps + POST /im-deps/feishu:后台安装(注入假 spawn,不联网),phase 从 downloading→done", async () => {
|
|
569
|
+
const relayDir = await tmpRelay();
|
|
570
|
+
const pkgDir = path.join(relayDir, "im-deps", "node_modules", "@larksuiteoapi", "node-sdk");
|
|
571
|
+
const spawnCalls = [];
|
|
572
|
+
const imDepsSpawn = (cmd, args) => {
|
|
573
|
+
spawnCalls.push({ cmd, args });
|
|
574
|
+
const child = fakeSpawnChild(true, "");
|
|
575
|
+
// 模拟 npm 装完后落盘(测试里直接写好 package.json)
|
|
576
|
+
setTimeout(async () => {
|
|
577
|
+
await mkdir(pkgDir, { recursive: true });
|
|
578
|
+
await writeFile(path.join(pkgDir, "package.json"), JSON.stringify({ name: "@larksuiteoapi/node-sdk", version: "1.40.0" }));
|
|
579
|
+
child._finish();
|
|
580
|
+
}, 10);
|
|
581
|
+
return child;
|
|
582
|
+
};
|
|
583
|
+
await withHost(relayDir, { imDepsSpawn }, async (base) => {
|
|
584
|
+
let deps = await getDeps(base);
|
|
585
|
+
assert.equal(deps.feishu.installed, false);
|
|
586
|
+
assert.equal(deps.feishu.phase, "idle");
|
|
587
|
+
|
|
588
|
+
const r = await post(base, `${base}/dsh-remote/im-deps/feishu`);
|
|
589
|
+
assert.equal(r.status, 200);
|
|
590
|
+
assert.equal(r.body.ok, true);
|
|
591
|
+
assert.equal(r.body.started, true);
|
|
592
|
+
assert.ok(spawnCalls.length === 1, "应触发一次 npm install");
|
|
593
|
+
assert.ok(spawnCalls[0].args.join(" ").includes("@larksuiteoapi/node-sdk@1.40.0"), "安装命令必须钉死 SDK 版本");
|
|
594
|
+
|
|
595
|
+
// 轮询 phase 直到 done
|
|
596
|
+
let phase = "";
|
|
597
|
+
for (let i = 0; i < 50; i++) {
|
|
598
|
+
deps = await getDeps(base);
|
|
599
|
+
phase = deps.feishu.phase;
|
|
600
|
+
if (phase === "done" || phase === "failed") break;
|
|
601
|
+
await new Promise((r2) => setTimeout(r2, 30));
|
|
602
|
+
}
|
|
603
|
+
deps = await getDeps(base);
|
|
604
|
+
assert.equal(deps.feishu.phase, "done", "安装完成后 phase=done,实际 " + deps.feishu.phase);
|
|
605
|
+
assert.equal(deps.feishu.installed, true);
|
|
606
|
+
assert.equal(deps.feishu.version, "1.40.0");
|
|
607
|
+
assert.ok(deps.feishu.path.includes("node-sdk"));
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
test("POST /im-deps/feishu:安装失败 → phase=failed + 人话原因(network/disk/permission/sdk_install_failed)", async () => {
|
|
612
|
+
const relayDir = await tmpRelay();
|
|
613
|
+
const imDepsSpawn = () => {
|
|
614
|
+
const child = fakeSpawnChild(false, "npm ERR! code ETIMEDOUT network registry.npmjs.org");
|
|
615
|
+
setTimeout(() => child._finish(), 10);
|
|
616
|
+
return child;
|
|
617
|
+
};
|
|
618
|
+
await withHost(relayDir, { imDepsSpawn }, async (base) => {
|
|
619
|
+
const r = await post(base, `${base}/dsh-remote/im-deps/feishu`);
|
|
620
|
+
assert.equal(r.status, 200);
|
|
621
|
+
let deps = null;
|
|
622
|
+
for (let i = 0; i < 60; i++) {
|
|
623
|
+
deps = await getDeps(base);
|
|
624
|
+
if (deps.feishu.phase === "failed") break;
|
|
625
|
+
await new Promise((r2) => setTimeout(r2, 30));
|
|
626
|
+
}
|
|
627
|
+
assert.equal(deps.feishu.phase, "failed", "失败必须落到 failed 而不是永远 downloading");
|
|
628
|
+
assert.match(deps.feishu.lastError, /网络不通/, "网络类错误要翻成人话");
|
|
629
|
+
// 已失败后重试:再次触发安装
|
|
630
|
+
const r2 = await post(base, `${base}/dsh-remote/im-deps/feishu`);
|
|
631
|
+
assert.equal(r2.body.started, true, "失败后允许重试");
|
|
632
|
+
});
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
// ─────────────────── 钉钉「扫码一键创建机器人」device-flow(2026-11-08) ───────────────────
|
|
636
|
+
|
|
637
|
+
/** 假钉钉接口(注入 imAuthFetch;测试绝不真的打 oapi.dingtalk.com)。 */
|
|
638
|
+
function makeAuthFetch(plan = {}) {
|
|
639
|
+
const calls = [];
|
|
640
|
+
const resp = (body, ok = true, status = 200) => ({ ok, status, json: async () => body });
|
|
641
|
+
const fn = async (url, opts) => {
|
|
642
|
+
const body = opts && opts.body ? JSON.parse(opts.body) : {};
|
|
643
|
+
calls.push({ url: String(url), body });
|
|
644
|
+
const u = String(url);
|
|
645
|
+
if (u.endsWith("/init")) {
|
|
646
|
+
if (plan.initThrows) throw new Error("ECONNRESET");
|
|
647
|
+
return resp(plan.init || { nonce: "nonce-1" });
|
|
648
|
+
}
|
|
649
|
+
if (u.endsWith("/begin")) {
|
|
650
|
+
if (plan.beginRejected) return resp({ errmsg: "rejected" }, false, 400);
|
|
651
|
+
return resp(plan.begin || { device_code: "dc-secret", verification_uri_complete: "https://oapi.dingtalk.com/auth?x=1", interval: 2, expires_in: 120 });
|
|
652
|
+
}
|
|
653
|
+
if (u.endsWith("/poll")) {
|
|
654
|
+
if (plan.pollThrows) throw new Error("ETIMEDOUT");
|
|
655
|
+
return resp(plan.polls && plan.polls.length ? plan.polls.shift() : { status: "WAITING" });
|
|
656
|
+
}
|
|
657
|
+
throw new Error("unexpected url " + u);
|
|
658
|
+
};
|
|
659
|
+
fn.calls = calls;
|
|
660
|
+
return fn;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
test("POST /im-dingtalk-auth/begin:返回 authId/url/interval/expiresIn;device_code 绝不落浏览器", async () => {
|
|
664
|
+
const relayDir = await tmpRelay();
|
|
665
|
+
const imAuthFetch = makeAuthFetch();
|
|
666
|
+
await withHost(relayDir, { imAuthFetch }, async (base) => {
|
|
667
|
+
const r = await post(base, `${base}/dsh-remote/im-dingtalk-auth/begin`);
|
|
668
|
+
assert.equal(r.status, 200);
|
|
669
|
+
assert.equal(r.body.ok, true);
|
|
670
|
+
assert.ok(r.body.authId, "必须给浏览器一个 authId 句柄");
|
|
671
|
+
assert.equal(r.body.url, "https://oapi.dingtalk.com/auth?x=1");
|
|
672
|
+
assert.equal(r.body.interval, 2);
|
|
673
|
+
assert.equal(r.body.expiresIn, 120);
|
|
674
|
+
const raw = JSON.stringify(r.body);
|
|
675
|
+
assert.ok(!raw.includes("dc-secret"), "★ device_code 不能下发给浏览器");
|
|
676
|
+
});
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
test("POST begin 限流:同一时间只允许一个授权,旧 authId 查询回 cancelled", async () => {
|
|
680
|
+
const relayDir = await tmpRelay();
|
|
681
|
+
const imAuthFetch = makeAuthFetch();
|
|
682
|
+
await withHost(relayDir, { imAuthFetch }, async (base) => {
|
|
683
|
+
const a = await post(base, `${base}/dsh-remote/im-dingtalk-auth/begin`);
|
|
684
|
+
const b = await post(base, `${base}/dsh-remote/im-dingtalk-auth/begin`);
|
|
685
|
+
assert.ok(a.body.authId && b.body.authId && a.body.authId !== b.body.authId);
|
|
686
|
+
const old = await json(`${base}/dsh-remote/im-dingtalk-auth/status?id=${encodeURIComponent(a.body.authId)}`);
|
|
687
|
+
assert.equal(old.body.status, "failed");
|
|
688
|
+
assert.equal(old.body.code, "cancelled", "旧授权必须被作废(而不是继续有效)");
|
|
689
|
+
const cur = await json(`${base}/dsh-remote/im-dingtalk-auth/status?id=${encodeURIComponent(b.body.authId)}`);
|
|
690
|
+
assert.equal(cur.body.status, "waiting");
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
test("GET status:waiting → success 时把凭据写入 im-bots.json(0600、credentialSource=scan),响应不回 secret", async () => {
|
|
695
|
+
const relayDir = await tmpRelay();
|
|
696
|
+
const imAuthFetch = makeAuthFetch({
|
|
697
|
+
polls: [
|
|
698
|
+
{ status: "WAITING" },
|
|
699
|
+
{ status: "SUCCESS", client_id: "ding-client-8899", client_secret: "DING-SCAN-SECRET" },
|
|
700
|
+
],
|
|
701
|
+
});
|
|
702
|
+
await withHost(relayDir, { imAuthFetch }, async (base) => {
|
|
703
|
+
const begin = await post(base, `${base}/dsh-remote/im-dingtalk-auth/begin`);
|
|
704
|
+
const id = encodeURIComponent(begin.body.authId);
|
|
705
|
+
let st = await json(`${base}/dsh-remote/im-dingtalk-auth/status?id=${id}`);
|
|
706
|
+
assert.equal(st.body.status, "waiting", "用户还没确认 → waiting");
|
|
707
|
+
st = await json(`${base}/dsh-remote/im-dingtalk-auth/status?id=${id}`);
|
|
708
|
+
assert.equal(st.body.status, "success");
|
|
709
|
+
assert.equal(st.body.credentialSource, "scan");
|
|
710
|
+
assert.equal(st.body.hasSecret, true);
|
|
711
|
+
assert.equal(st.body.clientIdTail, "8899");
|
|
712
|
+
assert.ok(!JSON.stringify(st.body).includes("DING-SCAN-SECRET"), "★ 扫码拿到的 secret 绝不能回给浏览器");
|
|
713
|
+
|
|
714
|
+
const cfg = JSON.parse(await readFile(path.join(relayDir, "im-bots.json"), "utf8"));
|
|
715
|
+
assert.equal(cfg.dingtalk.clientId, "ding-client-8899");
|
|
716
|
+
assert.equal(cfg.dingtalk.clientSecret, "DING-SCAN-SECRET", "凭据要写入配置文件(桥读)");
|
|
717
|
+
assert.equal(cfg.dingtalk.credentialSource, "scan");
|
|
718
|
+
assert.equal(cfg.dingtalk.enabled, true, "扫码成功即可用");
|
|
719
|
+
const mode = (await stat(path.join(relayDir, "im-bots.json"))).mode & 0o777;
|
|
720
|
+
assert.equal(mode, 0o600, "配置文件必须 0600");
|
|
721
|
+
|
|
722
|
+
// 面板 GET 里也只见 hasSecret,不见 secret
|
|
723
|
+
const view = await json(`${base}/dsh-remote/im-bots`);
|
|
724
|
+
assert.equal(view.body.dingtalk.hasSecret, true);
|
|
725
|
+
assert.equal(view.body.dingtalk.credentialSource, "scan");
|
|
726
|
+
assert.ok(!JSON.stringify(view.body).includes("DING-SCAN-SECRET"));
|
|
727
|
+
});
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
test("GET status:未知/过期 authId → expired;钉钉拒绝 → failed(dingtalk_api_rejected);网络不通 → failed(network_error)", async () => {
|
|
731
|
+
const relayDir = await tmpRelay();
|
|
732
|
+
await withHost(relayDir, { imAuthFetch: makeAuthFetch() }, async (base) => {
|
|
733
|
+
const none = await json(`${base}/dsh-remote/im-dingtalk-auth/status?id=nope`);
|
|
734
|
+
assert.equal(none.body.status, "expired");
|
|
735
|
+
assert.equal(none.body.code, "expired");
|
|
736
|
+
|
|
737
|
+
// 钉钉拒绝 poll
|
|
738
|
+
const rejDir = await tmpRelay();
|
|
739
|
+
const rejFetch = makeAuthFetch({ polls: [{ status: "DENIED", message: "用户取消了创建" }] });
|
|
740
|
+
await withHost(rejDir, { imAuthFetch: rejFetch }, async (base2) => {
|
|
741
|
+
const b = await post(base2, `${base2}/dsh-remote/im-dingtalk-auth/begin`);
|
|
742
|
+
const st = await json(`${base2}/dsh-remote/im-dingtalk-auth/status?id=${encodeURIComponent(b.body.authId)}`);
|
|
743
|
+
assert.equal(st.body.status, "failed");
|
|
744
|
+
assert.equal(st.body.code, "dingtalk_api_rejected");
|
|
745
|
+
assert.ok(st.body.message.includes("用户取消"), "钉钉原文原因要带给用户");
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
// 网络错误(poll 抛错)
|
|
749
|
+
const netDir = await tmpRelay();
|
|
750
|
+
const netFetch = makeAuthFetch({ pollThrows: true });
|
|
751
|
+
await withHost(netDir, { imAuthFetch: netFetch }, async (base3) => {
|
|
752
|
+
const b = await post(base3, `${base3}/dsh-remote/im-dingtalk-auth/begin`);
|
|
753
|
+
const st = await json(`${base3}/dsh-remote/im-dingtalk-auth/status?id=${encodeURIComponent(b.body.authId)}`);
|
|
754
|
+
assert.equal(st.body.status, "failed");
|
|
755
|
+
assert.equal(st.body.code, "network_error");
|
|
756
|
+
});
|
|
757
|
+
});
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
test("POST begin:钉钉接口不可用 → network_error / dingtalk_api_rejected(人话)", async () => {
|
|
761
|
+
const dir1 = await tmpRelay();
|
|
762
|
+
await withHost(dir1, { imAuthFetch: makeAuthFetch({ initThrows: true }) }, async (base) => {
|
|
763
|
+
const r = await post(base, `${base}/dsh-remote/im-dingtalk-auth/begin`);
|
|
764
|
+
assert.equal(r.status, 502);
|
|
765
|
+
assert.equal(r.body.ok, false);
|
|
766
|
+
assert.equal(r.body.code, "network_error");
|
|
767
|
+
assert.match(r.body.message, /网络不通/);
|
|
768
|
+
});
|
|
769
|
+
const dir2 = await tmpRelay();
|
|
770
|
+
await withHost(dir2, { imAuthFetch: makeAuthFetch({ beginRejected: true }) }, async (base) => {
|
|
771
|
+
const r = await post(base, `${base}/dsh-remote/im-dingtalk-auth/begin`);
|
|
772
|
+
assert.equal(r.body.code, "dingtalk_api_rejected");
|
|
773
|
+
});
|
|
774
|
+
});
|