From a4d32be2cd83f18bf0053bde60f5bf682a8812d3 Mon Sep 17 00:00:00 2001 From: Michael Richardson <40401643+mdrichardson@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:23:22 -0700 Subject: [PATCH] fix: enable TeamsInfo methods to work with CloudAdapter (#3830) --- libraries/botbuilder/src/teamsInfo.ts | 8 +++-- libraries/botbuilder/tests/teamsInfo.test.js | 33 ++++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/libraries/botbuilder/src/teamsInfo.ts b/libraries/botbuilder/src/teamsInfo.ts index dfd57157ca..835d8adb8b 100644 --- a/libraries/botbuilder/src/teamsInfo.ts +++ b/libraries/botbuilder/src/teamsInfo.ts @@ -414,11 +414,15 @@ export class TeamsInfo { * @private */ private static getConnectorClient(context: TurnContext): ConnectorClient { - if (!context.adapter || !('createConnectorClient' in context.adapter)) { + const client = + context.adapter && 'createConnectorClient' in context.adapter + ? (context.adapter as BotFrameworkAdapter).createConnectorClient(context.activity.serviceUrl) + : context.turnState?.get(context.adapter.ConnectorClientKey); + if (!client) { throw new Error('This method requires a connector client.'); } - return (context.adapter as BotFrameworkAdapter).createConnectorClient(context.activity.serviceUrl); + return client; } /** diff --git a/libraries/botbuilder/tests/teamsInfo.test.js b/libraries/botbuilder/tests/teamsInfo.test.js index 536ce5d789..4a73ffd061 100644 --- a/libraries/botbuilder/tests/teamsInfo.test.js +++ b/libraries/botbuilder/tests/teamsInfo.test.js @@ -3,7 +3,7 @@ const nock = require('nock'); const sinon = require('sinon'); const { BotFrameworkAdapter, TeamsInfo, CloudAdapter } = require('../'); const { Conversations } = require('botframework-connector/lib/connectorApi/operations'); -const { MicrosoftAppCredentials, ConnectorClient } = require('botframework-connector'); +const { MicrosoftAppCredentials, ConnectorClient, ServiceClientCredentialsFactory, PasswordServiceClientCredentialFactory } = require('botframework-connector'); const { TurnContext, MessageFactory, ActionTypes, BotAdapter, Channels } = require('botbuilder-core'); class TeamsInfoAdapter extends BotFrameworkAdapter { @@ -182,6 +182,10 @@ const teamActivity = { }; describe('TeamsInfo', function () { + const connectorClient = new ConnectorClient(new MicrosoftAppCredentials('abc', '123'), { + baseUri: 'https://smba.trafficmanager.net/amer/', + }); + beforeEach(function () { nock.cleanAll(); }); @@ -342,6 +346,7 @@ describe('TeamsInfo', function () { .reply(200, { conversations }); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const channels = await TeamsInfo.getTeamChannels(context); assert(fetchOauthToken.isDone()); @@ -381,6 +386,7 @@ describe('TeamsInfo', function () { .reply(200, { conversations }); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const channels = await TeamsInfo.getTeamChannels(context, '19:ChannelIdgeneralChannelId@thread.skype'); assert(fetchOauthToken.isDone()); @@ -431,6 +437,7 @@ describe('TeamsInfo', function () { .reply(200, teamDetails); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedTeamDetails = await TeamsInfo.getTeamDetails(context); assert(fetchOauthToken.isDone()); @@ -457,6 +464,7 @@ describe('TeamsInfo', function () { .reply(200, teamDetails); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedTeamDetails = await TeamsInfo.getTeamDetails( context, '19:ChannelIdgeneralChannelId@thread.skype' @@ -495,6 +503,7 @@ describe('TeamsInfo', function () { .reply(200, members); const context = new TestContext(oneOnOneActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedMembers = await TeamsInfo.getMembers(context); assert(fetchOauthToken.isDone()); @@ -548,6 +557,7 @@ describe('TeamsInfo', function () { .reply(200, members); const context = new TestContext(groupChatActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedMembers = await TeamsInfo.getMembers(context); assert(fetchOauthToken.isDone()); @@ -591,6 +601,7 @@ describe('TeamsInfo', function () { .reply(200, members); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedMembers = await TeamsInfo.getMembers(context); assert(fetchOauthToken.isDone()); @@ -604,6 +615,7 @@ describe('TeamsInfo', function () { it('should not work if conversationId is falsey', async function () { const context = new TestContext(oneOnOneActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); context.activity.conversation.id = undefined; await assert.rejects(TeamsInfo.getMembers(context), (err) => { @@ -635,6 +647,7 @@ describe('TeamsInfo', function () { .reply(200, member); const context = new TestContext(oneOnOneActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedMember = await TeamsInfo.getMember(context, oneOnOneActivity.from.id); assert(fetchOauthToken.isDone()); @@ -663,6 +676,7 @@ describe('TeamsInfo', function () { .reply(200, member); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedMember = await TeamsInfo.getMember(context, teamActivity.from.id); assert(fetchOauthToken.isDone()); @@ -685,6 +699,7 @@ describe('TeamsInfo', function () { describe('getMeetingParticipant', function () { const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); it('should work with correct arguments', async function () { const participant = { @@ -729,6 +744,7 @@ describe('TeamsInfo', function () { describe('getMeetingInfo', function () { const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); it('should work with correct arguments-meetingId in context', async function () { const details = { @@ -772,6 +788,9 @@ describe('TeamsInfo', function () { }); it('should work with correct arguments-meetingId passed in', async function () { + const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); + const details = { organizer: { id: teamActivity.from.id, @@ -872,6 +891,7 @@ describe('TeamsInfo', function () { .reply(200, members); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedMembers = await TeamsInfo.getTeamMembers(context); assert(fetchOauthToken.isDone()); @@ -915,6 +935,7 @@ describe('TeamsInfo', function () { .reply(200, members); const context = new TestContext(teamActivity); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); const fetchedMembers = await TeamsInfo.getTeamMembers(context, '19:ChannelIdgeneralChannelId@thread.skype'); assert(fetchOauthToken.isDone()); @@ -936,11 +957,11 @@ describe('TeamsInfo', function () { ); }); - it(`should error if the adapter doesn't have a createConnectorClient method`, function () { - assert.throws( - () => TeamsInfo.getConnectorClient({ adapter: {} }), - new Error('This method requires a connector client.') - ); + it(`should fallback to the connectorClient on turnState if adapter doesn't exist in context.adapter`, function () { + const context = new TurnContext({}); + context.turnState.set(context.adapter.ConnectorClientKey, connectorClient); + const result = TeamsInfo.getConnectorClient(context); + assert.strictEqual(result, connectorClient); }); });