Skip to content

Commit

Permalink
update TeamsActivityHandler, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengum committed Oct 9, 2019
1 parent 16ed50f commit 205180e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 96 deletions.
4 changes: 0 additions & 4 deletions libraries/botbuilder/src/teamsActivityHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ export class TeamsActivityHandler extends ActivityHandler {
* @param context
*/
protected async onTurnActivity(context: TurnContext): Promise<void> {
if (context.activity.channelId === 'msteams') {
// this.teamsRosterClient =
}

switch (context.activity.type) {
case ActivityTypes.Invoke:
const invokeResponse = await this.onInvokeActivity(context);
Expand Down
180 changes: 88 additions & 92 deletions libraries/botbuilder/tests/teamsActivityHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ function createInvokeActivity(name, value = {}, channelData = {}) {
}

describe('TeamsActivityHandler', () => {
it('should call onTurnActivity if non-Invoke is received', async () => {
const bot = new TeamsActivityHandler();
bot.onMessage(async (context, next) => {
await context.sendActivity('Hello');
await next();
});

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

adapter.send({ type: ActivityTypes.Message, text: 'Hello' })
.assertReply(activity => {
assert.strictEqual(activity.type, ActivityTypes.Message);
assert.strictEqual(activity.text, 'Hello');
});
});

describe('should send a BadRequest status code if', () => {
it('a bad BotMessagePreview.action is received by the bot', async () => {
const bot = new TeamsActivityHandler();
Expand Down Expand Up @@ -136,13 +154,20 @@ describe('TeamsActivityHandler', () => {
});
});

xdescribe('should send an OK status code when', () => {
it('a "fileConsent/invoke" activity is handled by an onTeamsFileConsentAccept handler', async () => {
const bot = new TeamsActivityHandler();
bot.onTeamsFileConsentAccept(async (context, fileConsentCardResponse) => {
describe('should send an OK status code when', () => {
class OKFileConsent extends TeamsActivityHandler {
async onTeamsFileConsentAccept(context, fileConsentCardResponse) {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
});
}

async onTeamsFileConsentDecline(context, fileConsentCardResponse) {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
}
}
it('a "fileConsent/invoke" activity is handled by onTeamsFileConsentAccept', async () => {
const bot = new OKFileConsent();

const adapter = new TestAdapter(async context => {
await bot.run(context);
Expand All @@ -158,17 +183,13 @@ describe('TeamsActivityHandler', () => {
});
});

it('a "fileConsent/invoke" activity is handled by an onTeamsFileConsentAccept handler', async () => {
const bot = new TeamsActivityHandler();
bot.onTeamsFileConsentDecline(async (context, fileConsentCardResponse) => {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
});

it('a "fileConsent/invoke" activity is handled by onTeamsFileConsentDecline', async () => {
const bot = new OKFileConsent();

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

const fileConsentActivity = createInvokeActivity('fileConsent/invoke', { action: 'decline' });
adapter.send(fileConsentActivity)
.assertReply(activity => {
Expand All @@ -178,72 +199,78 @@ describe('TeamsActivityHandler', () => {
`expected empty body for invokeResponse from fileConsent flow.\nReceived: ${JSON.stringify(activity.value.body)}`);
});
});
});

xdescribe('should dispatch through a registered', () => {
it('onTeamsFileConsent handler before an onTeamsFileConsentAccept handler', async () => {
const bot = new TeamsActivityHandler();

let fileConsentCalled = false;
let fileConsentAcceptCalled = false;

bot.onTeamsFileConsent(async (context, fileConsentCardResponse) => {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
fileConsentCalled = true;
});
bot.onTeamsFileConsentAccept(async (context, fileConsentCardResponse) => {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
assert(fileConsentCalled, 'onTeamsFileConsent handler was not called before onTeamsFileConsentAccept handler');
fileConsentAcceptCalled = true;
});
assert(bot.handlers['TeamsFileConsent'].length === 1);
assert(bot.handlers['TeamsFileConsentAccept'].length === 1);

it('a "fileConsent/invoke" activity handled by onTeamsFileConsent', async () => {
class FileConsent extends TeamsActivityHandler {
async onTeamsFileConsent(context, fileConsentCardResponse) {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
}
}
const bot = new FileConsent();

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

const fileConsentActivity = createInvokeActivity('fileConsent/invoke', { action: 'accept' });
adapter.send(fileConsentActivity)
.assertReply(activity => {
assert(activity.type === 'invokeResponse', `incorrect activity type "${activity.type}", expected "invokeResponse"`);
assert(activity.value.status === 200, `incorrect status code "${activity.value.status}", expected "200"`);
assert(!activity.value.body,
`expected empty body for invokeResponse from fileConsent flow.\nReceived: ${JSON.stringify(activity.value.body)}`);
}).then(() => {
assert(fileConsentCalled, 'onTeamsFileConsent handler not called');
assert(fileConsentAcceptCalled, 'onTeamsFileConsentAccept handler not called');

});
});
});

it('onTeamsFileConsent handler before an onTeamsFileConsentDecline handler', async () => {
const bot = new TeamsActivityHandler();

let fileConsentCalled = false;
let fileConsentDeclineCalled = false;

bot.onTeamsFileConsent(async (context, fileConsentCardResponse) => {
describe('should dispatch through a registered', () => {
let fileConsentAcceptCalled = false;
let fileConsentDeclineCalled = false;
let fileConsentCalled = false;

class FileConsentBot extends TeamsActivityHandler {
async onTeamsFileConsent(context, fileConsentCardResponse) {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
fileConsentCalled = true;
});
bot.onTeamsFileConsentDecline(async (context, fileConsentCardResponse, next) => {
await super.onTeamsFileConsent(context, fileConsentCardResponse);
}

async onTeamsFileConsentAccept(context, fileConsentCardResponse) {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
assert(fileConsentCalled, 'onTeamsFileConsent handler was not called before onTeamsFileConsentAccept handler');
fileConsentAcceptCalled = true;
}

async onTeamsFileConsentDecline(context, fileConsentCardResponse) {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
assert(fileConsentCalled, 'onTeamsFileConsent handler was not called before onTeamsFileConsentDecline handler');
fileConsentDeclineCalled = true;
});
assert(bot.handlers['TeamsFileConsent'].length === 1);
assert(bot.handlers['TeamsFileConsentDecline'].length === 1);

}
}

beforeEach(() => {
fileConsentAcceptCalled = false;
fileConsentDeclineCalled = false;
fileConsentCalled = false;
});

afterEach(() => {
fileConsentAcceptCalled = false;
fileConsentDeclineCalled = false;
fileConsentCalled = false;
});

it('onTeamsFileConsent handler before an onTeamsFileConsentAccept handler', async () => {
const bot = new FileConsentBot();
const adapter = new TestAdapter(async context => {
await bot.run(context);
});

const fileConsentActivity = createInvokeActivity('fileConsent/invoke', { action: 'decline' });
const fileConsentActivity = createInvokeActivity('fileConsent/invoke', { action: 'accept' });
adapter.send(fileConsentActivity)
.assertReply(activity => {
assert(activity.type === 'invokeResponse', `incorrect activity type "${activity.type}", expected "invokeResponse"`);
Expand All @@ -252,58 +279,27 @@ describe('TeamsActivityHandler', () => {
`expected empty body for invokeResponse from fileConsent flow.\nReceived: ${JSON.stringify(activity.value.body)}`);
}).then(() => {
assert(fileConsentCalled, 'onTeamsFileConsent handler not called');
assert(fileConsentDeclineCalled, 'onTeamsFileConsentDecline handler not called');

assert(fileConsentAcceptCalled, 'onTeamsFileConsentAccept handler not called');
});
});

it('onTeamsFileConsent handler and send a NotImplemented if no onTeamsFileConsentAccept handler is registered', async () => {
const bot = new TeamsActivityHandler();
let fileConsentCalled = false;

bot.onTeamsFileConsent(async (context, fileConsentCardResponse) => {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
fileConsentCalled = true;
});
it('onTeamsFileConsent handler before an onTeamsFileConsentDecline handler', async () => {
const bot = new FileConsentBot();
const adapter = new TestAdapter(async context => {
await bot.run(context);
});

const fileConsentActivity = createInvokeActivity('fileConsent/invoke', { action: 'decline' });
adapter.send(fileConsentActivity)
.assertReply(activity => {
assert(activity.type === 'invokeResponse', `incorrect activity type "${ activity.type }", expected "invokeResponse"`);
assert(activity.value.status === 501, `incorrect status code "${ activity.value.status }", expected "501"`);
assert(activity.type === 'invokeResponse', `incorrect activity type "${activity.type}", expected "invokeResponse"`);
assert(activity.value.status === 200, `incorrect status code "${activity.value.status}", expected "200"`);
assert(!activity.value.body,
`expected empty body for invokeResponse from fileConsent flow.\nReceived: ${ JSON.stringify(activity.value.body) }`);
`expected empty body for invokeResponse from fileConsent flow.\nReceived: ${JSON.stringify(activity.value.body)}`);
}).then(() => {
assert(fileConsentCalled, 'onTeamsFileConsent handler not called');
});
});

it('onTeamsFileConsent handler and send a NotImplemented if no onTeamsFileConsentDecline handler is registered', async () => {
const bot = new TeamsActivityHandler();
let fileConsentCalled = false;

bot.onTeamsFileConsent(async (context, fileConsentCardResponse) => {
assert(context, 'context not found');
assert(fileConsentCardResponse, 'fileConsentCardResponse not found');
fileConsentCalled = true;
});
const adapter = new TestAdapter(async context => {
await bot.run(context);
});
assert(fileConsentDeclineCalled, 'onTeamsFileConsentDecline handler not called');

const fileConsentActivity = createInvokeActivity('fileConsent/invoke', { action: 'decline' });
adapter.send(fileConsentActivity)
.assertReply(activity => {
assert(activity.type === 'invokeResponse', `incorrect activity type "${ activity.type }", expected "invokeResponse"`);
assert(activity.value.status === 501, `incorrect status code "${ activity.value.status }", expected "501"`);
assert(!activity.value.body,
`expected empty body for invokeResponse from fileConsent flow.\nReceived: ${ JSON.stringify(activity.value.body) }`);
}).then(() => {
assert(fileConsentCalled, 'onTeamsFileConsent handler not called');
});
});
});
Expand Down

0 comments on commit 205180e

Please sign in to comment.