|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +// Unit coverage for the bundled telemetry pac-auth copy. The plugin ships a |
| 4 | +// physical copy of shared/telemetry/lib/pac-auth.js (no symlink), so this test |
| 5 | +// asserts the copy parses `pac auth who` the same way — including the optional |
| 6 | +// "Entra ID Object Id" line surfaced as `objectId`. The emit-* hook tests are |
| 7 | +// spawn-based integration tests that call real `pac`, so they can't inject a |
| 8 | +// fake object id; this is the deterministic seam for that field. |
| 9 | + |
| 10 | +const test = require("node:test"); |
| 11 | +const assert = require("node:assert/strict"); |
| 12 | + |
| 13 | +const pacAuth = require("../lib/telemetry/lib/pac-auth"); |
| 14 | + |
| 15 | +const SAMPLE_OUTPUT = `Type: Universal |
| 16 | +Cloud: Public |
| 17 | +Tenant Id: 11111111-1111-1111-1111-111111111111 |
| 18 | +Tenant Country: US |
| 19 | +User: user@example.com |
| 20 | +Entra ID Object Id: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa |
| 21 | +PUID: 10000000ABCDEF01 |
| 22 | +User Country/Region: US |
| 23 | +Token Expires: 2026-05-05T18:00:00Z |
| 24 | +Authority: https://login.microsoftonline.com/... |
| 25 | +Environment Geo: NorthAmerica |
| 26 | +Environment Id: 22222222-2222-2222-2222-222222222222 |
| 27 | +Environment Type: Sandbox |
| 28 | +Organization Id: 33333333-3333-3333-3333-333333333333 |
| 29 | +Organization Unique Name: contoso |
| 30 | +Organization Friendly Name: Contoso |
| 31 | +`; |
| 32 | + |
| 33 | +test("parses orgId, tenantId, cloud, and Entra ID objectId", () => { |
| 34 | + pacAuth._resetCache(); |
| 35 | + const result = pacAuth.readPacAuth({ _exec: () => SAMPLE_OUTPUT }); |
| 36 | + assert.deepEqual(result, { |
| 37 | + orgId: "33333333-3333-3333-3333-333333333333", |
| 38 | + tenantId: "11111111-1111-1111-1111-111111111111", |
| 39 | + cloud: "Public", |
| 40 | + objectId: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +test("objectId is '' when the Entra ID Object Id line is missing", () => { |
| 45 | + pacAuth._resetCache(); |
| 46 | + const result = pacAuth.readPacAuth({ |
| 47 | + _exec: () => |
| 48 | + "Cloud: Public\n" + |
| 49 | + "Tenant Id: 11111111-1111-1111-1111-111111111111\n" + |
| 50 | + "Organization Id: 33333333-3333-3333-3333-333333333333\n", |
| 51 | + }); |
| 52 | + assert.equal(result.objectId, ""); |
| 53 | +}); |
| 54 | + |
| 55 | +test("returns null when neither Tenant Id nor Organization Id is found", () => { |
| 56 | + pacAuth._resetCache(); |
| 57 | + const result = pacAuth.readPacAuth({ |
| 58 | + _exec: () => "Type: Universal\nCloud: Public\n", |
| 59 | + }); |
| 60 | + assert.equal(result, null); |
| 61 | +}); |
| 62 | + |
| 63 | +test("returns null when pac is missing (ENOENT)", () => { |
| 64 | + pacAuth._resetCache(); |
| 65 | + const result = pacAuth.readPacAuth({ |
| 66 | + _exec: () => { |
| 67 | + const e = new Error("spawn pac ENOENT"); |
| 68 | + e.code = "ENOENT"; |
| 69 | + throw e; |
| 70 | + }, |
| 71 | + }); |
| 72 | + assert.equal(result, null); |
| 73 | +}); |
| 74 | + |
| 75 | +test("caches result across calls (single fork per process)", () => { |
| 76 | + pacAuth._resetCache(); |
| 77 | + let calls = 0; |
| 78 | + const exec = () => { |
| 79 | + calls++; |
| 80 | + return SAMPLE_OUTPUT; |
| 81 | + }; |
| 82 | + pacAuth.readPacAuth({ _exec: exec }); |
| 83 | + pacAuth.readPacAuth({ _exec: exec }); |
| 84 | + assert.equal(calls, 1, "second call should hit cache"); |
| 85 | +}); |
0 commit comments