-
-
Notifications
You must be signed in to change notification settings - Fork 76.3k
Expand file tree
/
Copy pathgmail-setup-utils.test.ts
More file actions
208 lines (186 loc) · 6.1 KB
/
gmail-setup-utils.test.ts
File metadata and controls
208 lines (186 loc) · 6.1 KB
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
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { withEnvAsync } from "../test-utils/env.js";
import {
ensureTailscaleEndpoint,
resetGmailSetupUtilsCachesForTest,
resolvePythonExecutablePath,
runGcloud,
} from "./gmail-setup-utils.js";
const itUnix = process.platform === "win32" ? it.skip : it;
const runCommandWithTimeoutMock = vi.fn();
vi.mock("../process/exec.js", () => ({
runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args),
}));
beforeEach(() => {
runCommandWithTimeoutMock.mockClear();
resetGmailSetupUtilsCachesForTest();
});
describe("resolvePythonExecutablePath", () => {
itUnix(
"resolves a working python path and caches the result",
async () => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-python-"));
try {
const realPython = path.join(tmp, "python-real");
await fs.writeFile(realPython, "#!/bin/sh\nexit 0\n", "utf-8");
await fs.chmod(realPython, 0o755);
const shimDir = path.join(tmp, "shims");
await fs.mkdir(shimDir, { recursive: true });
const shim = path.join(shimDir, "python3");
await fs.writeFile(shim, "#!/bin/sh\nexit 0\n", "utf-8");
await fs.chmod(shim, 0o755);
await withEnvAsync({ PATH: `${shimDir}${path.delimiter}/usr/bin` }, async () => {
runCommandWithTimeoutMock.mockResolvedValue({
stdout: `${realPython}\n`,
stderr: "",
code: 0,
signal: null,
killed: false,
});
const resolved = await resolvePythonExecutablePath();
expect(resolved).toBe(realPython);
await withEnvAsync({ PATH: "/bin" }, async () => {
const cached = await resolvePythonExecutablePath();
expect(cached).toBe(realPython);
});
expect(runCommandWithTimeoutMock).toHaveBeenCalledTimes(1);
});
} finally {
await fs.rm(tmp, { recursive: true, force: true });
}
},
60_000,
);
});
describe("runGcloud", () => {
itUnix(
"overrides an inherited CLOUDSDK_PYTHON value with a resolved interpreter",
async () => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gcloud-python-"));
try {
const realPython = path.join(tmp, "python-real");
await fs.writeFile(realPython, "#!/bin/sh\nexit 0\n", "utf-8");
await fs.chmod(realPython, 0o755);
const shimDir = path.join(tmp, "shims");
await fs.mkdir(shimDir, { recursive: true });
const shim = path.join(shimDir, "python3");
await fs.writeFile(shim, "#!/bin/sh\nexit 0\n", "utf-8");
await fs.chmod(shim, 0o755);
await withEnvAsync(
{
CLOUDSDK_PYTHON: path.join(tmp, "evil", "python"),
PATH: `${shimDir}${path.delimiter}/usr/bin`,
},
async () => {
runCommandWithTimeoutMock
.mockResolvedValueOnce({
stdout: `${realPython}\n`,
stderr: "",
code: 0,
signal: null,
killed: false,
})
.mockResolvedValueOnce({
stdout: "",
stderr: "",
code: 0,
signal: null,
killed: false,
});
await runGcloud(["config", "list"]);
expect(runCommandWithTimeoutMock).toHaveBeenLastCalledWith(
["gcloud", "config", "list"],
{
timeoutMs: 120_000,
env: { CLOUDSDK_PYTHON: realPython },
},
);
},
);
} finally {
await fs.rm(tmp, { recursive: true, force: true });
}
},
60_000,
);
itUnix("unsets inherited CLOUDSDK_PYTHON when no trusted interpreter is found", async () => {
await withEnvAsync(
{
CLOUDSDK_PYTHON: "/tmp/attacker-python",
PATH: "",
},
async () => {
runCommandWithTimeoutMock.mockResolvedValueOnce({
stdout: "",
stderr: "",
code: 0,
signal: null,
killed: false,
});
await runGcloud(["config", "list"]);
expect(runCommandWithTimeoutMock).toHaveBeenCalledTimes(1);
expect(runCommandWithTimeoutMock).toHaveBeenCalledWith(["gcloud", "config", "list"], {
timeoutMs: 120_000,
env: { CLOUDSDK_PYTHON: undefined },
});
},
);
});
});
describe("ensureTailscaleEndpoint", () => {
it("includes stdout and exit code when tailscale serve fails", async () => {
runCommandWithTimeoutMock
.mockResolvedValueOnce({
stdout: JSON.stringify({ Self: { DNSName: "host.tailnet.ts.net." } }),
stderr: "",
code: 0,
signal: null,
killed: false,
})
.mockResolvedValueOnce({
stdout: "tailscale output",
stderr: "Warning: client version mismatch",
code: 1,
signal: null,
killed: false,
});
let message = "";
try {
await ensureTailscaleEndpoint({
mode: "serve",
path: "/gmail-pubsub",
port: 8788,
});
} catch (err) {
message = err instanceof Error ? err.message : String(err);
}
expect(message).toContain("code=1");
expect(message).toContain("stderr: Warning: client version mismatch");
expect(message).toContain("stdout: tailscale output");
});
it("includes JSON parse failure details with stdout", async () => {
runCommandWithTimeoutMock.mockResolvedValueOnce({
stdout: "not-json",
stderr: "",
code: 0,
signal: null,
killed: false,
});
let message = "";
try {
await ensureTailscaleEndpoint({
mode: "funnel",
path: "/gmail-pubsub",
port: 8788,
});
} catch (err) {
message = err instanceof Error ? err.message : String(err);
}
expect(message).toContain("returned invalid JSON");
expect(message).toContain("stdout: not-json");
expect(message).toContain("code=0");
});
});