-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathdefaultCredentials.spec.ts
84 lines (68 loc) · 2.3 KB
/
defaultCredentials.spec.ts
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
import { expect } from "chai";
import * as sinon from "sinon";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import * as api from "./api";
import { configstore } from "./configstore";
import * as defaultCredentials from "./defaultCredentials";
import { getGlobalDefaultAccount } from "./auth";
import { Account } from "./types/auth";
describe("defaultCredentials", () => {
const sandbox: sinon.SinonSandbox = sinon.createSandbox();
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "firebase-tools"));
const FAKE_TOKEN = {
refresh_token: "abc123",
};
const FAKE_USER = {
email: "user@domain.com",
};
let configStub: sinon.SinonStub;
let oldHome: any;
let account: Account;
beforeEach(() => {
oldHome = process.env.HOME;
process.env.HOME = tmpDir;
// Default configStore mock
configStub = sandbox.stub(configstore, "get");
configStub.callsFake((key: string) => {
if (key === "tokens") {
return FAKE_TOKEN;
}
if (key === "user") {
return FAKE_USER;
}
});
account = getGlobalDefaultAccount()!;
});
afterEach(() => {
process.env.HOME = oldHome;
sandbox.restore();
});
it("creates a credential file when there are tokens in the config", async () => {
const credPath = await defaultCredentials.getCredentialPathAsync(account);
expect(credPath)
.to.be.a("string")
.that.satisfies((x: string) => {
return x.startsWith(tmpDir);
});
const fileContents = JSON.parse(fs.readFileSync(credPath!).toString());
expect(fileContents).to.eql({
client_id: api.clientId(),
client_secret: api.clientSecret(),
refresh_token: FAKE_TOKEN.refresh_token,
type: "authorized_user",
});
});
it("can clear credentials", async () => {
const credPath = await defaultCredentials.getCredentialPathAsync(account);
expect(fs.existsSync(credPath!)).to.be.true;
defaultCredentials.clearCredentials(account);
expect(fs.existsSync(credPath!)).to.be.false;
});
it("includes the users email in the path", async () => {
const credPath = await defaultCredentials.getCredentialPathAsync(account);
const baseName = path.basename(credPath!);
expect(baseName).to.eq("user_domain_com_application_default_credentials.json");
});
});