Skip to content

Commit cce902d

Browse files
authored
fix: correctly handle if a user has no permissions when calling iam.testPermissions (#2140)
1 parent b2f7600 commit cce902d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/iam.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ class Iam {
470470
return;
471471
}
472472

473-
const availablePermissions = resp.permissions;
473+
const availablePermissions = Array.isArray(resp.permissions)
474+
? resp.permissions
475+
: [];
474476

475477
const permissionsHash = permissionsArray.reduce(
476478
(acc: {[index: string]: boolean}, permission) => {

test/iam.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,28 @@ describe('storage/iam', () => {
240240
);
241241
});
242242

243+
it('should return false for supplied permissions if user has no permissions', done => {
244+
const permissions = ['storage.bucket.list', 'storage.bucket.consume'];
245+
const apiResponse = {permissions: undefined};
246+
247+
iam.request_ = (reqOpts: DecorateRequestOptions, callback: Function) => {
248+
callback(null, apiResponse);
249+
};
250+
iam.testPermissions(
251+
permissions,
252+
(err: Error, permissions: Array<{}>, apiResp: {}) => {
253+
assert.ifError(err);
254+
assert.deepStrictEqual(permissions, {
255+
'storage.bucket.list': false,
256+
'storage.bucket.consume': false,
257+
});
258+
assert.strictEqual(apiResp, apiResponse);
259+
260+
done();
261+
}
262+
);
263+
});
264+
243265
it('should accept an options object', done => {
244266
const permissions = ['storage.bucket.list'];
245267
const options = {

0 commit comments

Comments
 (0)