Skip to content

Commit

Permalink
Add tests for channelServiceHandler class. (#2484)
Browse files Browse the repository at this point in the history
Co-authored-by: Cecilia Avila <[email protected]>
Co-authored-by: Cecilia Avila <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2020
1 parent d79b10b commit 39fdab4
Showing 1 changed file with 159 additions and 3 deletions.
162 changes: 159 additions & 3 deletions libraries/botbuilder/tests/channelServiceHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const { fail, ok: assert, strictEqual } = require('assert');
const { ActivityTypes } = require('botbuilder-core');
const { AuthenticationConfiguration, ClaimsIdentity, SimpleCredentialProvider } = require('botframework-connector');
const { ActivityTypes, StatusCodes } = require('botbuilder-core');
const { AuthenticationConfiguration, ClaimsIdentity, JwtTokenValidation, SimpleCredentialProvider } = require('botframework-connector');
const { ChannelServiceHandler } = require('../');
const { isEmpty } = require('lodash');
const { stub } = require('sinon');

const AUTH_HEADER = 'Bearer HelloWorld';
const AUTH_CONFIG = new AuthenticationConfiguration();
const CREDENTIALS = new SimpleCredentialProvider('', '');
const ACTIVITY = { id: 'testId', type: ActivityTypes.Message }

class NoAuthHandler extends ChannelServiceHandler {
async handleSendToConversation(authHeader, conversationId, activity) {
Expand Down Expand Up @@ -75,4 +78,157 @@ describe('ChannelServiceHandler', () => {
}
});
});
});

describe('ReplyToActivity flow:', () => {
it('handleReplyToActivity should call onReplyToActivity', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleReplyToActivity(AUTH_HEADER, 'convId', ACTIVITY.id, ACTIVITY);
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onReplyToActivity'));
}
});
});

describe('UpdateActivity flow:', () => {
it('handleUpdateActivity should call onUpdateActivity', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleUpdateActivity(AUTH_HEADER, 'convId', ACTIVITY.id, ACTIVITY);
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onUpdateActivity'));
}
});
});

describe('DeleteActivity flow:', () => {
it('handleDeleteActivity should call onDeleteActivity', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleDeleteActivity(AUTH_HEADER, 'convId', ACTIVITY.id, ACTIVITY);
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onDeleteActivity'));
}
});
});

describe('GetActivityMembers flow:', () => {
it('handleGetActivityMembers should call onGetActivityMembers', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleGetActivityMembers(AUTH_HEADER, 'convId', ACTIVITY.id);
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onGetActivityMembers'));
}
});
});

describe('CreateConversation flow:', () => {
it('handleCreateConversation should call onCreateConversation', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleCreateConversation(AUTH_HEADER, { isGroup: false });
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onCreateConversation'));
}
});
});

describe('GetConversations flow:', () => {
it('handleGetConversations should call onGetConversations', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleGetConversations(AUTH_HEADER, 'convId');
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onGetConversations'));
}
});
});

describe('ConversationMembers flow:', () => {
it('handleGetConversationMembers should call onGetConversationMembers', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleGetConversationMembers(AUTH_HEADER, 'convId');
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onGetConversationMembers'));
}
});

it('handleGetConversationPagedMembers should call onGetConversationPagedMembers', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleGetConversationPagedMembers(AUTH_HEADER, 'convId');
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onGetConversationPagedMembers'));
}
});

it('handleDeleteConversationMember should call onDeleteConversationMember', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleDeleteConversationMember(AUTH_HEADER, 'convId', 'memberId');
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onDeleteConversationMember'));
}
});
});

describe('GetSendConversationHistory flow:', () => {
it('handleSendConversationHistory should call onSendConversationHistory', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleSendConversationHistory(AUTH_HEADER, 'convId', { ACTIVITY });
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onSendConversationHistory'));
}
});
});

describe('GetUploadAttachment flow:', () => {
it('handleUploadAttachment should call onUploadAttachment', async () => {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
try {
await handler.handleUploadAttachment(AUTH_HEADER, 'convId', { type: 'string', name: 'attachment' });
} catch (e) {
strictEqual(e.message, createDefaultErrorMessage('onUploadAttachment'));
}
});
});

describe('Authentication flow:', () => {
it('authenticate should return an empty claim when no authHeader is provided', async () => {
const channelService = 'channels';
const handler = new ChannelServiceHandler(CREDENTIALS, AUTH_CONFIG, channelService);
const result = await handler.authenticate();

isEmpty(result.claims);
strictEqual(result.isAuthenticated, false);
});

it('authenticate should return a valid claim identity', async () => {
const channelService = 'channels';
const handler = new ChannelServiceHandler(CREDENTIALS, AUTH_CONFIG, channelService);
const authHeaderStub = stub(JwtTokenValidation, 'validateAuthHeader');
authHeaderStub.returns(new ClaimsIdentity([], true));

const identity = await handler.authenticate(AUTH_HEADER);
try {
assert(authHeaderStub.called, 'JwtTokenValidation.validateAuthHeader() not called');
assert.strictEqual(identity.isAuthenticated, true);
} finally {
authHeaderStub.restore();
}
});

it('authenticate should throw an UNAUTHORIZED error', async () => {
const channelService = 'channels';
const handler = new ChannelServiceHandler(CREDENTIALS, AUTH_CONFIG, channelService);
try {
await handler.authenticate(AUTH_HEADER);
} catch (e) {
strictEqual(e.statusCode, StatusCodes.UNAUTHORIZED)
}
});
});
});

0 comments on commit 39fdab4

Please sign in to comment.