-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathcloudAdapter.test.js
295 lines (245 loc) · 11.7 KB
/
cloudAdapter.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
const assert = require('assert');
const httpMocks = require('node-mocks-http');
const net = require('net');
const { expect } = require('chai');
const sinon = require('sinon');
const {
AuthenticationConfiguration,
AuthenticationConstants,
BotFrameworkAuthenticationFactory,
allowedCallersClaimsValidator,
} = require('botframework-connector');
const {
CloudAdapter,
ConfigurationBotFrameworkAuthentication,
ConfigurationServiceClientCredentialFactory,
ActivityTypes,
createBotFrameworkAuthenticationFromConfiguration,
INVOKE_RESPONSE_KEY,
} = require('..');
const { NamedPipeServer } = require('botframework-streaming');
const { StatusCodes } = require('botframework-schema');
const { CallerIdConstants } = require('../../botbuilder-core/lib/index');
const FakeBuffer = () => Buffer.from([]);
const FakeNodeSocket = () => new net.Socket();
const noop = () => null;
describe('CloudAdapter', function () {
let sandbox;
beforeEach(function () {
sandbox = sinon.createSandbox({ useFakeTimers: false });
});
afterEach(function () {
if (sandbox) {
sandbox.restore();
}
});
const adapter = new CloudAdapter();
describe('constructor', function () {
it('throws for bad args', function () {
assert.throws(() => new CloudAdapter(null), {
name: 'TypeError',
message: '`botFrameworkAuthentication` parameter required',
});
});
it('succeeds', function () {
new CloudAdapter(BotFrameworkAuthenticationFactory.create());
new CloudAdapter();
});
});
describe('process', function () {
class TestConfiguration {
static DefaultConfig = {
// [AuthenticationConstants.ChannelService]: undefined,
ValidateAuthority: true,
ToChannelFromBotLoginUrl: AuthenticationConstants.ToChannelFromBotLoginUrl,
ToChannelFromBotOAuthScope: AuthenticationConstants.ToChannelFromBotOAuthScope,
ToBotFromChannelTokenIssuer: AuthenticationConstants.ToBotFromChannelTokenIssuer,
ToBotFromEmulatorOpenIdMetadataUrl: AuthenticationConstants.ToBotFromEmulatorOpenIdMetadataUrl,
CallerId: CallerIdConstants.PublicAzureChannel,
ToBotFromChannelOpenIdMetadataUrl: AuthenticationConstants.ToBotFromChannelOpenIdMetadataUrl,
OAuthUrl: AuthenticationConstants.OAuthUrl,
// [AuthenticationConstants.OAuthUrlKey]: 'test',
[AuthenticationConstants.BotOpenIdMetadataKey]: null,
};
constructor(config = {}) {
this.configuration = Object.assign({}, TestConfiguration.DefaultConfig, config);
}
get(_path) {
return this.configuration;
}
set(_path, _val) { }
}
const activity = { type: ActivityTypes.Invoke, value: 'invoke' };
const authorization = 'Bearer Authorization';
it('delegates to connect', async function () {
const req = {};
const socket = FakeNodeSocket();
const head = FakeBuffer();
const logic = () => Promise.resolve();
const mock = sandbox.mock(adapter);
mock.expects('connect').withArgs(req, socket, head, logic).once().resolves();
mock.expects('processActivity').never();
await adapter.process(req, socket, head, logic);
mock.verify();
});
it('delegates to processActivity', async function () {
const req = httpMocks.createRequest({
method: 'POST',
headers: { authorization },
body: activity,
});
const res = httpMocks.createResponse();
const logic = async (context) => {
context.turnState.set(INVOKE_RESPONSE_KEY, {
type: ActivityTypes.InvokeResponse,
value: {
status: 200,
body: 'invokeResponse',
},
});
};
const mock = sandbox.mock(adapter);
mock.expects('processActivity').withArgs(authorization, activity, logic).once().resolves();
mock.expects('connect').never();
await adapter.process(req, res, logic);
mock.verify();
});
it('throws exception on expired token', async function () {
const consoleStub = sandbox.stub(console, 'error');
// Expired token with removed AppID
const authorization =
'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyIsImtpZCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyJ9.eyJhdWQiOiJodHRwczovL2FwaS5ib3RmcmFtZXdvcmsuY29tIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvZDZkNDk0MjAtZjM5Yi00ZGY3LWExZGMtZDU5YTkzNTg3MWRiLyIsImlhdCI6MTY5Mjg3MDMwMiwibmJmIjoxNjkyODcwMzAyLCJleHAiOjE2OTI5NTcwMDIsImFpbyI6IkUyRmdZUGhhdFZ6czVydGFFYTlWbDN2ZnIyQ2JBZ0E9IiwiYXBwaWQiOiIxNWYwMTZmZS00ODhjLTQwZTktOWNiZS00Yjk0OGY5OGUyMmMiLCJhcHBpZGFjciI6IjEiLCJpZHAiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC9kNmQ0OTQyMC1mMzliLTRkZjctYTFkYy1kNTlhOTM1ODcxZGIvIiwicmgiOiIwLkFXNEFJSlRVMXB2ejkwMmgzTldhazFoeDIwSXpMWTBwejFsSmxYY09EcS05RnJ4dUFBQS4iLCJ0aWQiOiJkNmQ0OTQyMC1mMzliLTRkZjctYTFkYy1kNTlhOTM1ODcxZGIiLCJ1dGkiOiJkenVwa1dWd2FVT2x1RldkbnlvLUFBIiwidmVyIjoiMS4wIn0.sbQH997Q2GDKiiYd6l5MIz_XNfXypJd6zLY9xjtvEgXMBB0x0Vu3fv9W0nM57_ZipQiZDTZuSQA5BE30KBBwU-ZVqQ7MgiTkmE9eF6Ngie_5HwSr9xMK3EiDghHiOP9pIj3oEwGOSyjR5L9n-7tLSdUbKVyV14nS8OQtoPd1LZfoZI3e7tVu3vx8Lx3KzudanXX8Vz7RKaYndj3RyRi4wEN5hV9ab40d7fQsUzygFd5n_PXC2rs0OhjZJzjCOTC0VLQEn1KwiTkSH1E-OSzkrMltn1sbhD2tv_H-4rqQd51vAEJ7esC76qQjz_pfDRLs6T2jvJyhd5MZrN_MT0TqlA';
const activity = { type: ActivityTypes.Invoke, value: 'invoke' };
const req = httpMocks.createRequest({
method: 'POST',
headers: { authorization },
body: activity,
});
const res = httpMocks.createResponse();
const logic = async (context) => {
context.turnState.set(INVOKE_RESPONSE_KEY, {
type: ActivityTypes.InvokeResponse,
value: {
status: 200,
body: 'invokeResponse',
},
});
};
const validTokenIssuers = [];
const claimsValidators = allowedCallersClaimsValidator(['*']);
const authConfig = new AuthenticationConfiguration([], claimsValidators, validTokenIssuers);
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: '',
MicrosoftAppPassword: '',
MicrosoftAppType: '',
MicrosoftAppTenantId: '',
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(
null,
credentialsFactory,
authConfig,
undefined,
undefined
);
const adapter = new CloudAdapter(botFrameworkAuthentication);
await adapter.process(req, res, logic);
assert.equal(StatusCodes.UNAUTHORIZED, res.statusCode);
expect(consoleStub.calledWithMatch({ message: 'The token has expired' })).to.be.true;
});
it('calls processActivityDirect with string authorization', async function () {
const logic = async (context) => {
context.turnState.set(INVOKE_RESPONSE_KEY, {
type: ActivityTypes.InvokeResponse,
value: {
status: 200,
body: 'invokeResponse',
},
});
};
const mock = sandbox.mock(adapter);
mock.expects('processActivity').withArgs(authorization, activity, logic).once().resolves();
mock.expects('connect').never();
await adapter.processActivityDirect(authorization, activity, logic);
mock.verify();
});
it('calls processActivityDirect with AuthenticateRequestResult authorization', async function () {
const claimsIdentity = adapter.createClaimsIdentity('appId');
const audience = AuthenticationConstants.ToChannelFromBotOAuthScope;
const botFrameworkAuthentication = new ConfigurationBotFrameworkAuthentication(
TestConfiguration.DefaultConfig
);
const connectorFactory = botFrameworkAuthentication.createConnectorFactory(claimsIdentity);
//AuthenticateRequestResult
const authentication = {
audience: audience,
claimsIdentity: claimsIdentity,
callerId: 'callerdId',
connectorFactory: connectorFactory,
};
const logic = async (context) => {
context.turnState.set(INVOKE_RESPONSE_KEY, {
type: ActivityTypes.InvokeResponse,
value: {
status: 200,
body: 'invokeResponse',
},
});
};
const mock = sandbox.mock(adapter);
mock.expects('processActivity').withArgs(authentication, activity, logic).once().resolves();
mock.expects('connect').never();
await adapter.processActivityDirect(authentication, activity, logic);
mock.verify();
});
it('calls processActivityDirect with error', async function () {
const logic = async (context) => {
context.turnState.set(INVOKE_RESPONSE_KEY, {
type: ActivityTypes.InvokeResponse,
value: {
status: 200,
body: 'invokeResponse',
},
});
};
sandbox.stub(adapter, 'processActivity').throws({ stack: 'error stack' });
await assert.rejects(
adapter.processActivityDirect(authorization, activity, logic),
new Error('CloudAdapter.processActivityDirect(): ERROR\n error stack')
);
});
});
describe('connectNamedPipe', function () {
it('throws for bad args', async function () {
const includesParam = (param) => (err) => {
assert(err.message.includes(`${param}`), err.message);
return true;
};
await assert.rejects(
adapter.connectNamedPipe(undefined, noop, 'appId', 'audience', 'callerId'),
includesParam('pipeName')
);
await assert.rejects(
adapter.connectNamedPipe('pipeName', undefined, 'appId', 'audience', 'callerId'),
includesParam('logic')
);
await assert.rejects(
adapter.connectNamedPipe('pipeName', noop, undefined, 'audience', 'callerId'),
includesParam('appId')
);
await assert.rejects(
adapter.connectNamedPipe('pipeName', noop, 'appId', undefined, 'callerId'),
includesParam('audience')
);
await assert.rejects(
adapter.connectNamedPipe('pipeName', noop, 'appId', 'audience', 10),
includesParam('callerId')
);
});
it('succeeds', async function () {
sandbox.stub(NamedPipeServer.prototype, 'start').resolves();
await adapter.connectNamedPipe('pipeName', noop, 'appId', 'audience');
});
});
});