Skip to content

Commit

Permalink
fix: correctly handle if a user has no permissions when calling iam.t…
Browse files Browse the repository at this point in the history
…estPermissions (#2140)
  • Loading branch information
ddelgrosso1 authored Feb 6, 2023
1 parent b2f7600 commit cce902d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,9 @@ class Iam {
return;
}

const availablePermissions = resp.permissions;
const availablePermissions = Array.isArray(resp.permissions)
? resp.permissions
: [];

const permissionsHash = permissionsArray.reduce(
(acc: {[index: string]: boolean}, permission) => {
Expand Down
22 changes: 22 additions & 0 deletions test/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ describe('storage/iam', () => {
);
});

it('should return false for supplied permissions if user has no permissions', done => {
const permissions = ['storage.bucket.list', 'storage.bucket.consume'];
const apiResponse = {permissions: undefined};

iam.request_ = (reqOpts: DecorateRequestOptions, callback: Function) => {
callback(null, apiResponse);
};
iam.testPermissions(
permissions,
(err: Error, permissions: Array<{}>, apiResp: {}) => {
assert.ifError(err);
assert.deepStrictEqual(permissions, {
'storage.bucket.list': false,
'storage.bucket.consume': false,
});
assert.strictEqual(apiResp, apiResponse);

done();
}
);
});

it('should accept an options object', done => {
const permissions = ['storage.bucket.list'];
const options = {
Expand Down

0 comments on commit cce902d

Please sign in to comment.