@webex/plugin-attachment-actions 2.59.3-next.1 → 2.59.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,324 +1,324 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import '@webex/plugin-logger';
6
- import '@webex/plugin-people';
7
- import '@webex/plugin-messages';
8
- import '@webex/plugin-attachment-actions';
9
- import {SDK_EVENT} from '@webex/common';
10
- import {assert} from '@webex/test-helper-chai';
11
- import WebexCore from '@webex/webex-core';
12
- import testUsers from '@webex/test-helper-test-users';
13
-
14
- const debug = require('debug')('attachmentActions');
15
-
16
- describe('plugin-attachment-actions', function () {
17
- this.timeout(60000);
18
-
19
- let creator, spock;
20
- let messageSender, buttonPusher;
21
-
22
- before(() =>
23
- testUsers.create({count: 2}).then((users) => {
24
- [creator, spock] = users;
25
-
26
- // Creator will create the cards
27
- creator.webex = new WebexCore({
28
- credentials: {
29
- authorization: users[0].token,
30
- },
31
- });
32
-
33
- // Spock is the actor who will hit the Action.Submit button
34
- spock.webex = new WebexCore({
35
- credentials: {
36
- authorization: users[1].token,
37
- },
38
- });
39
- spock.webex.people
40
- .get('me')
41
- .then((person) => {
42
- buttonPusher = person;
43
-
44
- return creator.webex.people.get('me');
45
- })
46
- .then((person) => {
47
- messageSender = person;
48
- });
49
- })
50
- );
51
-
52
- // Stop listening for websocket events
53
- afterEach(() => {
54
- creator.webex.attachmentActions.stopListening();
55
- creator.webex.attachmentActions.off('created');
56
- });
57
-
58
- describe('#attachmentActions', () => {
59
- describe('#create()', () => {
60
- it('creates an attachment action and validates the attachment action created', () => {
61
- let message;
62
- const inputs = {
63
- formInput: 'test',
64
- };
65
-
66
- // "Block" this test with a promise that will
67
- // resolve after the attachmentAction:created arrives.
68
- const created = new Promise((resolve) => {
69
- creator.webex.attachmentActions.on('created', (event) => {
70
- debug('attachmentAction created event called');
71
- resolve(event);
72
- });
73
- });
74
-
75
- return (
76
- creator.webex.attachmentActions
77
- .listen()
78
- // As the creator, send the card
79
- .then(() =>
80
- creator.webex.messages
81
- .create({
82
- toPersonId: spock.id,
83
- text: 'Message',
84
- attachments: [
85
- {
86
- contentType: 'application/vnd.microsoft.card.adaptive',
87
- content: {
88
- type: 'AdaptiveCard',
89
- version: '1.0',
90
- body: [
91
- {
92
- type: 'Input.Text',
93
- id: 'formInput',
94
- title: 'New Input.Toggle',
95
- placeholder: 'Placeholder text',
96
- },
97
- {
98
- type: 'TextBlock',
99
- text: 'Adaptive Cards',
100
- separation: 'none',
101
- },
102
- ],
103
- actions: [
104
- {
105
- type: 'Action.OpenUrl',
106
- url: 'http://adaptivecards.io',
107
- title: 'Learn More',
108
- },
109
- ],
110
- },
111
- },
112
- ],
113
- })
114
- .then((m) => {
115
- message = m;
116
-
117
- return spock.webex.attachmentActions.create({
118
- // As the other user emulate an Action.Submit button press
119
- type: 'submit',
120
- messageId: message.id,
121
- inputs,
122
- });
123
- })
124
- .then(async (attachmentAction) => {
125
- try {
126
- validateAttachmentAction(attachmentAction, message, buttonPusher, inputs);
127
- const event = await created;
128
-
129
- return validateAttachmentActionEvent(
130
- event,
131
- attachmentAction,
132
- messageSender,
133
- buttonPusher
134
- );
135
- } catch (e) {
136
- // eslint-disable-next-line no-console
137
- console.error(`Failed validating attachmentAction: ${e.message}`);
138
-
139
- return Promise.reject(e);
140
- }
141
- })
142
- )
143
- );
144
- });
145
- });
146
- });
147
-
148
- describe('#get()', () => {
149
- let attachmentAction0;
150
- let message;
151
- const inputs = {
152
- formInput: 'test',
153
- };
154
-
155
- beforeEach(() => {
156
- // "Block" this test with a promise that will
157
- // resolve after the attachmentAction:created arrives.
158
- const created = new Promise((resolve) => {
159
- creator.webex.attachmentActions.on('created', (event) => {
160
- debug('attachmentAction created event called');
161
- resolve(event);
162
- });
163
- });
164
-
165
- return (
166
- creator.webex.attachmentActions
167
- .listen()
168
- // As the creator, send the card
169
- .then(() =>
170
- creator.webex.messages
171
- .create({
172
- toPersonId: spock.id,
173
- text: 'Message',
174
- attachments: [
175
- {
176
- contentType: 'application/vnd.microsoft.card.adaptive',
177
- content: {
178
- type: 'AdaptiveCard',
179
- version: '1.0',
180
- body: [
181
- {
182
- type: 'Input.Text',
183
- id: 'formInput',
184
- title: 'New Input.Toggle',
185
- placeholder: 'Placeholder text',
186
- },
187
- {
188
- type: 'TextBlock',
189
- text: 'Adaptive Cards',
190
- separation: 'none',
191
- },
192
- ],
193
- actions: [
194
- {
195
- type: 'Action.OpenUrl',
196
- url: 'http://adaptivecards.io',
197
- title: 'Learn More',
198
- },
199
- ],
200
- },
201
- },
202
- ],
203
- })
204
- .then((m) => {
205
- message = m;
206
-
207
- return spock.webex.attachmentActions.create({
208
- type: 'submit',
209
- messageId: message.id,
210
- inputs,
211
- });
212
- })
213
- .then(async (attachmentAction) => {
214
- try {
215
- attachmentAction0 = attachmentAction;
216
- validateAttachmentAction(attachmentAction, message, buttonPusher, inputs);
217
- const event = await created;
218
-
219
- return validateAttachmentActionEvent(
220
- event,
221
- attachmentAction,
222
- messageSender,
223
- buttonPusher
224
- );
225
- } catch (e) {
226
- // eslint-disable-next-line no-console
227
- console.error(`Failed validating attachmentAction: ${e.message}`);
228
-
229
- return Promise.reject(e);
230
- }
231
- })
232
- )
233
- );
234
- });
235
-
236
- it('retrieves a specific attachment action', () =>
237
- spock.webex.attachmentActions.get(attachmentAction0).then((attachmentAction) => {
238
- validateAttachmentAction(attachmentAction, message, buttonPusher, inputs);
239
- }));
240
- });
241
- });
242
-
243
- /**
244
- * Validate an AttachmentAction object.
245
- * @param {Object} attachmentAction - object to validate
246
- * @param {Object} message - parent message object
247
- * @param {Object} actor - person object who created the attachmentAction
248
- * @param {Object} data - optional inputs object to check against
249
- * @returns {void}
250
- */
251
- function validateAttachmentAction(attachmentAction, message, actor, data) {
252
- assert.isNotNull(attachmentAction.id);
253
- assert.isNotNull(attachmentAction.created);
254
- assert.equal(attachmentAction.type, 'submit');
255
- assert.equal(attachmentAction.messageId, message.id);
256
- assert.isObject(attachmentAction.inputs);
257
- assert.equal(attachmentAction.personId, actor.id);
258
-
259
- if (data && typeof data === 'object') {
260
- Object.entries(data).forEach((entry) => {
261
- assert.propertyVal(attachmentAction.inputs, entry[0], entry[1]);
262
- });
263
- }
264
- }
265
-
266
- /**
267
- * Validate an AttachmentAction event.
268
- * @param {Object} event - attachmentAction event
269
- * @param {Object} attachmentAction - return from the API that generate this event
270
- * @param {Object} messageSender - person object for user who posted the card
271
- * @param {Object} buttonPusher - person object for user who pushed the Action.Submit button
272
- * @returns {void}
273
- */
274
- function validateAttachmentActionEvent(event, attachmentAction, messageSender, buttonPusher) {
275
- assert.isTrue(
276
- event.resource === SDK_EVENT.EXTERNAL.RESOURCE.ATTACHMENT_ACTIONS,
277
- 'not an attachmentAction event'
278
- );
279
- assert.isDefined(event.event, 'attachmentAction event type not set');
280
- assert.isDefined(event.created, 'event listener created date not set');
281
- assert.equal(
282
- event.createdBy,
283
- messageSender.id,
284
- 'event listener createdBy not set to our messageSender'
285
- );
286
- assert.equal(
287
- event.orgId,
288
- messageSender.orgId,
289
- "event listener orgId not === to our messageSender's"
290
- );
291
- assert.equal(event.ownedBy, 'creator', 'event listener not owned by creator');
292
- assert.equal(event.status, 'active', 'event listener status not active');
293
- assert.equal(event.actorId, buttonPusher.id, "event actorId not equal to our buttonPusher's id");
294
-
295
- // Ensure event data matches data returned from function call
296
- assert.equal(event.data.id, attachmentAction.id, 'event/attachmentAction.id not equal');
297
- assert.equal(
298
- event.data.roomId,
299
- attachmentAction.roomId,
300
- 'event/attachmentAction.roomId not equal'
301
- );
302
- assert.equal(
303
- event.data.personId,
304
- attachmentAction.personId,
305
- 'event/attachmentAction.personId not equal'
306
- );
307
- assert.equal(
308
- event.data.personEmail,
309
- attachmentAction.personEmail,
310
- 'event/attachmentAction.personEmail not equal'
311
- );
312
- assert.equal(
313
- event.data.roomType,
314
- attachmentAction.roomType,
315
- 'event/attachmentAction.roomType not equal'
316
- );
317
-
318
- if (event.data.inputs && typeof event.data.inputs === 'object') {
319
- assert.isObject(attachmentAction.inputs);
320
- Object.entries(event.data.inputs).forEach((entry) => {
321
- assert.propertyVal(attachmentAction.inputs, entry[0], entry[1]);
322
- });
323
- }
324
- }
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import '@webex/plugin-logger';
6
+ import '@webex/plugin-people';
7
+ import '@webex/plugin-messages';
8
+ import '@webex/plugin-attachment-actions';
9
+ import {SDK_EVENT} from '@webex/common';
10
+ import {assert} from '@webex/test-helper-chai';
11
+ import WebexCore from '@webex/webex-core';
12
+ import testUsers from '@webex/test-helper-test-users';
13
+
14
+ const debug = require('debug')('attachmentActions');
15
+
16
+ describe('plugin-attachment-actions', function () {
17
+ this.timeout(60000);
18
+
19
+ let creator, spock;
20
+ let messageSender, buttonPusher;
21
+
22
+ before(() =>
23
+ testUsers.create({count: 2}).then((users) => {
24
+ [creator, spock] = users;
25
+
26
+ // Creator will create the cards
27
+ creator.webex = new WebexCore({
28
+ credentials: {
29
+ authorization: users[0].token,
30
+ },
31
+ });
32
+
33
+ // Spock is the actor who will hit the Action.Submit button
34
+ spock.webex = new WebexCore({
35
+ credentials: {
36
+ authorization: users[1].token,
37
+ },
38
+ });
39
+ spock.webex.people
40
+ .get('me')
41
+ .then((person) => {
42
+ buttonPusher = person;
43
+
44
+ return creator.webex.people.get('me');
45
+ })
46
+ .then((person) => {
47
+ messageSender = person;
48
+ });
49
+ })
50
+ );
51
+
52
+ // Stop listening for websocket events
53
+ afterEach(() => {
54
+ creator.webex.attachmentActions.stopListening();
55
+ creator.webex.attachmentActions.off('created');
56
+ });
57
+
58
+ describe('#attachmentActions', () => {
59
+ describe('#create()', () => {
60
+ it('creates an attachment action and validates the attachment action created', () => {
61
+ let message;
62
+ const inputs = {
63
+ formInput: 'test',
64
+ };
65
+
66
+ // "Block" this test with a promise that will
67
+ // resolve after the attachmentAction:created arrives.
68
+ const created = new Promise((resolve) => {
69
+ creator.webex.attachmentActions.on('created', (event) => {
70
+ debug('attachmentAction created event called');
71
+ resolve(event);
72
+ });
73
+ });
74
+
75
+ return (
76
+ creator.webex.attachmentActions
77
+ .listen()
78
+ // As the creator, send the card
79
+ .then(() =>
80
+ creator.webex.messages
81
+ .create({
82
+ toPersonId: spock.id,
83
+ text: 'Message',
84
+ attachments: [
85
+ {
86
+ contentType: 'application/vnd.microsoft.card.adaptive',
87
+ content: {
88
+ type: 'AdaptiveCard',
89
+ version: '1.0',
90
+ body: [
91
+ {
92
+ type: 'Input.Text',
93
+ id: 'formInput',
94
+ title: 'New Input.Toggle',
95
+ placeholder: 'Placeholder text',
96
+ },
97
+ {
98
+ type: 'TextBlock',
99
+ text: 'Adaptive Cards',
100
+ separation: 'none',
101
+ },
102
+ ],
103
+ actions: [
104
+ {
105
+ type: 'Action.OpenUrl',
106
+ url: 'http://adaptivecards.io',
107
+ title: 'Learn More',
108
+ },
109
+ ],
110
+ },
111
+ },
112
+ ],
113
+ })
114
+ .then((m) => {
115
+ message = m;
116
+
117
+ return spock.webex.attachmentActions.create({
118
+ // As the other user emulate an Action.Submit button press
119
+ type: 'submit',
120
+ messageId: message.id,
121
+ inputs,
122
+ });
123
+ })
124
+ .then(async (attachmentAction) => {
125
+ try {
126
+ validateAttachmentAction(attachmentAction, message, buttonPusher, inputs);
127
+ const event = await created;
128
+
129
+ return validateAttachmentActionEvent(
130
+ event,
131
+ attachmentAction,
132
+ messageSender,
133
+ buttonPusher
134
+ );
135
+ } catch (e) {
136
+ // eslint-disable-next-line no-console
137
+ console.error(`Failed validating attachmentAction: ${e.message}`);
138
+
139
+ return Promise.reject(e);
140
+ }
141
+ })
142
+ )
143
+ );
144
+ });
145
+ });
146
+ });
147
+
148
+ describe('#get()', () => {
149
+ let attachmentAction0;
150
+ let message;
151
+ const inputs = {
152
+ formInput: 'test',
153
+ };
154
+
155
+ beforeEach(() => {
156
+ // "Block" this test with a promise that will
157
+ // resolve after the attachmentAction:created arrives.
158
+ const created = new Promise((resolve) => {
159
+ creator.webex.attachmentActions.on('created', (event) => {
160
+ debug('attachmentAction created event called');
161
+ resolve(event);
162
+ });
163
+ });
164
+
165
+ return (
166
+ creator.webex.attachmentActions
167
+ .listen()
168
+ // As the creator, send the card
169
+ .then(() =>
170
+ creator.webex.messages
171
+ .create({
172
+ toPersonId: spock.id,
173
+ text: 'Message',
174
+ attachments: [
175
+ {
176
+ contentType: 'application/vnd.microsoft.card.adaptive',
177
+ content: {
178
+ type: 'AdaptiveCard',
179
+ version: '1.0',
180
+ body: [
181
+ {
182
+ type: 'Input.Text',
183
+ id: 'formInput',
184
+ title: 'New Input.Toggle',
185
+ placeholder: 'Placeholder text',
186
+ },
187
+ {
188
+ type: 'TextBlock',
189
+ text: 'Adaptive Cards',
190
+ separation: 'none',
191
+ },
192
+ ],
193
+ actions: [
194
+ {
195
+ type: 'Action.OpenUrl',
196
+ url: 'http://adaptivecards.io',
197
+ title: 'Learn More',
198
+ },
199
+ ],
200
+ },
201
+ },
202
+ ],
203
+ })
204
+ .then((m) => {
205
+ message = m;
206
+
207
+ return spock.webex.attachmentActions.create({
208
+ type: 'submit',
209
+ messageId: message.id,
210
+ inputs,
211
+ });
212
+ })
213
+ .then(async (attachmentAction) => {
214
+ try {
215
+ attachmentAction0 = attachmentAction;
216
+ validateAttachmentAction(attachmentAction, message, buttonPusher, inputs);
217
+ const event = await created;
218
+
219
+ return validateAttachmentActionEvent(
220
+ event,
221
+ attachmentAction,
222
+ messageSender,
223
+ buttonPusher
224
+ );
225
+ } catch (e) {
226
+ // eslint-disable-next-line no-console
227
+ console.error(`Failed validating attachmentAction: ${e.message}`);
228
+
229
+ return Promise.reject(e);
230
+ }
231
+ })
232
+ )
233
+ );
234
+ });
235
+
236
+ it('retrieves a specific attachment action', () =>
237
+ spock.webex.attachmentActions.get(attachmentAction0).then((attachmentAction) => {
238
+ validateAttachmentAction(attachmentAction, message, buttonPusher, inputs);
239
+ }));
240
+ });
241
+ });
242
+
243
+ /**
244
+ * Validate an AttachmentAction object.
245
+ * @param {Object} attachmentAction - object to validate
246
+ * @param {Object} message - parent message object
247
+ * @param {Object} actor - person object who created the attachmentAction
248
+ * @param {Object} data - optional inputs object to check against
249
+ * @returns {void}
250
+ */
251
+ function validateAttachmentAction(attachmentAction, message, actor, data) {
252
+ assert.isNotNull(attachmentAction.id);
253
+ assert.isNotNull(attachmentAction.created);
254
+ assert.equal(attachmentAction.type, 'submit');
255
+ assert.equal(attachmentAction.messageId, message.id);
256
+ assert.isObject(attachmentAction.inputs);
257
+ assert.equal(attachmentAction.personId, actor.id);
258
+
259
+ if (data && typeof data === 'object') {
260
+ Object.entries(data).forEach((entry) => {
261
+ assert.propertyVal(attachmentAction.inputs, entry[0], entry[1]);
262
+ });
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Validate an AttachmentAction event.
268
+ * @param {Object} event - attachmentAction event
269
+ * @param {Object} attachmentAction - return from the API that generate this event
270
+ * @param {Object} messageSender - person object for user who posted the card
271
+ * @param {Object} buttonPusher - person object for user who pushed the Action.Submit button
272
+ * @returns {void}
273
+ */
274
+ function validateAttachmentActionEvent(event, attachmentAction, messageSender, buttonPusher) {
275
+ assert.isTrue(
276
+ event.resource === SDK_EVENT.EXTERNAL.RESOURCE.ATTACHMENT_ACTIONS,
277
+ 'not an attachmentAction event'
278
+ );
279
+ assert.isDefined(event.event, 'attachmentAction event type not set');
280
+ assert.isDefined(event.created, 'event listener created date not set');
281
+ assert.equal(
282
+ event.createdBy,
283
+ messageSender.id,
284
+ 'event listener createdBy not set to our messageSender'
285
+ );
286
+ assert.equal(
287
+ event.orgId,
288
+ messageSender.orgId,
289
+ "event listener orgId not === to our messageSender's"
290
+ );
291
+ assert.equal(event.ownedBy, 'creator', 'event listener not owned by creator');
292
+ assert.equal(event.status, 'active', 'event listener status not active');
293
+ assert.equal(event.actorId, buttonPusher.id, "event actorId not equal to our buttonPusher's id");
294
+
295
+ // Ensure event data matches data returned from function call
296
+ assert.equal(event.data.id, attachmentAction.id, 'event/attachmentAction.id not equal');
297
+ assert.equal(
298
+ event.data.roomId,
299
+ attachmentAction.roomId,
300
+ 'event/attachmentAction.roomId not equal'
301
+ );
302
+ assert.equal(
303
+ event.data.personId,
304
+ attachmentAction.personId,
305
+ 'event/attachmentAction.personId not equal'
306
+ );
307
+ assert.equal(
308
+ event.data.personEmail,
309
+ attachmentAction.personEmail,
310
+ 'event/attachmentAction.personEmail not equal'
311
+ );
312
+ assert.equal(
313
+ event.data.roomType,
314
+ attachmentAction.roomType,
315
+ 'event/attachmentAction.roomType not equal'
316
+ );
317
+
318
+ if (event.data.inputs && typeof event.data.inputs === 'object') {
319
+ assert.isObject(attachmentAction.inputs);
320
+ Object.entries(event.data.inputs).forEach((entry) => {
321
+ assert.propertyVal(attachmentAction.inputs, entry[0], entry[1]);
322
+ });
323
+ }
324
+ }