Skip to content

Commit

Permalink
Add unit tests for botFrameworkHttpClient class (#2485)
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 9, 2020
1 parent dd4d5e6 commit fd3e25f
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion libraries/botbuilder/tests/botFrameworkHttpClient.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { strictEqual } = require('assert');
const { BotFrameworkHttpClient } = require('../');
const { AuthenticationConstants, SimpleCredentialProvider, MicrosoftAppCredentials } = require('botframework-connector');
const { AuthenticationConstants, GovernmentConstants, JwtTokenValidation, SimpleCredentialProvider, MicrosoftAppCredentials, AppCredentials } = require('botframework-connector');
const nock = require('nock');

class TestBotFrameworkHttpClient extends BotFrameworkHttpClient {
Expand Down Expand Up @@ -79,6 +79,52 @@ describe('BotFrameworkHttpClient', function() {
strictEqual(response.status, 200);
});

it('should fail to make call with wrong credentials', async () => {

nock('http://skillUrl')
.post('/api/good')
.reply(200, { id: 'some-id' });

const credentialProvider = new SimpleCredentialProvider('test-app-id', 'test-app-Secret');
const client = new TestBotFrameworkHttpClient(credentialProvider, 'channels');
try {
await client.postActivity('fromBotId', 'toBotId', 'http://skillUrl/api/good', 'serviceUrl', 'conversationId', { type: 'message', conversation: { } });
} catch(e) {
strictEqual(e.message, 'BotFrameworkHttpClient.postActivity(): Unable to get appCredentials to connect to the skill');
}
});

it('should fail to make call when no activity is provided', async () => {
nock('http://skillUrl')
.post('/api/good', (body) => body.recipient)
.reply(200, { id: 'some-id' });

const credentialProvider = new SimpleCredentialProvider('this-is-not-the-app-id-your-looking-for', '1');
const client = new TestBotFrameworkHttpClient(credentialProvider, 'channels');
const fromBotId = 'this-is-not-the-app-id-your-looking-for';
try {
await client.postActivity(fromBotId, 'toBotId', 'http://skillUrl/api/good', 'serviceUrl', 'conversationId');
} catch(e) {
strictEqual(e.message, 'BotFrameworkHttpClient.postActivity(): missing activity');
}
});

it('should fail to make call when activity.conversation is undefined', async () => {
nock('http://skillUrl')
.post('/api/good', (body) => body.recipient)
.reply(200, { id: 'some-id' });

const credentialProvider = new SimpleCredentialProvider('this-is-not-the-app-id-your-looking-for', '1');
const client = new TestBotFrameworkHttpClient(credentialProvider, 'channels');
const fromBotId = 'this-is-not-the-app-id-your-looking-for';
const activity = { type: 'message' };
try {
await client.postActivity(fromBotId, 'toBotId', 'http://skillUrl/api/good', 'serviceUrl', 'conversationId', activity);
} catch(e) {
strictEqual(e.message, 'BotFrameworkHttpClient.postActivity(): Activity must have a ConversationReference');
}
});

it('should add empty recipient if missing from activity', async () => {
nock('http://skillUrl')
.post('/api/good', (body) => body.recipient)
Expand Down Expand Up @@ -107,4 +153,20 @@ describe('BotFrameworkHttpClient', function() {
strictEqual(forwardedActivity.relatesTo, originalRelatesTo);
});
});

describe('BuildCredentials', () => {
it('should return credentials when channel service is goverment', async () => {
const credentialProvider = new SimpleCredentialProvider('', '');
const client = new BotFrameworkHttpClient(credentialProvider, 'https://botframework.azure.us');
const credentials = await client.buildCredentials('test-app-id', 'test-scope');
strictEqual(credentials.oAuthEndpoint, GovernmentConstants.ToChannelFromBotLoginUrl);
});

it('should return credentials when channel service different than goverment', async () => {
const credentialProvider = new SimpleCredentialProvider('', '');
const client = new BotFrameworkHttpClient(credentialProvider, 'channels');
const credentials = await client.buildCredentials('test-app-id', 'test-scope');
strictEqual(credentials.oAuthEndpoint, AuthenticationConstants.ToChannelFromBotLoginUrl);
});
});
});

0 comments on commit fd3e25f

Please sign in to comment.