@webex/test-users 2.59.2 → 2.59.3-next.1

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.
package/src/index.js CHANGED
@@ -1,318 +1,318 @@
1
- import assert from 'assert';
2
-
3
- import uuid from 'uuid';
4
- import btoa from 'btoa';
5
- import _ from 'lodash';
6
- import randomName from 'node-random-name';
7
- import {request} from '@webex/http-core';
8
-
9
- const BASE_PATH_SECURE = '/users/test_users_s';
10
- const BASE_PATH = '/users/test_users';
11
-
12
- /**
13
- * Computes `expires` and `refresh_token_expires` from `expires_in` and
14
- * `refresh_token_expires_in` and creates an `authorization` string.
15
- * @param {Object} token
16
- * @private
17
- * @returns {Object}
18
- */
19
- function fixToken(token) {
20
- const now = Date.now();
21
-
22
- if (token.expires_in && !token.expires) {
23
- token.expires = now + token.expires_in * 1000;
24
- }
25
-
26
- if (token.refresh_token_expires_in && !token.refresh_token_expires) {
27
- /* eslint camelcase: [0] */
28
- token.refresh_token_expires = now + token.refresh_token_expires_in * 1000;
29
- }
30
-
31
- if (token.token_type && token.access_token) {
32
- token.authorization = `${token.token_type} ${token.access_token}`;
33
- }
34
-
35
- return token;
36
- }
37
-
38
- let clientToken;
39
-
40
- /**
41
- * Fetches credentials to talk to the test_users_s endpoint
42
- *
43
- * Caches result in `clientToken` variable for multiple runs
44
- * @param {Object} options
45
- * @param {string} options.clientId
46
- * @param {string} options.clientSecret
47
- * @param {string} options.idbrokerUrl
48
- * @private
49
- * @returns {String}
50
- */
51
- function getClientCredentials({clientId, clientSecret, idbrokerUrl}) {
52
- if (clientToken) {
53
- return Promise.resolve(clientToken);
54
- }
55
-
56
- return request({
57
- method: 'POST',
58
- uri: `${idbrokerUrl}/idb/oauth2/v1/access_token`,
59
- json: true,
60
- form: {
61
- grant_type: 'client_credentials',
62
- scope: 'Identity:SCIM webexsquare:get_conversation',
63
- client_id: clientId,
64
- client_secret: clientSecret,
65
- },
66
- headers: {
67
- // Note: we can't request's auth hash here because this endpoint expects
68
- // us to send the auth header *without including "Basic "* before the
69
- // token string
70
- authorization: btoa(`${clientId}:${clientSecret}`),
71
- },
72
- })
73
- .then((res) => {
74
- const token = fixToken(res.body);
75
-
76
- return `${token.token_type} ${token.access_token}`;
77
- })
78
- .then((token) => {
79
- clientToken = token;
80
-
81
- return clientToken;
82
- });
83
- }
84
-
85
- /**
86
- * Makes a request authorized with client credentials
87
- * @param {Object} options
88
- * @param {Object} options.body
89
- * @param {string} options.body.clientId
90
- * @param {string} options.body.clientSecret
91
- * @param {string} options.body.idbrokerUrl
92
- * @private
93
- * @returns {Promise<HttpResponseObject>}
94
- */
95
- function requestWithAuth(options) {
96
- return getClientCredentials(options.body).then((authorization) => {
97
- options.headers = options.headers || {};
98
- options.headers.authorization = authorization;
99
-
100
- return request(options);
101
- });
102
- }
103
-
104
- /**
105
- * @typedef {Object} AccessTokenObject
106
- * @property {string} token.access_token
107
- * @property {Number} token.expires_in
108
- * @property {string} token.token_type
109
- * @property {string} token.refresh_token
110
- * @property {Number} token.refresh_token_expires_in
111
- * @property {string} token.expires
112
- * @property {string} token.refresh_token_expires
113
- */
114
-
115
- /**
116
- * @typedef {Object} CreateUserOptions
117
- * @property {boolean} [authCodeOnly] generates auth_code
118
- * @param {string} [clientId] defaults to WEBEX_CLIENT_ID
119
- * @param {string} [clientSecret] defaults to WEBEX_CLIENT_SECRET
120
- * @param {string} [cigServiceUrl] defaults to WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL
121
- * @property {string} [displayName]
122
- * @property {string} [emailAddress]
123
- * @property {Array.<string>} [entitlements]
124
- * @param {string} [idbrokerUrl] defaults to IDBROKER_BASE_URL
125
- * @property {string} [machineType] used when creating a machine user/device
126
- * @property {string} [orgId] organization ID to create the user under
127
- * @property {string} [password] defaults to a random password
128
- * @property {string} [roles] defaults to []
129
- * @property {string} [scope] defaults to WEBEX_SCOPE
130
- * @property {string} [type] used to create a machine
131
- */
132
-
133
- /**
134
- * @typedef {Object} TestUserObject
135
- * @property {string} password
136
- * @property {string} emailAddress
137
- * @property {string} displayName
138
- * @property {string} id
139
- * @property {string} userName
140
- * @property {string} email
141
- * @property {string} name
142
- * @property {string} givenName
143
- * @property {string} type
144
- * @property {Array.<string>} entitlements
145
- * @property {string} orgId
146
- * @property {AccessTokenObject} token
147
- */
148
-
149
- /**
150
- * Creates a test user
151
- * @param {CreateUserOptions} options
152
- * @returns {Promise.<TestUserObject>}
153
- */
154
- export function createTestUser(options = {}) {
155
- const clientId = options.clientId || process.env.WEBEX_CLIENT_ID;
156
- const clientSecret = options.clientSecret || process.env.WEBEX_CLIENT_SECRET;
157
- const idbrokerUrl = options.idbrokerUrl || process.env.IDBROKER_BASE_URL;
158
- const cigServiceUrl =
159
- options.cigServiceUrl ||
160
- process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL ||
161
- process.env.WEBEX_TEST_USERS_CONVERSATION_SERVICE_URL;
162
-
163
- if (!clientId) {
164
- throw new Error('options.clientId or process.env.WEBEX_CLIENT_ID must be defined');
165
- }
166
-
167
- if (!clientSecret) {
168
- throw new Error('options.clientSecret or process.env.WEBEX_CLIENT_SECRET must be defined');
169
- }
170
-
171
- if (!idbrokerUrl) {
172
- throw new Error('options.idbrokerUrl or process.env.IDBROKER_BASE_URL must be defined');
173
- }
174
-
175
- if (!cigServiceUrl) {
176
- throw new Error(
177
- 'options.cigServiceUrl or process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL must be defined'
178
- );
179
- }
180
-
181
- const body = {
182
- authCodeOnly: options.authCodeOnly,
183
- clientId,
184
- clientSecret,
185
- displayName: options.displayName || randomName(),
186
- emailTemplate: options.emailAddress,
187
- entitlements: options.entitlements || [
188
- 'spark',
189
- 'squaredCallInitiation',
190
- 'squaredRoomModeration',
191
- 'squaredInviter',
192
- 'webExSquared',
193
- ],
194
- idbrokerUrl,
195
- machineType: options.machineType,
196
- orgId: options.orgId,
197
- // The five characters on the end are to hit all the password requirements
198
- password: options.password || `${uuid.v4()}zAY1*`,
199
- roles: options.roles || [],
200
- scopes: options.scope || process.env.WEBEX_SCOPE,
201
- type: options.type,
202
- };
203
-
204
- return requestWithAuth({
205
- method: 'POST',
206
- uri: `${cigServiceUrl}${BASE_PATH_SECURE}`,
207
- json: true,
208
- body,
209
- }).then((res) => ({
210
- password: body.password,
211
- emailAddress: res.body.user.email,
212
- displayName: res.body.user.name,
213
- ...res.body.user,
214
- token: fixToken(res.body.token),
215
- }));
216
- }
217
-
218
- /**
219
- * Exchanges a user name/password for an access token
220
- * @param {Object} options
221
- * @param {string} options.id
222
- * @param {string} options.email
223
- * @param {string} options.password
224
- * @param {string} options.clientId
225
- * @param {string} options.clientSecret
226
- * @param {string} options.cigServiceUrl
227
- * @returns {Promise.<AccessTokenObject>}
228
- */
229
- export function loginTestUser(options) {
230
- const clientId = options.clientId || process.env.WEBEX_CLIENT_ID;
231
- const clientSecret = options.clientSecret || process.env.WEBEX_CLIENT_SECRET;
232
- const cigServiceUrl =
233
- options.cigServiceUrl ||
234
- process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL ||
235
- process.env.WEBEX_TEST_USERS_CONVERSATION_SERVICE_URL;
236
-
237
- if (!clientId) {
238
- throw new Error('options.clientId or process.env.WEBEX_CLIENT_ID must be defined');
239
- }
240
-
241
- if (!clientSecret) {
242
- throw new Error('options.clientSecret or process.env.WEBEX_CLIENT_SECRET must be defined');
243
- }
244
-
245
- if (!cigServiceUrl) {
246
- throw new Error(
247
- 'options.cigServiceUrl or process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL must be defined'
248
- );
249
- }
250
-
251
- return request({
252
- method: 'POST',
253
- uri: `${cigServiceUrl}${BASE_PATH}/login`,
254
- json: true,
255
- body: _.defaultsDeep(options, {
256
- clientId,
257
- clientSecret,
258
- }),
259
- }).then((res) => fixToken(res.body));
260
- }
261
-
262
- /**
263
- * Removes a test user
264
- * @param {Object} options
265
- * @param {string} options.id user id to remove
266
- * @param {string} options.cigServiceUrl
267
- * @param {Object} options.token
268
- * @param {string} options.token.authorization
269
- * @param {string} [options.token.refresh_token]
270
- * @returns {Promise}
271
- */
272
- export function removeTestUser(options = {}) {
273
- const cigServiceUrl =
274
- options.cigServiceUrl ||
275
- process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL ||
276
- process.env.WEBEX_TEST_USERS_CONVERSATION_SERVICE_URL;
277
-
278
- if (!cigServiceUrl) {
279
- throw new Error(
280
- 'options.cigServiceUrl or process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL must be defined'
281
- );
282
- }
283
-
284
- if (!options.id) {
285
- return Promise.reject(new Error('options.id is required'));
286
- }
287
-
288
- if (!options.token) {
289
- return loginTestUser(options).then((token) => {
290
- options.token = token;
291
-
292
- return removeTestUser(options);
293
- });
294
- }
295
-
296
- assert(options.token.authorization, 'options.token.authorization must be defined');
297
-
298
- return request({
299
- method: 'POST',
300
- json: true,
301
- headers: {
302
- authorization: options.token.authorization,
303
- },
304
- body: {
305
- /* eslint-disable camelcase */
306
- user_id: options.id,
307
- refresh_token: options.token.refresh_token,
308
- user_type: options.userType || 'PERSON',
309
- /* eslint-enable camelcase */
310
- },
311
- uri: `${cigServiceUrl}${BASE_PATH}/delete`,
312
- });
313
- }
314
-
315
- export {
316
- default as createWhistlerTestUser,
317
- removeTestUser as removeWhistlerTestUser,
318
- } from './whistler';
1
+ import assert from 'assert';
2
+
3
+ import uuid from 'uuid';
4
+ import btoa from 'btoa';
5
+ import _ from 'lodash';
6
+ import randomName from 'node-random-name';
7
+ import {request} from '@webex/http-core';
8
+
9
+ const BASE_PATH_SECURE = '/users/test_users_s';
10
+ const BASE_PATH = '/users/test_users';
11
+
12
+ /**
13
+ * Computes `expires` and `refresh_token_expires` from `expires_in` and
14
+ * `refresh_token_expires_in` and creates an `authorization` string.
15
+ * @param {Object} token
16
+ * @private
17
+ * @returns {Object}
18
+ */
19
+ function fixToken(token) {
20
+ const now = Date.now();
21
+
22
+ if (token.expires_in && !token.expires) {
23
+ token.expires = now + token.expires_in * 1000;
24
+ }
25
+
26
+ if (token.refresh_token_expires_in && !token.refresh_token_expires) {
27
+ /* eslint camelcase: [0] */
28
+ token.refresh_token_expires = now + token.refresh_token_expires_in * 1000;
29
+ }
30
+
31
+ if (token.token_type && token.access_token) {
32
+ token.authorization = `${token.token_type} ${token.access_token}`;
33
+ }
34
+
35
+ return token;
36
+ }
37
+
38
+ let clientToken;
39
+
40
+ /**
41
+ * Fetches credentials to talk to the test_users_s endpoint
42
+ *
43
+ * Caches result in `clientToken` variable for multiple runs
44
+ * @param {Object} options
45
+ * @param {string} options.clientId
46
+ * @param {string} options.clientSecret
47
+ * @param {string} options.idbrokerUrl
48
+ * @private
49
+ * @returns {String}
50
+ */
51
+ function getClientCredentials({clientId, clientSecret, idbrokerUrl}) {
52
+ if (clientToken) {
53
+ return Promise.resolve(clientToken);
54
+ }
55
+
56
+ return request({
57
+ method: 'POST',
58
+ uri: `${idbrokerUrl}/idb/oauth2/v1/access_token`,
59
+ json: true,
60
+ form: {
61
+ grant_type: 'client_credentials',
62
+ scope: 'Identity:SCIM webexsquare:get_conversation',
63
+ client_id: clientId,
64
+ client_secret: clientSecret,
65
+ },
66
+ headers: {
67
+ // Note: we can't request's auth hash here because this endpoint expects
68
+ // us to send the auth header *without including "Basic "* before the
69
+ // token string
70
+ authorization: btoa(`${clientId}:${clientSecret}`),
71
+ },
72
+ })
73
+ .then((res) => {
74
+ const token = fixToken(res.body);
75
+
76
+ return `${token.token_type} ${token.access_token}`;
77
+ })
78
+ .then((token) => {
79
+ clientToken = token;
80
+
81
+ return clientToken;
82
+ });
83
+ }
84
+
85
+ /**
86
+ * Makes a request authorized with client credentials
87
+ * @param {Object} options
88
+ * @param {Object} options.body
89
+ * @param {string} options.body.clientId
90
+ * @param {string} options.body.clientSecret
91
+ * @param {string} options.body.idbrokerUrl
92
+ * @private
93
+ * @returns {Promise<HttpResponseObject>}
94
+ */
95
+ function requestWithAuth(options) {
96
+ return getClientCredentials(options.body).then((authorization) => {
97
+ options.headers = options.headers || {};
98
+ options.headers.authorization = authorization;
99
+
100
+ return request(options);
101
+ });
102
+ }
103
+
104
+ /**
105
+ * @typedef {Object} AccessTokenObject
106
+ * @property {string} token.access_token
107
+ * @property {Number} token.expires_in
108
+ * @property {string} token.token_type
109
+ * @property {string} token.refresh_token
110
+ * @property {Number} token.refresh_token_expires_in
111
+ * @property {string} token.expires
112
+ * @property {string} token.refresh_token_expires
113
+ */
114
+
115
+ /**
116
+ * @typedef {Object} CreateUserOptions
117
+ * @property {boolean} [authCodeOnly] generates auth_code
118
+ * @param {string} [clientId] defaults to WEBEX_CLIENT_ID
119
+ * @param {string} [clientSecret] defaults to WEBEX_CLIENT_SECRET
120
+ * @param {string} [cigServiceUrl] defaults to WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL
121
+ * @property {string} [displayName]
122
+ * @property {string} [emailAddress]
123
+ * @property {Array.<string>} [entitlements]
124
+ * @param {string} [idbrokerUrl] defaults to IDBROKER_BASE_URL
125
+ * @property {string} [machineType] used when creating a machine user/device
126
+ * @property {string} [orgId] organization ID to create the user under
127
+ * @property {string} [password] defaults to a random password
128
+ * @property {string} [roles] defaults to []
129
+ * @property {string} [scope] defaults to WEBEX_SCOPE
130
+ * @property {string} [type] used to create a machine
131
+ */
132
+
133
+ /**
134
+ * @typedef {Object} TestUserObject
135
+ * @property {string} password
136
+ * @property {string} emailAddress
137
+ * @property {string} displayName
138
+ * @property {string} id
139
+ * @property {string} userName
140
+ * @property {string} email
141
+ * @property {string} name
142
+ * @property {string} givenName
143
+ * @property {string} type
144
+ * @property {Array.<string>} entitlements
145
+ * @property {string} orgId
146
+ * @property {AccessTokenObject} token
147
+ */
148
+
149
+ /**
150
+ * Creates a test user
151
+ * @param {CreateUserOptions} options
152
+ * @returns {Promise.<TestUserObject>}
153
+ */
154
+ export function createTestUser(options = {}) {
155
+ const clientId = options.clientId || process.env.WEBEX_CLIENT_ID;
156
+ const clientSecret = options.clientSecret || process.env.WEBEX_CLIENT_SECRET;
157
+ const idbrokerUrl = options.idbrokerUrl || process.env.IDBROKER_BASE_URL;
158
+ const cigServiceUrl =
159
+ options.cigServiceUrl ||
160
+ process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL ||
161
+ process.env.WEBEX_TEST_USERS_CONVERSATION_SERVICE_URL;
162
+
163
+ if (!clientId) {
164
+ throw new Error('options.clientId or process.env.WEBEX_CLIENT_ID must be defined');
165
+ }
166
+
167
+ if (!clientSecret) {
168
+ throw new Error('options.clientSecret or process.env.WEBEX_CLIENT_SECRET must be defined');
169
+ }
170
+
171
+ if (!idbrokerUrl) {
172
+ throw new Error('options.idbrokerUrl or process.env.IDBROKER_BASE_URL must be defined');
173
+ }
174
+
175
+ if (!cigServiceUrl) {
176
+ throw new Error(
177
+ 'options.cigServiceUrl or process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL must be defined'
178
+ );
179
+ }
180
+
181
+ const body = {
182
+ authCodeOnly: options.authCodeOnly,
183
+ clientId,
184
+ clientSecret,
185
+ displayName: options.displayName || randomName(),
186
+ emailTemplate: options.emailAddress,
187
+ entitlements: options.entitlements || [
188
+ 'spark',
189
+ 'squaredCallInitiation',
190
+ 'squaredRoomModeration',
191
+ 'squaredInviter',
192
+ 'webExSquared',
193
+ ],
194
+ idbrokerUrl,
195
+ machineType: options.machineType,
196
+ orgId: options.orgId,
197
+ // The five characters on the end are to hit all the password requirements
198
+ password: options.password || `${uuid.v4()}zAY1*`,
199
+ roles: options.roles || [],
200
+ scopes: options.scope || process.env.WEBEX_SCOPE,
201
+ type: options.type,
202
+ };
203
+
204
+ return requestWithAuth({
205
+ method: 'POST',
206
+ uri: `${cigServiceUrl}${BASE_PATH_SECURE}`,
207
+ json: true,
208
+ body,
209
+ }).then((res) => ({
210
+ password: body.password,
211
+ emailAddress: res.body.user.email,
212
+ displayName: res.body.user.name,
213
+ ...res.body.user,
214
+ token: fixToken(res.body.token),
215
+ }));
216
+ }
217
+
218
+ /**
219
+ * Exchanges a user name/password for an access token
220
+ * @param {Object} options
221
+ * @param {string} options.id
222
+ * @param {string} options.email
223
+ * @param {string} options.password
224
+ * @param {string} options.clientId
225
+ * @param {string} options.clientSecret
226
+ * @param {string} options.cigServiceUrl
227
+ * @returns {Promise.<AccessTokenObject>}
228
+ */
229
+ export function loginTestUser(options) {
230
+ const clientId = options.clientId || process.env.WEBEX_CLIENT_ID;
231
+ const clientSecret = options.clientSecret || process.env.WEBEX_CLIENT_SECRET;
232
+ const cigServiceUrl =
233
+ options.cigServiceUrl ||
234
+ process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL ||
235
+ process.env.WEBEX_TEST_USERS_CONVERSATION_SERVICE_URL;
236
+
237
+ if (!clientId) {
238
+ throw new Error('options.clientId or process.env.WEBEX_CLIENT_ID must be defined');
239
+ }
240
+
241
+ if (!clientSecret) {
242
+ throw new Error('options.clientSecret or process.env.WEBEX_CLIENT_SECRET must be defined');
243
+ }
244
+
245
+ if (!cigServiceUrl) {
246
+ throw new Error(
247
+ 'options.cigServiceUrl or process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL must be defined'
248
+ );
249
+ }
250
+
251
+ return request({
252
+ method: 'POST',
253
+ uri: `${cigServiceUrl}${BASE_PATH}/login`,
254
+ json: true,
255
+ body: _.defaultsDeep(options, {
256
+ clientId,
257
+ clientSecret,
258
+ }),
259
+ }).then((res) => fixToken(res.body));
260
+ }
261
+
262
+ /**
263
+ * Removes a test user
264
+ * @param {Object} options
265
+ * @param {string} options.id user id to remove
266
+ * @param {string} options.cigServiceUrl
267
+ * @param {Object} options.token
268
+ * @param {string} options.token.authorization
269
+ * @param {string} [options.token.refresh_token]
270
+ * @returns {Promise}
271
+ */
272
+ export function removeTestUser(options = {}) {
273
+ const cigServiceUrl =
274
+ options.cigServiceUrl ||
275
+ process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL ||
276
+ process.env.WEBEX_TEST_USERS_CONVERSATION_SERVICE_URL;
277
+
278
+ if (!cigServiceUrl) {
279
+ throw new Error(
280
+ 'options.cigServiceUrl or process.env.WEBEX_TEST_USERS_CI_GATEWAY_SERVICE_URL must be defined'
281
+ );
282
+ }
283
+
284
+ if (!options.id) {
285
+ return Promise.reject(new Error('options.id is required'));
286
+ }
287
+
288
+ if (!options.token) {
289
+ return loginTestUser(options).then((token) => {
290
+ options.token = token;
291
+
292
+ return removeTestUser(options);
293
+ });
294
+ }
295
+
296
+ assert(options.token.authorization, 'options.token.authorization must be defined');
297
+
298
+ return request({
299
+ method: 'POST',
300
+ json: true,
301
+ headers: {
302
+ authorization: options.token.authorization,
303
+ },
304
+ body: {
305
+ /* eslint-disable camelcase */
306
+ user_id: options.id,
307
+ refresh_token: options.token.refresh_token,
308
+ user_type: options.userType || 'PERSON',
309
+ /* eslint-enable camelcase */
310
+ },
311
+ uri: `${cigServiceUrl}${BASE_PATH}/delete`,
312
+ });
313
+ }
314
+
315
+ export {
316
+ default as createWhistlerTestUser,
317
+ removeTestUser as removeWhistlerTestUser,
318
+ } from './whistler';