Skip to content

Commit 6a11b91

Browse files
test: add ut to raise code coverage
1 parent 8183cad commit 6a11b91

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed

packages/fx-core/src/component/coordinator/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class Coordinator {
440440

441441
const checkM365TenatRes = provisionUtils.ensureM365TenantMatchesV3(
442442
tenantSwitchCheckActions,
443-
m365tenantInfo?.tenantIdInToken
443+
m365tenantInfo.tenantIdInToken
444444
);
445445
if (checkM365TenatRes.isErr()) {
446446
const msg = getLocalizedString("core.provision.switchAccount");

packages/fx-core/tests/component/coordinator/coordinator.provision.test.ts

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,87 @@ describe("coordinator provision", () => {
15131513
assert.equal(res.error.name, "checkM365TenantError");
15141514
}
15151515
});
1516+
it("provision failed with resource group after user succeed to re-login m365 tenant", async () => {
1517+
const mockProjectModel: ProjectModel = {
1518+
version: "1.0.0",
1519+
provision: {
1520+
name: "configureApp",
1521+
driverDefs: [
1522+
{
1523+
uses: "arm/deploy",
1524+
with: undefined,
1525+
},
1526+
{
1527+
uses: "teamsApp/create",
1528+
with: undefined,
1529+
},
1530+
],
1531+
resolvePlaceholders: () => {
1532+
return ["AZURE_SUBSCRIPTION_ID", "AZURE_RESOURCE_GROUP_NAME"];
1533+
},
1534+
execute: async (ctx: DriverContext): Promise<ExecutionResult> => {
1535+
return { result: ok(new Map()), summaries: [] };
1536+
},
1537+
resolveDriverInstances: mockedResolveDriverInstances,
1538+
},
1539+
};
1540+
sandbox.stub(metadataUtil, "parse").resolves(ok(mockProjectModel));
1541+
sandbox.stub(envUtil, "listEnv").resolves(ok(["dev", "prod"]));
1542+
sandbox.stub(envUtil, "readEnv").resolves(ok({}));
1543+
sandbox.stub(envUtil, "writeEnv").resolves(ok(undefined));
1544+
sandbox.stub(provisionUtils, "ensureResourceGroup").resolves(
1545+
ok({
1546+
createNewResourceGroup: true,
1547+
name: "test-rg",
1548+
location: "East US",
1549+
})
1550+
);
1551+
sandbox.stub(provisionUtils, "getM365TenantId").resolves(
1552+
ok({
1553+
tenantIdInToken: "mockM365Tenant",
1554+
tenantUserName: "mockM365UserName",
1555+
})
1556+
);
1557+
sandbox.stub(provisionUtils, "askForProvisionConsentV3").resolves(ok(undefined));
1558+
sandbox
1559+
.stub(provisionUtils, "ensureM365TenantMatchesV3")
1560+
.onFirstCall()
1561+
.returns(err(new UserError("coordinator", "checkM365TenantError", "msg", "msg")))
1562+
.onSecondCall()
1563+
.returns(ok(undefined));
1564+
sandbox.stub(tools.tokenProvider.azureAccountProvider, "getSelectedSubscription").resolves({
1565+
subscriptionId: "mockSubId",
1566+
tenantId: "mockTenantId",
1567+
subscriptionName: "mockSubName",
1568+
});
1569+
sandbox.stub(tools.tokenProvider.azureAccountProvider, "setSubscription").resolves();
1570+
sandbox.stub(tools.tokenProvider.m365TokenProvider, "signout").resolves();
1571+
sandbox.stub(tools.ui, "selectOption").callsFake(async (config) => {
1572+
if (config.name === "env") {
1573+
return ok({ type: "success", result: "dev" });
1574+
} else {
1575+
return ok({ type: "success", result: "" });
1576+
}
1577+
});
1578+
sandbox.stub(tools.ui, "showMessage").resolves(ok("Continue"));
1579+
sandbox.stub(pathUtils, "getEnvFilePath").resolves(ok("."));
1580+
sandbox.stub(pathUtils, "getYmlFilePath").returns("m365agents.yml");
1581+
sandbox.stub(fs, "pathExistsSync").onFirstCall().returns(false).onSecondCall().returns(true);
1582+
const inputs: Inputs = {
1583+
platform: Platform.VSCode,
1584+
projectPath: ".",
1585+
ignoreLockByUT: true,
1586+
};
1587+
sandbox
1588+
.stub(provisionUtils, "ensureSubscription")
1589+
.resolves(err(new UserError("coordinator", "ensureSubscriptionError", "msg", "msg")));
1590+
const fxCore = new FxCore(tools);
1591+
const res = await fxCore.provisionResources(inputs);
1592+
assert.isTrue(res.isErr());
1593+
if (res.isErr()) {
1594+
assert.equal(res.error.name, "ensureSubscriptionError");
1595+
}
1596+
});
15161597
it("provision failed with getting m365 tenant id after user retry", async () => {
15171598
const mockProjectModel: ProjectModel = {
15181599
version: "1.0.0",
@@ -1682,6 +1763,88 @@ describe("coordinator provision", () => {
16821763
assert.equal(res.error.name, "UserCancel");
16831764
}
16841765
});
1766+
it("provision failed with user exit after m365 tenant id mismatch", async () => {
1767+
const mockProjectModel: ProjectModel = {
1768+
version: "1.0.0",
1769+
provision: {
1770+
name: "configureApp",
1771+
driverDefs: [
1772+
{
1773+
uses: "arm/deploy",
1774+
with: undefined,
1775+
},
1776+
{
1777+
uses: "teamsApp/create",
1778+
with: undefined,
1779+
},
1780+
],
1781+
resolvePlaceholders: () => {
1782+
return ["AZURE_SUBSCRIPTION_ID", "AZURE_RESOURCE_GROUP_NAME"];
1783+
},
1784+
execute: async (ctx: DriverContext): Promise<ExecutionResult> => {
1785+
return { result: ok(new Map()), summaries: [] };
1786+
},
1787+
resolveDriverInstances: mockedResolveDriverInstances,
1788+
},
1789+
};
1790+
sandbox.stub(metadataUtil, "parse").resolves(ok(mockProjectModel));
1791+
sandbox.stub(envUtil, "listEnv").resolves(ok(["dev", "prod"]));
1792+
sandbox.stub(envUtil, "readEnv").resolves(ok({}));
1793+
sandbox.stub(envUtil, "writeEnv").resolves(ok(undefined));
1794+
sandbox.stub(provisionUtils, "ensureSubscription").resolves(
1795+
ok({
1796+
subscriptionId: "mockSubId",
1797+
tenantId: "mockTenantId",
1798+
subscriptionName: "mockSubName",
1799+
})
1800+
);
1801+
sandbox.stub(provisionUtils, "ensureResourceGroup").resolves(
1802+
ok({
1803+
createNewResourceGroup: true,
1804+
name: "test-rg",
1805+
location: "East US",
1806+
})
1807+
);
1808+
sandbox.stub(provisionUtils, "getM365TenantId").resolves(
1809+
ok({
1810+
tenantIdInToken: "mockM365Tenant",
1811+
tenantUserName: "mockM365UserName",
1812+
})
1813+
);
1814+
sandbox.stub(provisionUtils, "askForProvisionConsentV3").resolves(ok(undefined));
1815+
sandbox
1816+
.stub(provisionUtils, "ensureM365TenantMatchesV3")
1817+
.returns(err(new UserError("coordinator", "checkM365TenantError", "msg", "msg")));
1818+
sandbox.stub(tools.tokenProvider.azureAccountProvider, "getSelectedSubscription").resolves({
1819+
subscriptionId: "mockSubId",
1820+
tenantId: "mockTenantId",
1821+
subscriptionName: "mockSubName",
1822+
});
1823+
sandbox.stub(tools.tokenProvider.azureAccountProvider, "setSubscription").resolves();
1824+
sandbox.stub(tools.tokenProvider.m365TokenProvider, "signout").resolves();
1825+
sandbox.stub(tools.ui, "selectOption").callsFake(async (config) => {
1826+
if (config.name === "env") {
1827+
return ok({ type: "success", result: "dev" });
1828+
} else {
1829+
return ok({ type: "success", result: "" });
1830+
}
1831+
});
1832+
sandbox.stub(tools.ui, "showMessage").resolves(undefined);
1833+
sandbox.stub(pathUtils, "getEnvFilePath").resolves(ok("."));
1834+
sandbox.stub(pathUtils, "getYmlFilePath").returns("m365agents.yml");
1835+
sandbox.stub(fs, "pathExistsSync").onFirstCall().returns(false).onSecondCall().returns(true);
1836+
const inputs: Inputs = {
1837+
platform: Platform.VSCode,
1838+
projectPath: ".",
1839+
ignoreLockByUT: true,
1840+
};
1841+
const fxCore = new FxCore(tools);
1842+
const res = await fxCore.provisionResources(inputs);
1843+
assert.isTrue(res.isErr());
1844+
if (res.isErr()) {
1845+
assert.equal(res.error.name, "UserCancel");
1846+
}
1847+
});
16851848
it("provision failed with no subscription permission", async () => {
16861849
const mockProjectModel: ProjectModel = {
16871850
version: "1.0.0",

0 commit comments

Comments
 (0)