|
1 | 1 | // Msteams tests cover token plugin behavior. |
2 | | -import { existsSync, mkdtempSync, rmSync } from "node:fs"; |
| 2 | +import { generateKeyPairSync } from "node:crypto"; |
| 3 | +import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
3 | 4 | import os from "node:os"; |
4 | 5 | import path from "node:path"; |
5 | 6 | import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime"; |
6 | 7 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
7 | 8 | import type { MSTeamsConfig } from "../runtime-api.js"; |
8 | 9 | import { setMSTeamsRuntime } from "./runtime.js"; |
| 10 | +import { loadMSTeamsSdkWithAuth } from "./sdk.js"; |
9 | 11 | import { msteamsRuntimeStub } from "./test-support/runtime.js"; |
10 | 12 | import { readAccessToken } from "./token-response.js"; |
11 | 13 | import { |
@@ -143,6 +145,119 @@ describe("token – federated credentials (certificate)", () => { |
143 | 145 | expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(false); |
144 | 146 | }); |
145 | 147 |
|
| 148 | + it("ignores blank certificate settings", () => { |
| 149 | + process.env.MSTEAMS_CERTIFICATE_PATH = " "; |
| 150 | + const cfg = { |
| 151 | + appId: "app-id", |
| 152 | + tenantId: "tenant-id", |
| 153 | + authType: "federated", |
| 154 | + certificatePath: " ", |
| 155 | + } satisfies MSTeamsConfig; |
| 156 | + |
| 157 | + expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(false); |
| 158 | + expect(resolveMSTeamsCredentials(cfg)).toBeUndefined(); |
| 159 | + }); |
| 160 | + |
| 161 | + it("falls back to env certificate settings without changing path bytes", () => { |
| 162 | + process.env.MSTEAMS_CERTIFICATE_PATH = " /env/cert.pem "; |
| 163 | + process.env.MSTEAMS_CERTIFICATE_THUMBPRINT = "environment-thumbprint"; |
| 164 | + process.env.MSTEAMS_MANAGED_IDENTITY_CLIENT_ID = "environment-managed-identity"; |
| 165 | + const cfg = { |
| 166 | + appId: "app-id", |
| 167 | + tenantId: "tenant-id", |
| 168 | + authType: "federated", |
| 169 | + certificatePath: " ", |
| 170 | + certificateThumbprint: " configured-thumbprint ", |
| 171 | + managedIdentityClientId: " configured-managed-identity ", |
| 172 | + } satisfies MSTeamsConfig; |
| 173 | + |
| 174 | + expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true); |
| 175 | + expect(resolveMSTeamsCredentials(cfg)).toEqual({ |
| 176 | + type: "federated", |
| 177 | + appId: "app-id", |
| 178 | + tenantId: "tenant-id", |
| 179 | + certificatePath: " /env/cert.pem ", |
| 180 | + certificateThumbprint: " configured-thumbprint ", |
| 181 | + useManagedIdentity: undefined, |
| 182 | + managedIdentityClientId: " configured-managed-identity ", |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + it("opens the exact environment certificate path when the configured path is blank", async () => { |
| 187 | + const certificateDirectory = mkdtempSync( |
| 188 | + path.join(os.tmpdir(), "openclaw-msteams-real-certificate-"), |
| 189 | + ); |
| 190 | + const certificatePath = path.join(certificateDirectory, " certificate.pem"); |
| 191 | + |
| 192 | + try { |
| 193 | + const { privateKey } = generateKeyPairSync("rsa", { |
| 194 | + modulusLength: 2048, |
| 195 | + publicKeyEncoding: { type: "spki", format: "pem" }, |
| 196 | + privateKeyEncoding: { type: "pkcs8", format: "pem" }, |
| 197 | + }); |
| 198 | + writeFileSync(certificatePath, privateKey, { mode: 0o600 }); |
| 199 | + process.env.MSTEAMS_CERTIFICATE_PATH = certificatePath; |
| 200 | + |
| 201 | + const cfg = { |
| 202 | + appId: "app-id", |
| 203 | + tenantId: "tenant-id", |
| 204 | + authType: "federated", |
| 205 | + certificatePath: " ", |
| 206 | + } satisfies MSTeamsConfig; |
| 207 | + |
| 208 | + expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true); |
| 209 | + const credentials = resolveMSTeamsCredentials(cfg); |
| 210 | + expect(credentials).toMatchObject({ |
| 211 | + type: "federated", |
| 212 | + certificatePath, |
| 213 | + }); |
| 214 | + if (!credentials) { |
| 215 | + throw new Error("expected configured Microsoft Teams certificate credentials"); |
| 216 | + } |
| 217 | + |
| 218 | + const { app } = await loadMSTeamsSdkWithAuth(credentials); |
| 219 | + expect(app.tokenManager).toBeDefined(); |
| 220 | + } finally { |
| 221 | + rmSync(certificateDirectory, { recursive: true, force: true }); |
| 222 | + } |
| 223 | + }); |
| 224 | + |
| 225 | + it("preserves configured nonblank certificate path bytes and precedence", () => { |
| 226 | + process.env.MSTEAMS_CERTIFICATE_PATH = "/environment/certificate.pem"; |
| 227 | + const certificatePath = " /configured/certificate.pem "; |
| 228 | + const cfg = { |
| 229 | + appId: "app-id", |
| 230 | + tenantId: "tenant-id", |
| 231 | + authType: "federated", |
| 232 | + certificatePath, |
| 233 | + } satisfies MSTeamsConfig; |
| 234 | + |
| 235 | + expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true); |
| 236 | + expect(resolveMSTeamsCredentials(cfg)).toMatchObject({ certificatePath }); |
| 237 | + }); |
| 238 | + |
| 239 | + it("keeps managed identity configured when the optional certificate path is blank", () => { |
| 240 | + const cfg = { |
| 241 | + appId: "app-id", |
| 242 | + tenantId: "tenant-id", |
| 243 | + authType: "federated", |
| 244 | + certificatePath: " ", |
| 245 | + useManagedIdentity: true, |
| 246 | + managedIdentityClientId: " configured-managed-identity ", |
| 247 | + } satisfies MSTeamsConfig; |
| 248 | + |
| 249 | + expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true); |
| 250 | + expect(resolveMSTeamsCredentials(cfg)).toEqual({ |
| 251 | + type: "federated", |
| 252 | + appId: "app-id", |
| 253 | + tenantId: "tenant-id", |
| 254 | + certificatePath: undefined, |
| 255 | + certificateThumbprint: undefined, |
| 256 | + useManagedIdentity: true, |
| 257 | + managedIdentityClientId: " configured-managed-identity ", |
| 258 | + }); |
| 259 | + }); |
| 260 | + |
146 | 261 | it("resolves federated credentials with certificate from config", () => { |
147 | 262 | const cfg = { |
148 | 263 | appId: "app-id", |
|
0 commit comments