/
lro.create.spec.ts
95 lines (79 loc) · 3.29 KB
/
lro.create.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { assert } from "chai";
import { Context } from "mocha";
import { PollerStoppedError } from "@azure/core-lro";
import { env, Recorder } from "@azure-tools/test-recorder";
import { CertificateClient, KeyVaultCertificate, DefaultCertificatePolicy } from "../../src";
import { testPollerProperties } from "../utils/recorderUtils";
import { authenticate } from "../utils/testAuthentication";
import { getServiceVersion } from "../utils/utils.common";
import TestClient from "../utils/testClient";
describe("Certificates client - LRO - create", () => {
const certificatePrefix = `lroCreate${env.CERTIFICATE_NAME || "CertificateName"}`;
let certificateSuffix: string;
let client: CertificateClient;
let testClient: TestClient;
let recorder: Recorder;
beforeEach(async function (this: Context) {
const authentication = await authenticate(this, getServiceVersion());
certificateSuffix = authentication.suffix;
client = authentication.client;
testClient = authentication.testClient;
recorder = authentication.recorder;
});
afterEach(async function () {
await recorder.stop();
});
// The tests follow
it("can wait until a certificate is created", async function (this: Context) {
const certificateName = testClient.formatName(
`${certificatePrefix}-${this!.test!.title}-${certificateSuffix}`
);
const poller = await client.beginCreateCertificate(
certificateName,
DefaultCertificatePolicy,
testPollerProperties
);
assert.ok(poller.getOperationState().isStarted);
// The pending certificate can be obtained this way:
assert.equal(poller.getOperationState().result!.name, certificateName);
const createdCertificate: KeyVaultCertificate = await poller.pollUntilDone();
assert.equal(createdCertificate.name, certificateName);
assert.ok(poller.getOperationState().isCompleted);
// The final certificate can also be obtained this way:
assert.equal(poller.getOperationState().result!.name, certificateName);
});
it("can resume from a stopped poller", async function (this: Context) {
const certificateName = testClient.formatName(
`${certificatePrefix}-${this!.test!.title}-${certificateSuffix}`
);
const poller = await client.beginCreateCertificate(
certificateName,
DefaultCertificatePolicy,
testPollerProperties
);
assert.ok(poller.getOperationState().isStarted);
poller.pollUntilDone().catch((e) => {
assert.ok(e instanceof PollerStoppedError);
assert.equal(e.name, "PollerStoppedError");
assert.equal(e.message, "This poller is already stopped");
});
poller.stopPolling();
assert.ok(poller.isStopped());
assert.ok(!poller.getOperationState().isCompleted);
const serialized = poller.toString();
const resumePoller = await client.beginCreateCertificate(
certificateName,
DefaultCertificatePolicy,
{
resumeFrom: serialized,
...testPollerProperties,
}
);
assert.ok(resumePoller.getOperationState().isStarted);
const createdCertificate: KeyVaultCertificate = await resumePoller.pollUntilDone();
assert.equal(createdCertificate.name, certificateName);
assert.ok(resumePoller.getOperationState().isCompleted);
});
});