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

Increase code coverage in botbuilder library - TeamsInfo class #2531

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion libraries/botbuilder/src/teamsInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class TeamsInfo {
}

if (!userId) {
throw new Error('The getMember operation needs a valid conversationId.');
throw new Error('The getMember operation needs a valid userId.');
}

const teamMember: ChannelAccount = await connectorClient.conversations.getConversationMember(conversationId, userId);
Expand Down
103 changes: 100 additions & 3 deletions libraries/botbuilder/tests/teamsInfo.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const assert = require('assert');
const nock = require('nock');
const { TurnContext, MessageFactory } = require('botbuilder-core');
const { TurnContext, MessageFactory, ActionTypes } = require('botbuilder-core');
const { BotFrameworkAdapter, TeamsInfo } = require('../');
const { MicrosoftAppCredentials, ConnectorClient } = require('botframework-connector');
const { Conversations } = require('botframework-connector/lib/connectorApi/operations');
const { stub } = require('sinon');

beforeEach(function (done) {
nock.cleanAll();
Expand Down Expand Up @@ -429,7 +432,17 @@ describe('TeamsInfo', () => {
assert(fetchExpectation.isDone());
assertMemberInfo(fetchedMember, member);
});
});

describe('getTeamMember()', () => {
it('should throw error when teamId is not present', async () => {
try {
const context = new TestContext({ type: ActionTypes.message });
await TeamsInfo.getTeamMember(context);
} catch(err) {
assert.strictEqual(err.message, 'This method is only valid within the scope of a MS Teams Team.');
}
});
});

describe('getTeamMembers()', () => {
Expand Down Expand Up @@ -567,6 +580,90 @@ describe('TeamsInfo', () => {
assert.strictEqual(err.message, 'The getMembers operation needs a valid conversationId.');
}
});

it(`getMemberInternal() should error if an invalid conversationId is passed in.`, async () => {
try {
const results = await TeamsInfo.getMemberInternal({}, undefined);
console.error(results)
throw new Error('should have thrown an error');
} catch (err) {
assert.strictEqual(err.message, 'The getMember operation needs a valid conversationId.');
}
});

it(`getMemberInternal() should error if an invalid userId is passed in.`, async () => {
try {
const results = await TeamsInfo.getMemberInternal({}, 'conversationId', undefined);
console.error(results)
throw new Error('should have thrown an error');
} catch (err) {
assert.strictEqual(err.message, 'The getMember operation needs a valid userId.');
}
});

it(`getPagedMembersInternal() should error if an invalid conversationId is passed in.`, async () => {
try {
const results = await TeamsInfo.getPagedMembersInternal({}, undefined, 'options');
console.error(results)
throw new Error('should have thrown an error');
} catch (err) {
assert.strictEqual(err.message, 'The getPagedMembers operation needs a valid conversationId.');
}
});

it(`getPagedMembersInternal() should call connectorClient.conversations.getConversationPagedMembers()`, async function () {
const members = [
{
"id": "29:User-One-Id",
"name": "User One",
"objectId": "User-One-Object-Id",
"givenName": "User",
"surname": "One",
"email": "[email protected]",
"userPrincipalName": "[email protected]",
"tenantId": "tenantId-Guid"
},
{
"id": "29:User-Two-Id",
"name": "User Two",
"objectId": "User-Two-Object-Id",
"givenName": "User",
"surname": "Two",
"email": "[email protected]",
"userPrincipalName": "[email protected]",
"tenantId": "tenantId-Guid"
}
];
const conversations = new Conversations({ id: 'convo1', id: 'convo2' });
const getPagedMembers = stub(conversations, 'getConversationPagedMembers');
getPagedMembers.returns({ continuationToken: 'token', members: members});
const connector = new ConnectorClient(new MicrosoftAppCredentials('', ''));
connector.conversations = conversations;

const result = await TeamsInfo.getPagedMembersInternal(connector, 'convo1');

assert.strictEqual(getPagedMembers.calledOnce, true, `should have called conversations.getConversationPagedMembers`);
});

it(`getTeamId() should error if an invalid context is passed in.`, async () => {
try {
const results = await TeamsInfo.getTeamId(undefined);
console.error(results)
throw new Error('should have thrown an error');
} catch (err) {
assert.strictEqual(err.message, 'Missing context parameter');
}
});

it(`getTeamId() should error if an invalid activity is passed in.`, async () => {
try {
const results = await TeamsInfo.getTeamId({ activity: undefined });
console.error(results)
throw new Error('should have thrown an error');
} catch (err) {
assert.strictEqual(err.message, 'Missing activity on context');
}
});
});
});

Expand Down Expand Up @@ -639,7 +736,7 @@ const groupChatActivity = {
"id": "tenantId-Guid"
}
}
}
};

const teamActivity = {
"text": "<at>Teams Bot</at> hi\n",
Expand Down Expand Up @@ -691,4 +788,4 @@ const teamActivity = {
"id": "tenantId-Guid"
}
}
}
};