Skip to content

Commit fd3e25f

Browse files
Add unit tests for botFrameworkHttpClient class (#2485)
Co-authored-by: Cecilia Avila <[email protected]> Co-authored-by: Cecilia Avila <[email protected]>
1 parent dd4d5e6 commit fd3e25f

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

libraries/botbuilder/tests/botFrameworkHttpClient.test.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { strictEqual } = require('assert');
22
const { BotFrameworkHttpClient } = require('../');
3-
const { AuthenticationConstants, SimpleCredentialProvider, MicrosoftAppCredentials } = require('botframework-connector');
3+
const { AuthenticationConstants, GovernmentConstants, JwtTokenValidation, SimpleCredentialProvider, MicrosoftAppCredentials, AppCredentials } = require('botframework-connector');
44
const nock = require('nock');
55

66
class TestBotFrameworkHttpClient extends BotFrameworkHttpClient {
@@ -79,6 +79,52 @@ describe('BotFrameworkHttpClient', function() {
7979
strictEqual(response.status, 200);
8080
});
8181

82+
it('should fail to make call with wrong credentials', async () => {
83+
84+
nock('http://skillUrl')
85+
.post('/api/good')
86+
.reply(200, { id: 'some-id' });
87+
88+
const credentialProvider = new SimpleCredentialProvider('test-app-id', 'test-app-Secret');
89+
const client = new TestBotFrameworkHttpClient(credentialProvider, 'channels');
90+
try {
91+
await client.postActivity('fromBotId', 'toBotId', 'http://skillUrl/api/good', 'serviceUrl', 'conversationId', { type: 'message', conversation: { } });
92+
} catch(e) {
93+
strictEqual(e.message, 'BotFrameworkHttpClient.postActivity(): Unable to get appCredentials to connect to the skill');
94+
}
95+
});
96+
97+
it('should fail to make call when no activity is provided', async () => {
98+
nock('http://skillUrl')
99+
.post('/api/good', (body) => body.recipient)
100+
.reply(200, { id: 'some-id' });
101+
102+
const credentialProvider = new SimpleCredentialProvider('this-is-not-the-app-id-your-looking-for', '1');
103+
const client = new TestBotFrameworkHttpClient(credentialProvider, 'channels');
104+
const fromBotId = 'this-is-not-the-app-id-your-looking-for';
105+
try {
106+
await client.postActivity(fromBotId, 'toBotId', 'http://skillUrl/api/good', 'serviceUrl', 'conversationId');
107+
} catch(e) {
108+
strictEqual(e.message, 'BotFrameworkHttpClient.postActivity(): missing activity');
109+
}
110+
});
111+
112+
it('should fail to make call when activity.conversation is undefined', async () => {
113+
nock('http://skillUrl')
114+
.post('/api/good', (body) => body.recipient)
115+
.reply(200, { id: 'some-id' });
116+
117+
const credentialProvider = new SimpleCredentialProvider('this-is-not-the-app-id-your-looking-for', '1');
118+
const client = new TestBotFrameworkHttpClient(credentialProvider, 'channels');
119+
const fromBotId = 'this-is-not-the-app-id-your-looking-for';
120+
const activity = { type: 'message' };
121+
try {
122+
await client.postActivity(fromBotId, 'toBotId', 'http://skillUrl/api/good', 'serviceUrl', 'conversationId', activity);
123+
} catch(e) {
124+
strictEqual(e.message, 'BotFrameworkHttpClient.postActivity(): Activity must have a ConversationReference');
125+
}
126+
});
127+
82128
it('should add empty recipient if missing from activity', async () => {
83129
nock('http://skillUrl')
84130
.post('/api/good', (body) => body.recipient)
@@ -107,4 +153,20 @@ describe('BotFrameworkHttpClient', function() {
107153
strictEqual(forwardedActivity.relatesTo, originalRelatesTo);
108154
});
109155
});
156+
157+
describe('BuildCredentials', () => {
158+
it('should return credentials when channel service is goverment', async () => {
159+
const credentialProvider = new SimpleCredentialProvider('', '');
160+
const client = new BotFrameworkHttpClient(credentialProvider, 'https://botframework.azure.us');
161+
const credentials = await client.buildCredentials('test-app-id', 'test-scope');
162+
strictEqual(credentials.oAuthEndpoint, GovernmentConstants.ToChannelFromBotLoginUrl);
163+
});
164+
165+
it('should return credentials when channel service different than goverment', async () => {
166+
const credentialProvider = new SimpleCredentialProvider('', '');
167+
const client = new BotFrameworkHttpClient(credentialProvider, 'channels');
168+
const credentials = await client.buildCredentials('test-app-id', 'test-scope');
169+
strictEqual(credentials.oAuthEndpoint, AuthenticationConstants.ToChannelFromBotLoginUrl);
170+
});
171+
});
110172
});

0 commit comments

Comments
 (0)