Skip to content

Commit cc541df

Browse files
committed
bf(VLTCLT-67): Fix validation and add unit tests
1 parent ac66d7c commit cc541df

4 files changed

Lines changed: 225 additions & 38 deletions

File tree

lib/IAMClient.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,9 @@ class VaultClient {
325325
Version: '2010-05-08',
326326
};
327327

328-
if (accountName) {
329-
assert(!this.parameterValidation || typeof accountName === 'string',
330-
'the account name, if set, should be a string');
331-
data.AccountName = accountName;
332-
}
328+
assert(!this.parameterValidation || typeof accountName === 'string',
329+
'the account name should be a string');
330+
data.AccountName = accountName;
333331
this.request('POST', '/', true, callback, data);
334332
}
335333

@@ -473,16 +471,15 @@ class VaultClient {
473471
if (key === undefined) {
474472
assert(false, 'key needs to be specified');
475473
}
476-
assert((accountArn, typeof accountArn === 'string'
477-
|| 'arn should be a string'));
478-
assert((accountName, typeof accountName === 'string'
479-
|| 'name should be a string'));
480-
assert((accountId, typeof accountId === 'string'
481-
|| 'id should be a string'));
482-
assert((canonicalId, typeof canonicalId === 'string'
483-
|| 'canonicalId should be a string'));
484-
assert((key, typeof key === 'string'
485-
|| 'key should be a string'));
474+
assert(accountArn === undefined || typeof accountArn === 'string',
475+
'arn should be a string');
476+
assert(accountName === undefined || typeof accountName === 'string',
477+
'name should be a string');
478+
assert(accountId === undefined || typeof accountId === 'string',
479+
'id should be a string');
480+
assert(canonicalId === undefined || typeof canonicalId === 'string',
481+
'canonicalId should be a string');
482+
assert(typeof key === 'string', 'key should be a string');
486483
const data = {
487484
Action: 'AddAccountAttribute',
488485
Version: '2010-05-08',
@@ -501,8 +498,7 @@ class VaultClient {
501498
data.canonicalId = canonicalId;
502499
}
503500
if (value) {
504-
assert((value, typeof value === 'string'
505-
|| 'value should be a string'));
501+
assert(typeof value === 'string', 'value should be a string');
506502
data.value = value;
507503
}
508504
this.request('POST', '/', true, callback, data);
@@ -538,16 +534,15 @@ class VaultClient {
538534
if (key === undefined) {
539535
assert(false, 'key needs to be specified');
540536
}
541-
assert((accountArn, typeof accountArn === 'string'
542-
|| 'arn should be a string'));
543-
assert((accountName, typeof accountName === 'string'
544-
|| 'name should be a string'));
545-
assert((accountId, typeof accountId === 'string'
546-
|| 'id should be a string'));
547-
assert((canonicalId, typeof canonicalId === 'string'
548-
|| 'canonicalId should be a string'));
549-
assert((key, typeof key === 'string'
550-
|| 'key should be a string'));
537+
assert(accountArn === undefined || typeof accountArn === 'string',
538+
'arn should be a string');
539+
assert(accountName === undefined || typeof accountName === 'string',
540+
'name should be a string');
541+
assert(accountId === undefined || typeof accountId === 'string',
542+
'id should be a string');
543+
assert(canonicalId === undefined || typeof canonicalId === 'string',
544+
'canonicalId should be a string');
545+
assert(typeof key === 'string', 'key should be a string');
551546
const data = {
552547
Action: 'DeleteAccountAttribute',
553548
Version: '2010-05-08',

tests/unit/addAccountAttribute.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
const assert = require('assert');
2+
const IAMClient = require('../../lib/IAMClient');
3+
4+
describe('IAMClient - addAccountAttribute', () => {
5+
let client;
6+
let lastRequestData;
7+
8+
beforeEach('stub the request method', () => {
9+
client = new IAMClient('127.0.0.1', 8500);
10+
lastRequestData = null;
11+
client.request = (method, path, iamAuthenticate, callback, data) => {
12+
lastRequestData = { method, path, iamAuthenticate, data };
13+
callback();
14+
};
15+
});
16+
17+
it('should call the request method with the correct parameters', () => {
18+
client.addAccountAttribute({
19+
accountName: 'exampleAccount',
20+
key: 'exampleKey',
21+
value: 'exampleValue',
22+
}, () => {});
23+
24+
assert.strictEqual(lastRequestData.method, 'POST');
25+
assert.strictEqual(lastRequestData.path, '/');
26+
assert.strictEqual(lastRequestData.iamAuthenticate, true);
27+
assert.deepStrictEqual(lastRequestData.data, {
28+
Action: 'AddAccountAttribute',
29+
Version: '2010-05-08',
30+
key: 'exampleKey',
31+
accountName: 'exampleAccount',
32+
value: 'exampleValue',
33+
});
34+
});
35+
36+
it('should not send the value when it is not provided', () => {
37+
client.addAccountAttribute({
38+
accountName: 'exampleAccount',
39+
key: 'exampleKey',
40+
}, () => {});
41+
42+
assert.deepStrictEqual(lastRequestData.data, {
43+
Action: 'AddAccountAttribute',
44+
Version: '2010-05-08',
45+
key: 'exampleKey',
46+
accountName: 'exampleAccount',
47+
});
48+
});
49+
50+
['accountArn', 'accountName', 'accountId', 'canonicalId'].forEach(identifier => {
51+
it(`should accept ${identifier} on its own`, () => {
52+
client.addAccountAttribute({
53+
[identifier]: 'exampleIdentifier',
54+
key: 'exampleKey',
55+
}, () => {});
56+
57+
assert.deepStrictEqual(lastRequestData.data, {
58+
Action: 'AddAccountAttribute',
59+
Version: '2010-05-08',
60+
key: 'exampleKey',
61+
[identifier]: 'exampleIdentifier',
62+
});
63+
});
64+
});
65+
66+
it('should throw an error if no account identifier is specified', () => {
67+
assert.throws(() => {
68+
client.addAccountAttribute({ key: 'exampleKey' }, () => {});
69+
}, /account-name, account-id, account-arn or canonical-id need to be specified/);
70+
});
71+
72+
it('should throw an error if key is not specified', () => {
73+
assert.throws(() => {
74+
client.addAccountAttribute({ accountName: 'exampleAccount' }, () => {});
75+
}, /key needs to be specified/);
76+
});
77+
78+
[
79+
{ identifier: 'accountArn', message: /arn should be a string/ },
80+
{ identifier: 'accountName', message: /name should be a string/ },
81+
{ identifier: 'accountId', message: /id should be a string/ },
82+
{ identifier: 'canonicalId', message: /canonicalId should be a string/ },
83+
].forEach(({ identifier, message }) => {
84+
it(`should throw an error if ${identifier} is not a string`, () => {
85+
assert.throws(() => {
86+
client.addAccountAttribute({
87+
[identifier]: 123,
88+
key: 'exampleKey',
89+
}, () => {});
90+
}, message);
91+
});
92+
});
93+
94+
it('should throw an error if key is not a string', () => {
95+
assert.throws(() => {
96+
client.addAccountAttribute({
97+
accountName: 'exampleAccount',
98+
key: 123,
99+
}, () => {});
100+
}, /key should be a string/);
101+
});
102+
103+
it('should throw an error if value is not a string', () => {
104+
assert.throws(() => {
105+
client.addAccountAttribute({
106+
accountName: 'exampleAccount',
107+
key: 'exampleKey',
108+
value: 123,
109+
}, () => {});
110+
}, /value should be a string/);
111+
});
112+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const assert = require('assert');
2+
const IAMClient = require('../../lib/IAMClient');
3+
4+
describe('IAMClient - deleteAccountAttribute', () => {
5+
let client;
6+
let lastRequestData;
7+
8+
beforeEach('stub the request method', () => {
9+
client = new IAMClient('127.0.0.1', 8500);
10+
lastRequestData = null;
11+
client.request = (method, path, iamAuthenticate, callback, data) => {
12+
lastRequestData = { method, path, iamAuthenticate, data };
13+
callback();
14+
};
15+
});
16+
17+
it('should call the request method with the correct parameters', () => {
18+
client.deleteAccountAttribute({
19+
accountName: 'exampleAccount',
20+
key: 'exampleKey',
21+
}, () => {});
22+
23+
assert.strictEqual(lastRequestData.method, 'POST');
24+
assert.strictEqual(lastRequestData.path, '/');
25+
assert.strictEqual(lastRequestData.iamAuthenticate, true);
26+
assert.deepStrictEqual(lastRequestData.data, {
27+
Action: 'DeleteAccountAttribute',
28+
Version: '2010-05-08',
29+
key: 'exampleKey',
30+
accountName: 'exampleAccount',
31+
});
32+
});
33+
34+
['accountArn', 'accountName', 'accountId', 'canonicalId'].forEach(identifier => {
35+
it(`should accept ${identifier} on its own`, () => {
36+
client.deleteAccountAttribute({
37+
[identifier]: 'exampleIdentifier',
38+
key: 'exampleKey',
39+
}, () => {});
40+
41+
assert.deepStrictEqual(lastRequestData.data, {
42+
Action: 'DeleteAccountAttribute',
43+
Version: '2010-05-08',
44+
key: 'exampleKey',
45+
[identifier]: 'exampleIdentifier',
46+
});
47+
});
48+
});
49+
50+
it('should throw an error if no account identifier is specified', () => {
51+
assert.throws(() => {
52+
client.deleteAccountAttribute({ key: 'exampleKey' }, () => {});
53+
}, /account-name, account-id, account-arn or canonical-id need to be specified/);
54+
});
55+
56+
it('should throw an error if key is not specified', () => {
57+
assert.throws(() => {
58+
client.deleteAccountAttribute({ accountName: 'exampleAccount' }, () => {});
59+
}, /key needs to be specified/);
60+
});
61+
62+
[
63+
{ identifier: 'accountArn', message: /arn should be a string/ },
64+
{ identifier: 'accountName', message: /name should be a string/ },
65+
{ identifier: 'accountId', message: /id should be a string/ },
66+
{ identifier: 'canonicalId', message: /canonicalId should be a string/ },
67+
].forEach(({ identifier, message }) => {
68+
it(`should throw an error if ${identifier} is not a string`, () => {
69+
assert.throws(() => {
70+
client.deleteAccountAttribute({
71+
[identifier]: 123,
72+
key: 'exampleKey',
73+
}, () => {});
74+
}, message);
75+
});
76+
});
77+
78+
it('should throw an error if key is not a string', () => {
79+
assert.throws(() => {
80+
client.deleteAccountAttribute({
81+
accountName: 'exampleAccount',
82+
key: 123,
83+
}, () => {});
84+
}, /key should be a string/);
85+
});
86+
});

tests/unit/deleteAccountQuota.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,18 @@ describe('IAMClient - deleteAccountQuota', () => {
4747
});
4848
});
4949

50-
it('should call the request method with the correct parameters when options are not provided', () => {
50+
it('should throw an error when accountName is not provided', () => {
5151
client = createClient(true);
52-
client.deleteAccountQuota(undefined, () => {});
53-
54-
assert.strictEqual(lastRequestData.method, 'POST');
55-
assert.strictEqual(lastRequestData.path, '/');
56-
assert.strictEqual(lastRequestData.iamAuthenticate, true);
57-
assert.deepStrictEqual(lastRequestData.data, {
58-
Action: 'DeleteAccountQuota',
59-
Version: '2010-05-08',
60-
});
52+
assert.throws(() => {
53+
client.deleteAccountQuota(undefined, () => {});
54+
}, /the account name should be a string/);
6155
});
6256

6357
it('should throw an error if accountName is not a string', () => {
6458
client = createClient(true);
6559
const accountName = 123;
6660
assert.throws(() => {
6761
client.deleteAccountQuota(accountName, () => {});
68-
}, /the account name, if set, should be a string/);
62+
}, /the account name should be a string/);
6963
});
7064
});

0 commit comments

Comments
 (0)