Skip to content

Commit

Permalink
fix: enable TeamsInfo methods to work with CloudAdapter (#3830)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrichardson authored and Josh Gummersall committed Jun 30, 2021
1 parent 741ffee commit a4d32be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
8 changes: 6 additions & 2 deletions libraries/botbuilder/src/teamsInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectorClient>(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;
}

/**
Expand Down
33 changes: 27 additions & 6 deletions libraries/botbuilder/tests/teamsInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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'
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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) => {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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);
});
});

Expand Down

0 comments on commit a4d32be

Please sign in to comment.