-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathacquire.test.ts
156 lines (135 loc) · 5.14 KB
/
acquire.test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import "mocha";
import * as sinon from "sinon";
import chai from "chai";
import fs from "fs-extra";
import { PackageService } from "../../../../src/component/m365/packageService";
import { M365TitleAcquireDriver } from "../../../../src/component/driver/m365/acquire";
import { MockedLogProvider, MockedUserInteraction } from "../../../plugins/solution/util";
import {
FileNotFoundError,
InvalidActionInputError,
UnhandledError,
} from "../../../../src/error/common";
import { MockedM365Provider } from "../../../core/utils";
describe("teamsApp/extendToM365", async () => {
const acquireDriver = new M365TitleAcquireDriver();
const mockedDriverContext: any = {
m365TokenProvider: new MockedM365Provider(),
logProvider: new MockedLogProvider(),
ui: new MockedUserInteraction(),
projectPath: "./",
};
afterEach(() => {
sinon.restore();
});
it("run: happy path", async () => {
const args = {
appPackagePath: "fakePath",
};
const result = await acquireDriver.run(args, mockedDriverContext);
chai.assert.isTrue(result.isErr());
if (result.isErr()) {
chai.assert.isTrue(result.error instanceof InvalidActionInputError);
chai.assert.isTrue(result.error.message.includes("writeToEnvironmentFile"));
}
});
it("execute: invalid param error", async () => {
const args = {
appPackagePath: false,
} as any;
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);
const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert(result.result.isErr());
if (result.result.isErr()) {
chai.assert.isTrue(result.result.error instanceof InvalidActionInputError);
chai.assert.isTrue(result.result.error.message.includes("appPackagePath"));
}
});
it("execute: writeToEnvironmentFile undefined", async () => {
const args = {
appPackagePath: "fakePath",
};
const result = await acquireDriver.execute(args, mockedDriverContext, undefined);
chai.assert(result.result.isErr());
if (result.result.isErr()) {
chai.assert.isTrue(result.result.error instanceof InvalidActionInputError);
chai.assert.isTrue(result.result.error.message.includes("writeToEnvironmentFile"));
}
});
it("execute: missing titleId", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([["appId", "MY_APP_ID"]]);
const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert(result.result.isErr());
if (result.result.isErr()) {
chai.assert.isTrue(result.result.error instanceof InvalidActionInputError);
chai.assert.isTrue(result.result.error.message.includes("writeToEnvironmentFile"));
}
});
it("execute: missing appId", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([["titleId", "MY_TITLE_ID"]]);
const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert(result.result.isErr());
if (result.result.isErr()) {
chai.assert.isTrue(result.result.error instanceof InvalidActionInputError);
chai.assert.isTrue(result.result.error.message.includes("writeToEnvironmentFile"));
}
});
it("execute: should throw error if file not exists", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);
const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert(result.result.isErr());
if (result.result.isErr()) {
chai.assert.isTrue(result.result.error instanceof FileNotFoundError);
}
});
it("execute: unhandled error", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);
sinon.stub(PackageService.prototype, "sideLoading").throws(new Error("test error"));
sinon.stub(fs, "pathExists").resolves(true);
const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert(result.result.isErr());
if (result.result.isErr()) {
chai.assert.isTrue(result.result.error instanceof UnhandledError);
}
});
it("execute: happy path", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);
sinon
.stub(PackageService.prototype, "sideLoading")
.resolves(["test-title-id", "test-app-id", ""]);
sinon.stub(fs, "pathExists").resolves(true);
const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert.isTrue(result.result.isOk());
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
});
});