1+ const { expect } = require ( "chai" ) ;
2+ const sinon = require ( "sinon" ) ;
3+ const { BigQuery } = require ( "@google-cloud/bigquery" ) ;
4+ const { revokeTableOrViewAccess } = require ( "../src/revokeTableOrViewAccess" ) ;
5+
6+ describe ( "revokeTableOrViewAccess" , ( ) => {
7+ let bigQueryStub ;
8+ let tableStub ;
9+ let constructorStub ;
10+
11+ beforeEach ( ( ) => {
12+ // Create stubs for BigQuery client and its methods
13+ tableStub = {
14+ iam : {
15+ getPolicy : sinon . stub ( ) ,
16+ setPolicy : sinon . stub ( ) ,
17+ } ,
18+ } ;
19+
20+ const datasetStub = {
21+ table : sinon . stub ( ) . returns ( tableStub ) ,
22+ } ;
23+
24+ bigQueryStub = {
25+ dataset : sinon . stub ( ) . returns ( datasetStub ) ,
26+ } ;
27+
28+ // Stub the BigQuery constructor correctly
29+ constructorStub = sinon . stub ( BigQuery . prototype , "constructor" ) ;
30+ constructorStub . returns ( bigQueryStub ) ;
31+
32+ // Stub the BigQuery class itself
33+ sinon . stub ( BigQuery . prototype , "dataset" ) . returns ( datasetStub ) ;
34+ } ) ;
35+
36+ afterEach ( ( ) => {
37+ sinon . restore ( ) ;
38+ } ) ;
39+
40+ it ( "should revoke access for a specific member and role" , async ( ) => {
41+ // Setup test data
42+ const testPolicy = {
43+ bindings : [
44+ {
45+ role : "roles/bigquery.dataViewer" ,
46+ 47+ } ,
48+ ] ,
49+ } ;
50+
51+ const expectedNewPolicy = {
52+ bindings : [
53+ {
54+ role : "roles/bigquery.dataViewer" ,
55+ members :
[ "group:[email protected] " ] , 56+ } ,
57+ ] ,
58+ } ;
59+
60+ // Configure stubs
61+ tableStub . iam . getPolicy . resolves ( [ testPolicy ] ) ;
62+ tableStub . iam . setPolicy . resolves ( [ ] ) ;
63+
64+ // Test the function
65+ await revokeTableOrViewAccess ( {
66+ projectId : "test-project" ,
67+ datasetId : "test-dataset" ,
68+ resourceId : "test-table" ,
69+ memberToRevoke :
"group:[email protected] " , 70+ roleToRevoke : "roles/bigquery.dataViewer" ,
71+ } ) ;
72+
73+ // Verify the new policy was set correctly
74+ sinon . assert . calledWith (
75+ tableStub . iam . setPolicy ,
76+ sinon . match ( expectedNewPolicy )
77+ ) ;
78+ } ) ;
79+
80+ it ( "should revoke all access for a specific role" , async ( ) => {
81+ // Setup test data
82+ const testPolicy = {
83+ bindings : [
84+ {
85+ role : "roles/bigquery.dataViewer" ,
86+ members :
[ "group:[email protected] " ] , 87+ } ,
88+ {
89+ role : "roles/bigquery.dataEditor" ,
90+ members :
[ "group:[email protected] " ] , 91+ } ,
92+ ] ,
93+ } ;
94+
95+ const expectedNewPolicy = {
96+ bindings : [
97+ {
98+ role : "roles/bigquery.dataEditor" ,
99+ members :
[ "group:[email protected] " ] , 100+ } ,
101+ ] ,
102+ } ;
103+
104+ // Configure stubs
105+ tableStub . iam . getPolicy . resolves ( [ testPolicy ] ) ;
106+ tableStub . iam . setPolicy . resolves ( [ ] ) ;
107+
108+ // Test the function
109+ await revokeTableOrViewAccess ( {
110+ projectId : "test-project" ,
111+ datasetId : "test-dataset" ,
112+ resourceId : "test-table" ,
113+ roleToRevoke : "roles/bigquery.dataViewer" ,
114+ } ) ;
115+
116+ // Verify the new policy was set correctly
117+ sinon . assert . calledWith (
118+ tableStub . iam . setPolicy ,
119+ sinon . match ( expectedNewPolicy )
120+ ) ;
121+ } ) ;
122+
123+ it ( "should handle errors appropriately" , async ( ) => {
124+ // Configure stub to throw an error
125+ tableStub . iam . getPolicy . rejects ( new Error ( "Test error" ) ) ;
126+
127+ // Test the function
128+ try {
129+ await revokeTableOrViewAccess ( {
130+ projectId : "test-project" ,
131+ datasetId : "test-dataset" ,
132+ resourceId : "test-table" ,
133+ } ) ;
134+ expect . fail ( "Should have thrown an error" ) ;
135+ } catch ( error ) {
136+ expect ( error . message ) . to . equal ( "Test error" ) ;
137+ }
138+ } ) ;
139+ } ) ;
0 commit comments