Skip to content

Commit 6c40940

Browse files
feat(bigquery): Add revokeDatasetAccess tests
1 parent 0a5f5dd commit 6c40940

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const sinon = require('sinon');
19+
const {BigQuery} = require('@google-cloud/bigquery');
20+
const {revokeDatasetAccess} = require('../src/revokeDatasetAccess');
21+
22+
describe('revokeDatasetAccess', () => {
23+
const datasetId = 'test_dataset';
24+
const entityId = '[email protected]';
25+
let sandbox;
26+
let mockBigQuery;
27+
28+
beforeEach(() => {
29+
sandbox = sinon.createSandbox();
30+
31+
// Mock the BigQuery constructor
32+
mockBigQuery = sandbox.stub(BigQuery.prototype);
33+
});
34+
35+
afterEach(() => {
36+
sandbox.restore();
37+
});
38+
39+
it('should successfully revoke access from a dataset', async () => {
40+
const initialAccess = [
41+
{
42+
role: 'READER',
43+
userByEmail: entityId,
44+
},
45+
];
46+
47+
const dataset = {
48+
id: datasetId,
49+
metadata: {
50+
access: initialAccess,
51+
},
52+
setMetadata: sandbox.stub().resolves([
53+
{
54+
metadata: {
55+
access: [],
56+
},
57+
},
58+
]),
59+
};
60+
61+
// Mock the dataset method to return our mock dataset
62+
mockBigQuery.dataset = sandbox.stub().returns({
63+
get: sandbox.stub().resolves([dataset]),
64+
});
65+
66+
// Execute revoke access
67+
const updatedAccess = await revokeDatasetAccess({
68+
datasetId,
69+
entityId,
70+
});
71+
72+
// Verify the access was revoked
73+
assert.isArray(updatedAccess);
74+
assert.isEmpty(updatedAccess);
75+
});
76+
77+
it('should handle precondition failed error', async () => {
78+
const preconditionError = new Error('Precondition Failed');
79+
preconditionError.code = 412;
80+
81+
const dataset = {
82+
id: datasetId,
83+
metadata: {
84+
access: [],
85+
},
86+
setMetadata: sandbox.stub().rejects(preconditionError),
87+
};
88+
89+
// Mock the dataset method to return our mock dataset
90+
mockBigQuery.dataset = sandbox.stub().returns({
91+
get: sandbox.stub().resolves([dataset]),
92+
});
93+
94+
try {
95+
await revokeDatasetAccess({
96+
datasetId,
97+
entityId,
98+
});
99+
assert.fail('Should have thrown an error');
100+
} catch (error) {
101+
assert.equal(error.code, 412);
102+
assert.equal(error.message, 'Precondition Failed');
103+
}
104+
});
105+
106+
it('should handle missing entity gracefully', async () => {
107+
const dataset = {
108+
id: datasetId,
109+
metadata: {
110+
access: [],
111+
},
112+
setMetadata: sandbox.stub().resolves([
113+
{
114+
metadata: {
115+
access: [],
116+
},
117+
},
118+
]),
119+
};
120+
121+
// Mock the dataset method to return our mock dataset
122+
mockBigQuery.dataset = sandbox.stub().returns({
123+
get: sandbox.stub().resolves([dataset]),
124+
});
125+
126+
const updatedAccess = await revokeDatasetAccess({
127+
datasetId,
128+
entityId,
129+
});
130+
131+
assert.isArray(updatedAccess);
132+
assert.isEmpty(updatedAccess);
133+
});
134+
});

0 commit comments

Comments
 (0)