15
15
import unittest2
16
16
17
17
18
+ class TestAccessGrant (unittest2 .TestCase ):
19
+
20
+ def _getTargetClass (self ):
21
+ from gcloud .bigquery .dataset import AccessGrant
22
+ return AccessGrant
23
+
24
+ def _makeOne (self , * args , ** kw ):
25
+ return self ._getTargetClass ()(* args , ** kw )
26
+
27
+ def test_ctor_defaults (self ):
28
+ grant = self ._makeOne ('OWNER' , 'userByEmail' , 'phred@example.com' )
29
+ self .assertEqual (grant .role , 'OWNER' )
30
+ self .assertEqual (grant .entity_type , 'userByEmail' )
31
+ self .assertEqual (grant .entity_id , 'phred@example.com' )
32
+
33
+
18
34
class TestDataset (unittest2 .TestCase ):
19
35
PROJECT = 'project'
20
36
DS_NAME = 'dataset-name'
@@ -39,6 +55,8 @@ def _setUpConstants(self):
39
55
40
56
def _makeResource (self ):
41
57
self ._setUpConstants ()
58
+ USER_EMAIL = 'phred@example.com'
59
+ GROUP_EMAIL = 'group-name@lists.example.com'
42
60
return {
43
61
'creationTime' : self .WHEN_TS * 1000 ,
44
62
'datasetReference' :
@@ -48,10 +66,32 @@ def _makeResource(self):
48
66
'lastModifiedTime' : self .WHEN_TS * 1000 ,
49
67
'location' : 'US' ,
50
68
'selfLink' : self .RESOURCE_URL ,
69
+ 'access' : [
70
+ {'role' : 'OWNER' , 'userByEmail' : USER_EMAIL },
71
+ {'role' : 'OWNER' , 'groupByEmail' : GROUP_EMAIL },
72
+ {'role' : 'WRITER' , 'specialGroup' : 'projectWriters' },
73
+ {'role' : 'READER' , 'specialGroup' : 'projectReaders' }],
51
74
}
52
75
53
- def _verifyResourceProperties (self , dataset , resource ):
76
+ def _verifyAccessGrants (self , access_grants , resource ):
77
+ r_grants = []
78
+ for r_grant in resource ['access' ]:
79
+ role = r_grant .pop ('role' )
80
+ for entity_type , entity_id in sorted (r_grant .items ()):
81
+ r_grants .append ({'role' : role ,
82
+ 'entity_type' : entity_type ,
83
+ 'entity_id' : entity_id })
84
+
85
+ self .assertEqual (len (access_grants ), len (r_grants ))
86
+ for a_grant , r_grant in zip (access_grants , r_grants ):
87
+ self .assertEqual (a_grant .role , r_grant ['role' ])
88
+ self .assertEqual (a_grant .entity_type , r_grant ['entity_type' ])
89
+ self .assertEqual (a_grant .entity_id , r_grant ['entity_id' ])
90
+
91
+ def _verifyReadonlyResourceProperties (self , dataset , resource ):
92
+
54
93
self .assertEqual (dataset .dataset_id , self .DS_ID )
94
+
55
95
if 'creationTime' in resource :
56
96
self .assertEqual (dataset .created , self .WHEN )
57
97
else :
@@ -69,12 +109,21 @@ def _verifyResourceProperties(self, dataset, resource):
69
109
else :
70
110
self .assertEqual (dataset .self_link , None )
71
111
112
+ def _verifyResourceProperties (self , dataset , resource ):
113
+
114
+ self ._verifyReadonlyResourceProperties (dataset , resource )
115
+
72
116
self .assertEqual (dataset .default_table_expiration_ms ,
73
117
resource .get ('defaultTableExpirationMs' ))
74
118
self .assertEqual (dataset .description , resource .get ('description' ))
75
119
self .assertEqual (dataset .friendly_name , resource .get ('friendlyName' ))
76
120
self .assertEqual (dataset .location , resource .get ('location' ))
77
121
122
+ if 'access' in resource :
123
+ self ._verifyAccessGrants (dataset .access_grants , resource )
124
+ else :
125
+ self .assertEqual (dataset .access_grants , [])
126
+
78
127
def test_ctor (self ):
79
128
client = _Client (self .PROJECT )
80
129
dataset = self ._makeOne (self .DS_NAME , client )
@@ -84,6 +133,7 @@ def test_ctor(self):
84
133
self .assertEqual (
85
134
dataset .path ,
86
135
'/projects/%s/datasets/%s' % (self .PROJECT , self .DS_NAME ))
136
+ self .assertEqual (dataset .access_grants , [])
87
137
88
138
self .assertEqual (dataset .created , None )
89
139
self .assertEqual (dataset .dataset_id , None )
@@ -96,6 +146,29 @@ def test_ctor(self):
96
146
self .assertEqual (dataset .friendly_name , None )
97
147
self .assertEqual (dataset .location , None )
98
148
149
+ def test_access_roles_setter_non_list (self ):
150
+ client = _Client (self .PROJECT )
151
+ dataset = self ._makeOne (self .DS_NAME , client )
152
+ with self .assertRaises (TypeError ):
153
+ dataset .access_grants = object ()
154
+
155
+ def test_access_roles_setter_invalid_field (self ):
156
+ from gcloud .bigquery .dataset import AccessGrant
157
+ client = _Client (self .PROJECT )
158
+ dataset = self ._makeOne (self .DS_NAME , client )
159
+ phred = AccessGrant ('OWNER' , 'userByEmail' , 'phred@example.com' )
160
+ with self .assertRaises (ValueError ):
161
+ dataset .access_grants = [phred , object ()]
162
+
163
+ def test_access_roles_setter (self ):
164
+ from gcloud .bigquery .dataset import AccessGrant
165
+ client = _Client (self .PROJECT )
166
+ dataset = self ._makeOne (self .DS_NAME , client )
167
+ phred = AccessGrant ('OWNER' , 'userByEmail' , 'phred@example.com' )
168
+ bharney = AccessGrant ('OWNER' , 'userByEmail' , 'bharney@example.com' )
169
+ dataset .access_grants = [phred , bharney ]
170
+ self .assertEqual (dataset .access_grants , [phred , bharney ])
171
+
99
172
def test_default_table_expiration_ms_setter_bad_value (self ):
100
173
client = _Client (self .PROJECT )
101
174
dataset = self ._makeOne (self .DS_NAME , client )
@@ -175,6 +248,41 @@ def test_from_api_repr_w_properties(self):
175
248
self .assertTrue (dataset ._client is client )
176
249
self ._verifyResourceProperties (dataset , RESOURCE )
177
250
251
+ def test__parse_access_grants_w_unknown_entity_type (self ):
252
+ USER_EMAIL = 'phred@example.com'
253
+ GROUP_EMAIL = 'group-name@lists.example.com'
254
+ RESOURCE = {
255
+ 'access' : [
256
+ {'role' : 'OWNER' , 'userByEmail' : USER_EMAIL },
257
+ {'role' : 'WRITER' , 'groupByEmail' : GROUP_EMAIL },
258
+ {'role' : 'READER' , 'specialGroup' : 'projectReaders' },
259
+ {'role' : 'READER' , 'unknown' : 'UNKNOWN' }]
260
+ }
261
+ client = _Client (self .PROJECT )
262
+ dataset = self ._makeOne (self .DS_NAME , client = client )
263
+ grants = dataset ._parse_access_grants (RESOURCE ['access' ])
264
+ self ._verifyAccessGrants (grants , RESOURCE )
265
+
266
+ def test__parse_access_grants_w_multiple_entity_types (self ):
267
+ # Hypothetical case: we don't know that the back-end will ever
268
+ # return such structures, but they are logical. See:
269
+ # https://github.com/GoogleCloudPlatform/gcloud-python/pull/1046#discussion_r36687769
270
+ USER_EMAIL = 'phred@example.com'
271
+ OTHER_EMAIL = 'bharney@example.com'
272
+ GROUP_EMAIL = 'group-name@lists.example.com'
273
+ RESOURCE = {
274
+ 'access' : [
275
+ {'role' : 'OWNER' , 'userByEmail' : USER_EMAIL },
276
+ {'role' : 'WRITER' , 'groupByEmail' : GROUP_EMAIL },
277
+ {'role' : 'READER' ,
278
+ 'specialGroup' : 'projectReaders' ,
279
+ 'userByEmail' : OTHER_EMAIL }]
280
+ }
281
+ client = _Client (self .PROJECT )
282
+ dataset = self ._makeOne (self .DS_NAME , client = client )
283
+ grants = dataset ._parse_access_grants (RESOURCE ['access' ])
284
+ self ._verifyAccessGrants (grants , RESOURCE )
285
+
178
286
def test_create_w_bound_client (self ):
179
287
PATH = 'projects/%s/datasets' % self .PROJECT
180
288
RESOURCE = self ._makeResource ()
@@ -196,7 +304,10 @@ def test_create_w_bound_client(self):
196
304
self ._verifyResourceProperties (dataset , RESOURCE )
197
305
198
306
def test_create_w_alternate_client (self ):
307
+ from gcloud .bigquery .dataset import AccessGrant
199
308
PATH = 'projects/%s/datasets' % self .PROJECT
309
+ USER_EMAIL = 'phred@example.com'
310
+ GROUP_EMAIL = 'group-name@lists.example.com'
200
311
DESCRIPTION = 'DESCRIPTION'
201
312
TITLE = 'TITLE'
202
313
RESOURCE = self ._makeResource ()
@@ -209,6 +320,11 @@ def test_create_w_alternate_client(self):
209
320
dataset = self ._makeOne (self .DS_NAME , client = CLIENT1 )
210
321
dataset .friendly_name = TITLE
211
322
dataset .description = DESCRIPTION
323
+ dataset .access_grants = [
324
+ AccessGrant ('OWNER' , 'userByEmail' , USER_EMAIL ),
325
+ AccessGrant ('OWNER' , 'groupByEmail' , GROUP_EMAIL ),
326
+ AccessGrant ('READER' , 'specialGroup' , 'projectReaders' ),
327
+ AccessGrant ('WRITER' , 'specialGroup' , 'projectWriters' )]
212
328
213
329
dataset .create (client = CLIENT2 )
214
330
@@ -222,6 +338,11 @@ def test_create_w_alternate_client(self):
222
338
{'projectId' : self .PROJECT , 'datasetId' : self .DS_NAME },
223
339
'description' : DESCRIPTION ,
224
340
'friendlyName' : TITLE ,
341
+ 'access' : [
342
+ {'role' : 'OWNER' , 'userByEmail' : USER_EMAIL },
343
+ {'role' : 'OWNER' , 'groupByEmail' : GROUP_EMAIL },
344
+ {'role' : 'READER' , 'specialGroup' : 'projectReaders' },
345
+ {'role' : 'WRITER' , 'specialGroup' : 'projectWriters' }],
225
346
}
226
347
self .assertEqual (req ['data' ], SENT )
227
348
self ._verifyResourceProperties (dataset , RESOURCE )
0 commit comments