@@ -113,126 +113,6 @@ def test_build_api_url_w_base_url_override(self):
113113 self .assertEqual (conn .build_api_url ('/foo' , api_base_url = base_url2 ),
114114 URI )
115115
116- def _verify_uri (self , uri , expected_path , ** expected_qs ):
117- from six .moves .urllib import parse
118- klass = self ._getTargetClass ()
119- scheme , netloc , path , query , _ = parse .urlsplit (uri )
120- self .assertEqual ('%s://%s' % (scheme , netloc ), klass .API_BASE_URL )
121- self .assertEqual (path , '/%s/%s' % (klass .API_VERSION , expected_path ))
122- qs = dict (parse .parse_qsl (query ))
123- self .assertEqual (qs , expected_qs )
124-
125- def test_get_iam_policy (self ):
126- import json
127- from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
128- PATH = '%s:getIamPolicy' % (self .TOPIC_PATH ,)
129- 130- OWNER2 = 'group:[email protected] ' 131- EDITOR1 = 'domain:google.com'
132- EDITOR2 = 'user:[email protected] ' 133- VIEWER1 = 'serviceAccount:[email protected] ' 134- VIEWER2 = 'user:[email protected] ' 135- RETURNED = {
136- 'etag' : 'DEADBEEF' ,
137- 'version' : 17 ,
138- 'bindings' : [
139- {'role' : OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
140- {'role' : EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
141- {'role' : VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
142- ],
143- }
144- HEADERS = {
145- 'status' : '200' ,
146- 'content-type' : 'application/json' ,
147- }
148- http = _Http (HEADERS , json .dumps (RETURNED ))
149- conn = self ._makeOne (http = http )
150-
151- policy = conn .get_iam_policy (self .TOPIC_PATH )
152-
153- self .assertEqual (policy , RETURNED )
154- self .assertEqual (http ._called_with ['method' ], 'GET' )
155- self ._verify_uri (http ._called_with ['uri' ], PATH )
156- self .assertEqual (http ._called_with ['body' ], None )
157-
158- def test_set_iam_policy (self ):
159- import json
160- from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
161- PATH = '%s:setIamPolicy' % (self .TOPIC_PATH ,)
162- 163- OWNER2 = 'group:[email protected] ' 164- EDITOR1 = 'domain:google.com'
165- EDITOR2 = 'user:[email protected] ' 166- VIEWER1 = 'serviceAccount:[email protected] ' 167- VIEWER2 = 'user:[email protected] ' 168- POLICY = {
169- 'etag' : 'DEADBEEF' ,
170- 'version' : 17 ,
171- 'bindings' : [
172- {'role' : OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
173- {'role' : EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
174- {'role' : VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
175- ],
176- }
177- RETURNED = POLICY .copy ()
178- HEADERS = {
179- 'status' : '200' ,
180- 'content-type' : 'application/json' ,
181- }
182- http = _Http (HEADERS , json .dumps (RETURNED ))
183- conn = self ._makeOne (http = http )
184-
185- policy = conn .set_iam_policy (self .TOPIC_PATH , POLICY )
186-
187- self .assertEqual (policy , RETURNED )
188- self .assertEqual (http ._called_with ['method' ], 'POST' )
189- self ._verify_uri (http ._called_with ['uri' ], PATH )
190- self .assertEqual (http ._called_with ['body' ],
191- json .dumps ({'policy' : POLICY }))
192-
193- def test_test_iam_permissions (self ):
194- import json
195- from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
196- PATH = '%s:testIamPermissions' % (self .TOPIC_PATH ,)
197- ALL_ROLES = [OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE ]
198- ALLOWED = ALL_ROLES [1 :]
199- RETURNED = {'permissions' : ALLOWED }
200- HEADERS = {
201- 'status' : '200' ,
202- 'content-type' : 'application/json' ,
203- }
204- http = _Http (HEADERS , json .dumps (RETURNED ))
205- conn = self ._makeOne (http = http )
206-
207- allowed = conn .test_iam_permissions (self .TOPIC_PATH , ALL_ROLES )
208-
209- self .assertEqual (allowed , ALLOWED )
210- self .assertEqual (http ._called_with ['method' ], 'POST' )
211- self ._verify_uri (http ._called_with ['uri' ], PATH )
212- self .assertEqual (http ._called_with ['body' ],
213- json .dumps ({'permissions' : ALL_ROLES }))
214-
215- def test_test_iam_permissions_missing_key (self ):
216- import json
217- from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
218- PATH = '%s:testIamPermissions' % (self .TOPIC_PATH ,)
219- ALL_ROLES = [OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE ]
220- RETURNED = {}
221- HEADERS = {
222- 'status' : '200' ,
223- 'content-type' : 'application/json' ,
224- }
225- http = _Http (HEADERS , json .dumps (RETURNED ))
226- conn = self ._makeOne (http = http )
227-
228- allowed = conn .test_iam_permissions (self .TOPIC_PATH , ALL_ROLES )
229-
230- self .assertEqual (allowed , [])
231- self .assertEqual (http ._called_with ['method' ], 'POST' )
232- self ._verify_uri (http ._called_with ['uri' ], PATH )
233- self .assertEqual (http ._called_with ['body' ],
234- json .dumps ({'permissions' : ALL_ROLES }))
235-
236116
237117class Test_PublisherAPI (_Base ):
238118
@@ -691,19 +571,95 @@ def test_ctor(self):
691571 api = self ._makeOne (connection )
692572 self .assertTrue (api ._connection is connection )
693573
574+ def test_get_iam_policy (self ):
575+ from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
576+ 577+ OWNER2 = 'group:[email protected] ' 578+ EDITOR1 = 'domain:google.com'
579+ EDITOR2 = 'user:[email protected] ' 580+ VIEWER1 = 'serviceAccount:[email protected] ' 581+ VIEWER2 = 'user:[email protected] ' 582+ RETURNED = {
583+ 'etag' : 'DEADBEEF' ,
584+ 'version' : 17 ,
585+ 'bindings' : [
586+ {'role' : OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
587+ {'role' : EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
588+ {'role' : VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
589+ ],
590+ }
591+ connection = _Connection (RETURNED )
592+ api = self ._makeOne (connection )
593+
594+ policy = api .get_iam_policy (self .TOPIC_PATH )
694595
695- class _Http (object ):
596+ self .assertEqual (policy , RETURNED )
597+ self .assertEqual (connection ._called_with ['method' ], 'GET' )
598+ path = '/%s:getIamPolicy' % (self .TOPIC_PATH ,)
599+ self .assertEqual (connection ._called_with ['path' ], path )
696600
697- _called_with = None
601+ def test_set_iam_policy (self ):
602+ from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
603+ 604+ OWNER2 = 'group:[email protected] ' 605+ EDITOR1 = 'domain:google.com'
606+ EDITOR2 = 'user:[email protected] ' 607+ VIEWER1 = 'serviceAccount:[email protected] ' 608+ VIEWER2 = 'user:[email protected] ' 609+ POLICY = {
610+ 'etag' : 'DEADBEEF' ,
611+ 'version' : 17 ,
612+ 'bindings' : [
613+ {'role' : OWNER_ROLE , 'members' : [OWNER1 , OWNER2 ]},
614+ {'role' : EDITOR_ROLE , 'members' : [EDITOR1 , EDITOR2 ]},
615+ {'role' : VIEWER_ROLE , 'members' : [VIEWER1 , VIEWER2 ]},
616+ ],
617+ }
618+ RETURNED = POLICY .copy ()
619+ connection = _Connection (RETURNED )
620+ api = self ._makeOne (connection )
698621
699- def __init__ (self , headers , content ):
700- from httplib2 import Response
701- self ._response = Response (headers )
702- self ._content = content
622+ policy = api .set_iam_policy (self .TOPIC_PATH , POLICY )
703623
704- def request (self , ** kw ):
705- self ._called_with = kw
706- return self ._response , self ._content
624+ self .assertEqual (policy , RETURNED )
625+ self .assertEqual (connection ._called_with ['method' ], 'POST' )
626+ path = '/%s:setIamPolicy' % (self .TOPIC_PATH ,)
627+ self .assertEqual (connection ._called_with ['path' ], path )
628+ self .assertEqual (connection ._called_with ['data' ],
629+ {'policy' : POLICY })
630+
631+ def test_test_iam_permissions (self ):
632+ from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
633+ ALL_ROLES = [OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE ]
634+ ALLOWED = ALL_ROLES [1 :]
635+ RETURNED = {'permissions' : ALLOWED }
636+ connection = _Connection (RETURNED )
637+ api = self ._makeOne (connection )
638+
639+ allowed = api .test_iam_permissions (self .TOPIC_PATH , ALL_ROLES )
640+
641+ self .assertEqual (allowed , ALLOWED )
642+ self .assertEqual (connection ._called_with ['method' ], 'POST' )
643+ path = '/%s:testIamPermissions' % (self .TOPIC_PATH ,)
644+ self .assertEqual (connection ._called_with ['path' ], path )
645+ self .assertEqual (connection ._called_with ['data' ],
646+ {'permissions' : ALL_ROLES })
647+
648+ def test_test_iam_permissions_missing_key (self ):
649+ from gcloud .pubsub .iam import OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE
650+ ALL_ROLES = [OWNER_ROLE , EDITOR_ROLE , VIEWER_ROLE ]
651+ RETURNED = {}
652+ connection = _Connection (RETURNED )
653+ api = self ._makeOne (connection )
654+
655+ allowed = api .test_iam_permissions (self .TOPIC_PATH , ALL_ROLES )
656+
657+ self .assertEqual (allowed , [])
658+ self .assertEqual (connection ._called_with ['method' ], 'POST' )
659+ path = '/%s:testIamPermissions' % (self .TOPIC_PATH ,)
660+ self .assertEqual (connection ._called_with ['path' ], path )
661+ self .assertEqual (connection ._called_with ['data' ],
662+ {'permissions' : ALL_ROLES })
707663
708664
709665class _Connection (object ):
0 commit comments