Skip to content

Commit

Permalink
test(e2e): use graph apis to handle aad apps (#9900)
Browse files Browse the repository at this point in the history
* test(e2e): use graph apis to handle aad apps


---------

Co-authored-by: Kuojian Lu <[email protected]>
  • Loading branch information
KennethBWSong and kuojianlu authored Sep 14, 2023
1 parent 5bc02f5 commit e58796e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ describe("Debug V3 command-and-response template", () => {
chai.assert.isDefined(context.BOT_ID);
chai.assert.isNotEmpty(context.BOT_ID);
const aadApp = await getAadAppByClientId(context.BOT_ID);
chai.assert.equal(aadApp?.id, context.BOT_ID);
chai.assert.isDefined(aadApp);
chai.assert.equal(aadApp?.appId, context.BOT_ID);
const bot = await getBot(context.BOT_ID);
chai.assert.equal(bot?.botId, context.BOT_ID);
chai.assert.equal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ describe("Debug V3 notification-http-trigger template", () => {
chai.assert.isDefined(context.BOT_ID);
chai.assert.isNotEmpty(context.BOT_ID);
const aadApp = await getAadAppByClientId(context.BOT_ID);
chai.assert.equal(aadApp?.id, context.BOT_ID);
chai.assert.isDefined(aadApp);
chai.assert.equal(aadApp?.appId, context.BOT_ID);
const bot = await getBot(context.BOT_ID);
chai.assert.equal(bot?.botId, context.BOT_ID);
chai.assert.equal(
Expand Down
36 changes: 28 additions & 8 deletions packages/tests/src/e2e/debug/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import m365Provider from "@microsoft/teamsfx-cli/src/commonlib/m365LoginUserPassword";
import { AppStudioScopes } from "@microsoft/teamsfx-core";
import { AppStudioScopes, GraphScopes } from "@microsoft/teamsfx-core";
import axios, { AxiosInstance } from "axios";

async function createRequester(): Promise<AxiosInstance> {
Expand All @@ -21,11 +21,27 @@ async function createRequester(): Promise<AxiosInstance> {
return requester;
}

async function createGraphRequester(): Promise<AxiosInstance> {
const appStudioTokenRes = await m365Provider.getAccessToken({
scopes: GraphScopes,
});
const appStudioToken = appStudioTokenRes.isOk()
? appStudioTokenRes.value
: undefined;
const requester = axios.create({
baseURL: "https://graph.microsoft.com/v1.0",
});
requester.defaults.headers.common[
"Authorization"
] = `Bearer ${appStudioToken}`;
return requester;
}

export async function deleteAadAppByObjectId(objectId: string) {
const requester = await createRequester();
const requester = await createGraphRequester();
for (let retries = 3; retries > 0; --retries) {
try {
const response = await requester.delete(`api/aadapp/v2/${objectId}`);
const response = await requester.delete(`/applications/${objectId}`);
if (response.status >= 200 && response.status < 300) {
console.log("Successfully deleted AAD app");
return;
Expand All @@ -37,12 +53,16 @@ export async function deleteAadAppByObjectId(objectId: string) {
}

export async function getAadAppByClientId(clientId: string): Promise<any> {
const requester = await createRequester();
const requester = await createGraphRequester();
for (let retries = 3; retries > 0; --retries) {
try {
const response = await requester.get(`api/aadapp/${clientId}`);
const response = await requester.get(
`/applications(appId='${clientId}')`
);
if (response.status >= 200 && response.status < 300) {
console.log("Successfully got AAD app");
console.log(
`Successfully got AAD app ${response.data.id} with client id ${clientId}`
);
return response.data;
}
} catch (e) {
Expand All @@ -54,8 +74,8 @@ export async function getAadAppByClientId(clientId: string): Promise<any> {

export async function deleteAadAppByClientId(clientId: string) {
const aadApp = await getAadAppByClientId(clientId);
if (aadApp?.objectId) {
await deleteAadAppByObjectId(aadApp.objectId);
if (aadApp?.id) {
await deleteAadAppByObjectId(aadApp.id);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/tests/src/e2e/m365/DebugLinkUnfurling.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe("Debug link unfurling template", () => {
chai.assert.isDefined(context.BOT_ID);
chai.assert.isNotEmpty(context.BOT_ID);
const aadApp = await getAadAppByClientId(context.BOT_ID);
chai.assert.equal(aadApp?.id, context.BOT_ID);
chai.assert.isDefined(aadApp);
chai.assert.equal(aadApp?.appId, context.BOT_ID);
const bot = await getBot(context.BOT_ID);
chai.assert.equal(bot?.botId, context.BOT_ID);
chai.assert.equal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe("Debug V3 m365-message-extension template", () => {
chai.assert.isDefined(context.BOT_ID);
chai.assert.isNotEmpty(context.BOT_ID);
const aadApp = await getAadAppByClientId(context.BOT_ID);
chai.assert.equal(aadApp?.id, context.BOT_ID);
chai.assert.isDefined(aadApp);
chai.assert.equal(aadApp?.appId, context.BOT_ID);
const bot = await getBot(context.BOT_ID);
chai.assert.equal(bot?.botId, context.BOT_ID);
chai.assert.equal(
Expand Down
3 changes: 2 additions & 1 deletion packages/tests/src/e2e/m365/DeployLinkUnfurling.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ describe("Deploy Link Unfurling template", () => {
chai.assert.isDefined(context.BOT_ID);
chai.assert.isNotEmpty(context.BOT_ID);
const aadApp = await getAadAppByClientId(context.BOT_ID);
chai.assert.equal(aadApp?.id, context.BOT_ID);
chai.assert.isDefined(aadApp);
chai.assert.equal(aadApp?.appId, context.BOT_ID);

// validate m365
chai.assert.isDefined(context.M365_TITLE_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ describe("Deploy V3 m365-message-extension template", () => {
chai.assert.isDefined(context.BOT_ID);
chai.assert.isNotEmpty(context.BOT_ID);
const aadApp = await getAadAppByClientId(context.BOT_ID);
chai.assert.equal(aadApp?.id, context.BOT_ID);
chai.assert.isDefined(aadApp);
chai.assert.equal(aadApp?.appId, context.BOT_ID);

// validate m365
chai.assert.isDefined(context.M365_TITLE_ID);
Expand Down

0 comments on commit e58796e

Please sign in to comment.