-
Notifications
You must be signed in to change notification settings - Fork 902
/
ensureApiEnabled.spec.ts
117 lines (91 loc) · 3.79 KB
/
ensureApiEnabled.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
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
import { expect } from "chai";
import * as nock from "nock";
import { check, ensure, POLL_SETTINGS } from "../ensureApiEnabled";
const FAKE_PROJECT_ID = "my_project";
const FAKE_API = "myapi.googleapis.com";
describe("ensureApiEnabled", () => {
describe("check", () => {
before(() => {
nock.disableNetConnect();
});
after(() => {
nock.enableNetConnect();
});
it("should call the API to check if it's enabled", async () => {
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.reply(200, { state: "ENABLED" });
await check(FAKE_PROJECT_ID, FAKE_API, "", true);
expect(nock.isDone()).to.be.true;
});
it("should return the value from the API", async () => {
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "ENABLED" });
await expect(check(FAKE_PROJECT_ID, FAKE_API, "", true)).to.eventually.be.true;
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "DISABLED" });
await expect(check(FAKE_PROJECT_ID, FAKE_API, "", true)).to.eventually.be.false;
});
});
describe("ensure", () => {
const originalPollInterval = POLL_SETTINGS.pollInterval;
const originalPollsBeforeRetry = POLL_SETTINGS.pollsBeforeRetry;
beforeEach(() => {
nock.disableNetConnect();
POLL_SETTINGS.pollInterval = 0;
POLL_SETTINGS.pollsBeforeRetry = 0; // Zero means "one check".
});
afterEach(() => {
nock.enableNetConnect();
POLL_SETTINGS.pollInterval = originalPollInterval;
POLL_SETTINGS.pollsBeforeRetry = originalPollsBeforeRetry;
});
it("should verify that the API is enabled, and stop if it is", async () => {
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "ENABLED" });
await expect(ensure(FAKE_PROJECT_ID, FAKE_API, "", true)).to.not.be.rejected;
});
it("should attempt to enable the API if it is not enabled", async () => {
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "DISABLED" });
nock("https://serviceusage.googleapis.com")
.post(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}:enable`)
.once()
.reply(200);
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "ENABLED" });
await expect(ensure(FAKE_PROJECT_ID, FAKE_API, "", true)).to.not.be.rejected;
expect(nock.isDone()).to.be.true;
});
it("should retry enabling the API if it does not enable in time", async () => {
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "DISABLED" });
nock("https://serviceusage.googleapis.com")
.post(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}:enable`)
.twice()
.reply(200);
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "DISABLED" });
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}`)
.once()
.reply(200, { state: "ENABLED" });
await expect(ensure(FAKE_PROJECT_ID, FAKE_API, "", true)).to.not.be.rejected;
expect(nock.isDone()).to.be.true;
});
});
});