Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do NOT call TeamsInfo.getMember for the bot #2237

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions libraries/botbuilder/src/teamsActivityHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,14 @@ export class TeamsActivityHandler extends ActivityHandler {
for (let i=0; i<context.activity.membersAdded.length; i++) {
const channelAccount = context.activity.membersAdded[i];

// check whether we have a TeamChannelAccount
// check whether we have a TeamChannelAccount, or the member is the bot
if ('givenName' in channelAccount ||
'surname' in channelAccount ||
'email' in channelAccount ||
'userPrincipalName' in channelAccount) {
'userPrincipalName' in channelAccount ||
context.activity.recipient.id === channelAccount.id) {

// we must have a TeamsChannelAccount so skip to the next one
// we must have a TeamsChannelAccount, or a bot so skip to the next one
continue;
}

Expand Down
72 changes: 69 additions & 3 deletions libraries/botbuilder/tests/teamsActivityHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const { TeamsActivityHandler } = require('../');
const { TeamsActivityHandler, TeamsInfo } = require('../');
const { ActivityTypes, TestAdapter } = require('botbuilder-core');

function createInvokeActivity(name, value = {}, channelData = {}) {
Expand Down Expand Up @@ -513,7 +513,8 @@ describe('TeamsActivityHandler', () => {
function createConvUpdateActivity(channelData) {
const activity = {
type: ActivityTypes.ConversationUpdate,
channelData
channelData,
channelId: 'msteams'
};
return activity;
}
Expand Down Expand Up @@ -543,7 +544,8 @@ describe('TeamsActivityHandler', () => {
it('onTeamsMembersAdded routed activity', () => {
const bot = new TeamsActivityHandler();
let onTeamsMemberAddedEvent = false;

let getMemberCalledCount = 0;

const membersAddedMock = [{ id: 'test'} , { id: 'id' }];
const team = { id: 'team' };
const activity = createConvUpdateActivity({ team, eventType: 'teamMemberAdded' });
Expand Down Expand Up @@ -578,11 +580,75 @@ describe('TeamsActivityHandler', () => {
await bot.run(context);
});

const wasGetMember = TeamsInfo.getMember;
TeamsInfo.getMember = function(context, userId) {
getMemberCalledCount ++;
return membersAddedMock.filter(obj=>obj.id === userId)[0];
};

adapter.send(activity)
.then(() => {
assert(onTeamsMemberAddedEvent, 'onTeamsMemberAddedEvent handler not called');
assert(onConversationUpdateCalled, 'handleTeamsFileConsentAccept handler not called');
assert(onDialogCalled, 'onDialog handler not called');
assert(getMemberCalledCount === 2, 'TeamsInfo.getMember not called twice');
TeamsInfo.getMember = wasGetMember;
});
});

it('onTeamsMembersAdded for bot routed activity does NOT call TeamsInfo.getMember', () => {
const bot = new TeamsActivityHandler();
let onTeamsMemberAddedEvent = false;
let getMemberCalled = false;

const membersAddedMock = [{ id: 'botid' }];
const team = { id: 'team' };
const activity = createConvUpdateActivity({ team, eventType: 'teamMemberAdded' });
activity.membersAdded = membersAddedMock;
activity.recipient = { id: membersAddedMock[0].id };

bot.onConversationUpdate(async (context, next) => {
assert(context, 'context not found');
assert(next, 'next not found');
onConversationUpdateCalled = true;
await next();
});

bot.onTeamsMembersAddedEvent(async (membersAdded, teamInfo, context, next) => {
assert(membersAdded, 'membersAdded not found');
assert(teamInfo, 'teamsInfo not found');
assert(context, 'context not found');
assert(next, 'next not found');
assert.strictEqual(teamInfo, team);
assert.strictEqual(membersAdded, membersAddedMock);
onTeamsMemberAddedEvent = true;
await next();
});

bot.onDialog(async (context, next) => {
assert(context, 'context not found');
assert(next, 'next not found');
onDialogCalled = true;
await next();
});

const adapter = new TestAdapter(async context => {
await bot.run(context);
});

const wasGetMember = TeamsInfo.getMember;
TeamsInfo.getMember = function(context, userId) {
getMemberCalled = true;
return membersAddedMock.filter(obj=>obj.id === userId)[0];
};

adapter.send(activity)
.then(() => {
assert(onTeamsMemberAddedEvent, 'onTeamsMemberAddedEvent handler not called');
assert(onConversationUpdateCalled, 'handleTeamsFileConsentAccept handler not called');
assert(onDialogCalled, 'onDialog handler not called');
assert(getMemberCalled === false, 'TeamsInfo.getMember was called, but should not have been');
TeamsInfo.getMember = wasGetMember;
});
});

Expand Down