-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathchannelServiceHandler.test.js
263 lines (212 loc) · 10.3 KB
/
channelServiceHandler.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
const assert = require('assert');
const sinon = require('sinon');
const { ActivityTypes, StatusCodes } = require('botbuilder-core');
const { ChannelServiceHandler } = require('../');
const isEmpty = require('lodash/isEmpty');
const {
AuthenticationConfiguration,
AuthenticationConstants,
ClaimsIdentity,
JwtTokenValidation,
SimpleCredentialProvider,
} = require('botframework-connector');
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) {
assert(authHeader, 'authHeader not received');
assert(conversationId, 'conversationId not received');
assert(activity, 'activity not received');
return await super.handleSendToConversation(authHeader, conversationId, activity);
}
// Override the private authenticate method to bypass auth.
async authenticate(authHeader) {
assert.strictEqual(authHeader, AUTH_HEADER);
return new ClaimsIdentity([]);
}
}
const matchStatusCodeError = (methodName) => ({
name: 'StatusCodeError',
message: `ChannelServiceHandler.${methodName}(): 501: Not Implemented`,
});
describe('ChannelServiceHandler', function () {
const handler = new ChannelServiceHandler(CREDENTIALS, AUTH_CONFIG, 'channels');
let sandbox;
beforeEach(function () {
sandbox = sinon.createSandbox();
});
afterEach(function () {
sandbox.restore();
});
describe('constructor', function () {
it('should succeed with valid parameters', function () {
const channelService = 'channels';
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG, channelService);
assert.strictEqual(handler.authConfig, AUTH_CONFIG);
assert.strictEqual(handler.credentialProvider, CREDENTIALS);
assert.strictEqual(handler.channelService, channelService);
});
it('should use process.env.ChannelService if no channelService is provided', function () {
process.env[AuthenticationConstants.ChannelService] = 'test';
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
assert.strictEqual(handler.authConfig, AUTH_CONFIG);
assert.strictEqual(handler.credentialProvider, CREDENTIALS);
assert.strictEqual(handler.channelService, 'test');
delete process.env[AuthenticationConstants.ChannelService];
});
it('should fail with invalid credentialProvider or authConfig', function () {
assert.throws(() => new NoAuthHandler(), {
message: 'ChannelServiceHandler(): missing credentialProvider',
});
assert.throws(() => new NoAuthHandler(CREDENTIALS), {
message: 'ChannelServiceHandler(): missing authConfig',
});
});
});
describe('SendToConversation flow:', function () {
it('handleSendToConversation should call onSendToConversation', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleSendToConversation(AUTH_HEADER, 'convId', { type: ActivityTypes.Message }),
matchStatusCodeError('onSendToConversation'),
);
});
});
describe('ReplyToActivity flow:', function () {
it('handleReplyToActivity should call onReplyToActivity', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleReplyToActivity(AUTH_HEADER, 'convId', ACTIVITY.id, ACTIVITY),
matchStatusCodeError('onReplyToActivity'),
);
});
});
describe('UpdateActivity flow:', function () {
it('handleUpdateActivity should call onUpdateActivity', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleUpdateActivity(AUTH_HEADER, 'convId', ACTIVITY.id, ACTIVITY),
matchStatusCodeError('onUpdateActivity'),
);
});
});
describe('DeleteActivity flow:', function () {
it('handleDeleteActivity should call onDeleteActivity', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleDeleteActivity(AUTH_HEADER, 'convId', ACTIVITY.id, ACTIVITY),
matchStatusCodeError('onDeleteActivity'),
);
});
});
describe('GetActivityMembers flow:', function () {
it('handleGetActivityMembers should call onGetActivityMembers', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleGetActivityMembers(AUTH_HEADER, 'convId', ACTIVITY.id),
matchStatusCodeError('onGetActivityMembers'),
);
});
});
describe('CreateConversation flow:', function () {
it('handleCreateConversation should call onCreateConversation', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleCreateConversation(AUTH_HEADER, { isGroup: false }),
matchStatusCodeError('onCreateConversation'),
);
});
});
describe('GetConversations flow:', function () {
it('handleGetConversations should call onGetConversations', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleGetConversations(AUTH_HEADER, 'convId'),
matchStatusCodeError('onGetConversations'),
);
});
});
describe('ConversationMembers flow:', function () {
it('handleGetConversationMembers should call onGetConversationMembers', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleGetConversationMembers(AUTH_HEADER, 'convId'),
matchStatusCodeError('onGetConversationMembers'),
);
});
it('handleGetConversationPagedMembers should call onGetConversationPagedMembers', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleGetConversationPagedMembers(AUTH_HEADER, 'convId'),
matchStatusCodeError('onGetConversationPagedMembers'),
);
});
it('handleDeleteConversationMember should call onDeleteConversationMember', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleDeleteConversationMember(AUTH_HEADER, 'convId', 'memberId'),
matchStatusCodeError('onDeleteConversationMember'),
);
});
});
describe('GetSendConversationHistory flow:', function () {
it('handleSendConversationHistory should call onSendConversationHistory', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleSendConversationHistory(AUTH_HEADER, 'convId', { ACTIVITY }),
matchStatusCodeError('onSendConversationHistory'),
);
});
});
describe('GetUploadAttachment flow:', function () {
it('handleUploadAttachment should call onUploadAttachment', async function () {
const handler = new NoAuthHandler(CREDENTIALS, AUTH_CONFIG);
await assert.rejects(
handler.handleUploadAttachment(AUTH_HEADER, 'convId', { type: 'string', name: 'attachment' }),
matchStatusCodeError('onUploadAttachment'),
);
});
});
describe('Authentication flow:', function () {
describe('authenticate() with no auth header', function () {
const mockAuthDisabled = (isDisabled) =>
sandbox
.mock(handler.credentialProvider)
.expects('isAuthenticationDisabled')
.once()
.returns(Promise.resolve(isDisabled));
it('should return a skill claim when auth is disabled', async function () {
mockAuthDisabled(true);
const result = await handler.authenticate();
assert(result.isAuthenticated, 'isAuthenticated is true');
assert(!isEmpty(result.claims), 'result.claims is not empty');
const appIdClaim = result.claims.find((claim) => claim.type === AuthenticationConstants.AppIdClaim);
assert(appIdClaim, 'app ID claim is set');
assert.strictEqual(appIdClaim.value, AuthenticationConstants.AnonymousSkillAppId);
sandbox.verify();
});
it('should throw an error when auth is enabled', async function () {
mockAuthDisabled(false);
await assert.rejects(handler.authenticate(), { statusCode: StatusCodes.UNAUTHORIZED });
sandbox.verify();
});
});
describe('authenticate() with an auth header', function () {
it('should return a valid claim identity', async function () {
sandbox
.mock(JwtTokenValidation)
.expects('validateAuthHeader')
.once()
.returns(new ClaimsIdentity([], AuthenticationConstants.AnonymousAuthType));
const identity = await handler.authenticate(AUTH_HEADER);
assert(identity.isAuthenticated, 'isAuthenticated is true');
sandbox.verify();
});
it('should throw an UNAUTHORIZED error for a bad auth header', async function () {
await assert.rejects(handler.authenticate(AUTH_HEADER), { statusCode: StatusCodes.UNAUTHORIZED });
});
});
});
});