-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathappcredential.js
478 lines (450 loc) · 15.2 KB
/
appcredential.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// appcredential.js
// ------------------------------------------------------------------
//
// Tests for operations on App Credentials
//
// Copyright 2019-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/* global describe, faker, it, before, after, assert */
const common = require("./common");
const util = require("util");
//require("request-debug")(require("postman-request"));
const delay = (ms) =>
function (x) {
return new Promise((resolve) => setTimeout(() => resolve(x), ms));
};
describe("AppCredential", function () {
this.timeout(common.testTimeout);
this.slow(common.slowThreshold);
common.connectApigee(function (org) {
const entityName =
"apigee-edge-js-test-" + faker.lorem.word() + faker.random.number(),
firstName = faker.name.firstName(),
lastName = faker.name.lastName(),
developerEmail = `${firstName}.${lastName}@apigee-edge-js-test.org`,
testCredential =
faker.lorem.word() +
"-" +
faker.lorem.word() +
"-" +
faker.random.number(),
createOptions = {
developerEmail,
lastName,
firstName,
userName: firstName + lastName,
};
let apiProducts = [];
before(() =>
org.developers
.create(createOptions)
.then(() => org.products.get())
.then((result) => {
apiProducts = result.apiProduct
? result.apiProduct.map((p) => p.name)
: result;
})
.then(delay(1200)) // not sure why. But we need to wait til the dev exists.
.then(() => {
const appCreateOptions = {
developerEmail,
name: entityName,
apiProduct: apiProducts[0],
};
return org.developerapps.create(appCreateOptions);
}),
);
after(() =>
org.developerapps
.get({ developerEmail })
.then((apps) => {
let p = Promise.resolve({});
if (apps && apps.length) {
const reducer = (promise, item) =>
promise.then((a) =>
org.developerapps.del({ developerEmail, name: item }),
);
p = apps.reduce(reducer, Promise.resolve([]));
}
return p.then((_) => org.developers.del({ developerEmail }));
})
// with mint orgs, this can fail
.catch((reason) => console.log(util.format(reason))),
);
describe("add", function () {
it("should add a generated credential with expiry", () => {
const options = {
developerEmail,
appName: entityName,
expiry: "60m",
apiProducts: [apiProducts[0]],
};
return org.appcredentials
.add(options)
.then((result) => {
//console.log(JSON.stringify(result));
})
.catch((reason) => {
console.log(reason.error);
assert.fail("should not be reached");
});
});
it("should add a generated credential with no expiry", () => {
const options = {
developerEmail,
appName: entityName,
apiProducts: [apiProducts[0]],
};
return org.appcredentials
.add(options)
.then((result) => {
//console.log(JSON.stringify(result));
})
.catch((reason) => {
console.log(reason.error);
assert.fail("should not be reached");
});
});
it("should add a provided credential", () => {
const options = {
developerEmail,
appName: entityName,
apiProducts: [apiProducts[0]],
consumerKey: testCredential,
};
return org.appcredentials
.add(options)
.then((result) => {
//console.log(JSON.stringify(result));
})
.catch((reason) => {
console.log(reason.error);
assert.fail("should not be reached");
});
});
it("should fail to add - no app name", () => {
const options = {
developerEmail,
//appName : entityName,
apiProducts: [apiProducts[0]],
consumerKey: faker.lorem.word() + faker.random.number(),
};
return org.appcredentials
.add(options)
.then((result) => {
assert.fail("should not be reached");
})
.catch((error) => {
//console.log(reason.error);
assert.equal(error, "Error: bad status: 404");
});
});
});
describe("get", function () {
it("should get details for an existing credential", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey: testCredential,
};
return org.appcredentials.get(options).then((result) => {
//console.log(JSON.stringify(result, null, 2));
assert.equal(result.consumerKey, testCredential);
assert.isNotNull(result.apiProducts);
assert.equal(result.apiProducts[0].apiproduct, apiProducts[0]); // from test setup
assert.isNotNull(result.attributes);
assert.isNotNull(result.expiresAt);
});
});
it("should fail to get details for a non-existing credential", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey: faker.lorem.word() + faker.random.number(), // DNE
};
return org.appcredentials
.get(options)
.then((result) => {
assert.fail("should not be reached");
})
.catch((error) => {
assert.equal(error, "Error: bad status: 404");
});
});
});
describe("find", function () {
it("should find a previously added credential", () => {
const options = {
consumerKey: testCredential,
};
return org.appcredentials.find(options).then((result) => {
assert.equal(result.key, testCredential);
});
// .catch( reason => {
// console.log(reason.error);
// assert.fail('should not be reached');
// });
});
it("should not find a non-existent credential", () => {
const options = {
consumerKey: faker.lorem.word() + faker.random.number(),
};
return org.appcredentials
.find(options)
.then((result) => {
assert.equal(typeof result, "undefined");
})
.catch((reason) => {
assert.fail("should not be reached");
});
});
it("should fail to find when no key specified", () => {
const options = { something: "nothing" };
return org.appcredentials
.find(options)
.then((result) => {
assert.fail("should not be reached");
})
.catch((error) => {
assert.equal(error, "Error: missing key");
});
});
});
describe("products", function () {
// it('should list products on an existing credential?', () => {
// const options = {
// developerEmail,
// consumerKey : testCredential
// };
// return org.appcredentials.listProducts(options)
// .then( result => {
// assert.equal(result.key, testCredential);
// });
// // .catch( reason => {
// // console.log(reason.error);
// // assert.fail('should not be reached');
// // });
// });
it("should add a product to an existing credential", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey: testCredential,
product: apiProducts[1],
};
return org.appcredentials.addProduct(options).then((result) => {
//console.log(JSON.stringify(result, null, 2));
assert.equal(result.consumerKey, testCredential);
assert.equal(result.apiProducts.length, 2);
assert.equal(result.apiProducts[0].apiproduct, apiProducts[0]); // from test setup
assert.equal(result.apiProducts[1].apiproduct, apiProducts[1]);
});
// .catch( reason => {
// console.log(reason.error);
// assert.fail('should not be reached');
// });
});
it("should fail to add a product when credential does not exist", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey:
"xxx-" + faker.lorem.word() + "-" + faker.random.number(), // DNE
product: apiProducts[1],
};
return org.appcredentials
.addProduct(options)
.then((_result) => {
assert.fail("should not be reached");
})
.catch((error) => {
assert.equal(error, "Error: bad status: 404");
});
});
it("should remove a product from a credential", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey: testCredential,
product: apiProducts[1],
};
return org.appcredentials
.removeProduct(options)
.then((result) => {
//console.log(JSON.stringify(result, null, 2));
assert.equal(result.consumerKey, testCredential);
assert.equal(result.apiProducts.length, 1);
assert.equal(result.apiProducts[0].apiproduct, apiProducts[0]); // from test setup
})
.catch((error) => {
console.log(util.format(error));
assert.fail("should not be reached");
});
});
it("should fail to remove a product that is not currently on a credential", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey: testCredential,
product: apiProducts[2],
};
return org.appcredentials
.removeProduct(options)
.then((result) => {
console.log(JSON.stringify(result, null, 2));
assert.fail("should not be reached");
})
.catch((error) => {
//let util = require('util');
//console.log(util.format(error));
assert.equal(error, "Error: bad status: 500");
assert.equal(
error.result.message,
"APIProduct is not associated with consumer key",
);
});
});
});
describe("update", function () {
it("should update an existing credential", () => {
const attr1 = faker.lorem.word() + "-" + faker.lorem.word(),
attr2 = faker.lorem.word() + "-" + faker.lorem.word(),
options = {
developerEmail,
appName: entityName,
consumerKey: testCredential,
attributes: { attr1, attr2 },
};
return org.appcredentials
.update(options)
.then((result) => {
//console.log(JSON.stringify(result));
assert.isNotNull(result.attributes);
assert.equal(result.attributes.length, 2);
assert.equal(result.attributes[0].value, attr1);
assert.equal(result.attributes[1].value, attr2);
})
.catch((error) => {
console.log(util.format(error));
assert.fail("should not be reached");
});
});
it("should fail to update a non-existing credential", () => {
const attr1 = faker.lorem.word() + "-" + faker.lorem.word(),
attr2 = faker.lorem.word() + "-" + faker.lorem.word(),
fakeCredential =
faker.lorem.word() +
"-" +
faker.lorem.word() +
"-" +
faker.random.number(), // DNE
options = {
developerEmail,
appName: entityName,
consumerKey: fakeCredential,
attributes: { attr1, attr2 },
};
return org.appcredentials
.update(options)
.then((result) => {
console.log(JSON.stringify(result, null, 2));
assert.fail("should not be reached");
})
.catch((error) => {
// let util = require('util');
// console.log(util.format(error));
assert.equal(error, "Error: bad status: 404");
assert.equal(
error.result.message,
//"API_KEY_NOT_FOUND" // MINT
"Invalid consumer key for Given App",
);
});
});
});
describe("del", function () {
it("should delete a previously added credential", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey: testCredential,
};
return org.appcredentials
.del(options)
.then((result) => {
//console.log(JSON.stringify(result));
assert.equal(result.consumerKey, testCredential);
})
.catch((error) => {
console.log(util.format(error));
assert.fail("should not be reached");
});
});
it("should fail to delete when not specifying a credential", () => {
const options = {
developerEmail,
appName: entityName,
//consumerKey : faker.lorem.word() + faker.random.number()
};
return org.appcredentials
.del(options)
.then((_result) => {
assert.fail("should not be reached");
})
.catch((error) => {
assert.equal(error, "Error: missing appName or key");
});
});
it("should fail to delete a non-existent credential", () => {
const options = {
developerEmail,
appName: entityName,
consumerKey: faker.lorem.word() + faker.random.number(),
};
return org.appcredentials
.del(options)
.then((_result) => {
assert.fail("should not be reached");
})
.catch((error) => {
assert.equal(error, "Error: bad status: 404");
assert.equal(
error.result.code,
"keymanagement.service.InvalidClientIdForGivenApp",
);
});
});
it("should fail to delete a credential on non-existing devapp", () => {
const options = {
developerEmail,
appName: faker.lorem.word(),
consumerKey: faker.lorem.word() + faker.random.number(),
};
return org.appcredentials
.del(options)
.then((_result) => {
assert.fail("should not be reached");
})
.catch((error) => {
assert.equal(error, "Error: bad status: 404");
assert.equal(
error.result.code,
"developer.service.AppDoesNotExist",
);
});
});
});
});
});