~ubuntu-branches/ubuntu/trusty/python-keystoneclient/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/v3/test_roles.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-01-18 07:44:54 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20130118074454-g7w5blpynohn1s48
Tags: 1:0.2.2-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import httplib2
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 OpenStack LLC
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
# not use this file except in compliance with the License. You may obtain
 
7
# a copy of the License at
 
8
#
 
9
#      http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
# License for the specific language governing permissions and limitations
 
15
# under the License.
 
16
 
 
17
import copy
2
18
import urlparse
3
19
import uuid
4
20
 
 
21
import requests
 
22
 
5
23
from keystoneclient import exceptions
6
24
from keystoneclient.v3 import roles
7
25
from tests.v3 import utils
25
43
        user_id = uuid.uuid4().hex
26
44
        domain_id = uuid.uuid4().hex
27
45
        ref = self.new_ref()
28
 
        resp = httplib2.Response({
29
 
            'status': 201,
30
 
            'body': '',
 
46
        resp = utils.TestResponse({
 
47
            "status_code": 201,
 
48
            "text": '',
31
49
        })
32
50
 
33
51
        method = 'PUT'
34
 
        httplib2.Http.request(
 
52
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
53
        kwargs['headers'] = self.headers[method]
 
54
        requests.request(
 
55
            method,
35
56
            urlparse.urljoin(
36
57
                self.TEST_URL,
37
58
                'v3/domains/%s/users/%s/%s/%s' % (
38
59
                    domain_id, user_id, self.collection_key, ref['id'])),
39
 
            method,
40
 
            headers=self.headers[method]) \
41
 
            .AndReturn((resp, resp['body']))
 
60
            **kwargs).AndReturn((resp))
42
61
        self.mox.ReplayAll()
43
62
 
44
63
        self.manager.grant(role=ref['id'], domain=domain_id, user=user_id)
45
64
 
 
65
    def test_domain_group_role_grant(self):
 
66
        group_id = uuid.uuid4().hex
 
67
        domain_id = uuid.uuid4().hex
 
68
        ref = self.new_ref()
 
69
        resp = utils.TestResponse({
 
70
            "status_code": 201,
 
71
            "text": '',
 
72
        })
 
73
 
 
74
        method = 'PUT'
 
75
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
76
        kwargs['headers'] = self.headers[method]
 
77
        requests.request(
 
78
            method,
 
79
            urlparse.urljoin(
 
80
                self.TEST_URL,
 
81
                'v3/domains/%s/groups/%s/%s/%s' % (
 
82
                    domain_id, group_id, self.collection_key, ref['id'])),
 
83
            **kwargs).AndReturn((resp))
 
84
        self.mox.ReplayAll()
 
85
 
 
86
        self.manager.grant(role=ref['id'], domain=domain_id, group=group_id)
 
87
 
46
88
    def test_domain_role_list(self):
47
89
        user_id = uuid.uuid4().hex
48
90
        domain_id = uuid.uuid4().hex
49
91
        ref_list = [self.new_ref(), self.new_ref()]
50
 
        resp = httplib2.Response({
51
 
            'status': 200,
52
 
            'body': self.serialize(ref_list),
 
92
        resp = utils.TestResponse({
 
93
            "status_code": 200,
 
94
            "text": self.serialize(ref_list),
53
95
        })
54
96
 
55
97
        method = 'GET'
56
 
        httplib2.Http.request(
 
98
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
99
        kwargs['headers'] = self.headers[method]
 
100
        requests.request(
 
101
            method,
57
102
            urlparse.urljoin(
58
103
                self.TEST_URL,
59
104
                'v3/domains/%s/users/%s/%s' % (
60
105
                    domain_id, user_id, self.collection_key)),
61
 
            method,
62
 
            headers=self.headers[method]) \
63
 
            .AndReturn((resp, resp['body']))
 
106
            **kwargs).AndReturn((resp))
64
107
        self.mox.ReplayAll()
65
108
 
66
109
        self.manager.list(domain=domain_id, user=user_id)
67
110
 
 
111
    def test_domain_group_role_list(self):
 
112
        group_id = uuid.uuid4().hex
 
113
        domain_id = uuid.uuid4().hex
 
114
        ref_list = [self.new_ref(), self.new_ref()]
 
115
        resp = utils.TestResponse({
 
116
            "status_code": 200,
 
117
            "text": self.serialize(ref_list),
 
118
        })
 
119
 
 
120
        method = 'GET'
 
121
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
122
        kwargs['headers'] = self.headers[method]
 
123
        requests.request(
 
124
            method,
 
125
            urlparse.urljoin(
 
126
                self.TEST_URL,
 
127
                'v3/domains/%s/groups/%s/%s' % (
 
128
                    domain_id, group_id, self.collection_key)),
 
129
            **kwargs).AndReturn((resp))
 
130
        self.mox.ReplayAll()
 
131
 
 
132
        self.manager.list(domain=domain_id, group=group_id)
 
133
 
68
134
    def test_domain_role_check(self):
69
135
        user_id = uuid.uuid4().hex
70
136
        domain_id = uuid.uuid4().hex
71
137
        ref = self.new_ref()
72
 
        resp = httplib2.Response({
73
 
            'status': 200,
74
 
            'body': '',
 
138
        resp = utils.TestResponse({
 
139
            "status_code": 204,
 
140
            "text": '',
75
141
        })
76
142
 
77
143
        method = 'HEAD'
78
 
        httplib2.Http.request(
 
144
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
145
        kwargs['headers'] = self.headers[method]
 
146
        requests.request(
 
147
            method,
79
148
            urlparse.urljoin(
80
149
                self.TEST_URL,
81
150
                'v3/domains/%s/users/%s/%s/%s' % (
82
151
                    domain_id, user_id, self.collection_key, ref['id'])),
 
152
            **kwargs).AndReturn((resp))
 
153
        self.mox.ReplayAll()
 
154
 
 
155
        self.manager.check(role=ref['id'], domain=domain_id,
 
156
                           user=user_id)
 
157
 
 
158
    def test_domain_group_role_check(self):
 
159
        return
 
160
        group_id = uuid.uuid4().hex
 
161
        domain_id = uuid.uuid4().hex
 
162
        ref = self.new_ref()
 
163
        resp = utils.TestResponse({
 
164
            "status_code": 204,
 
165
            "text": '',
 
166
        })
 
167
 
 
168
        method = 'HEAD'
 
169
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
170
        kwargs['headers'] = self.headers[method]
 
171
        requests.request(
83
172
            method,
84
 
            headers=self.headers[method]) \
85
 
            .AndReturn((resp, resp['body']))
 
173
            urlparse.urljoin(
 
174
                self.TEST_URL,
 
175
                'v3/domains/%s/groups/%s/%s/%s' % (
 
176
                    domain_id, group_id, self.collection_key, ref['id'])),
 
177
            **kwargs).AndReturn((resp))
86
178
        self.mox.ReplayAll()
87
179
 
88
 
        self.manager.check(role=ref['id'], domain=domain_id, user=user_id)
 
180
        self.manager.check(role=ref['id'], domain=domain_id, group=group_id)
89
181
 
90
182
    def test_domain_role_revoke(self):
91
183
        user_id = uuid.uuid4().hex
92
184
        domain_id = uuid.uuid4().hex
93
185
        ref = self.new_ref()
94
 
        resp = httplib2.Response({
95
 
            'status': 204,
96
 
            'body': '',
 
186
        resp = utils.TestResponse({
 
187
            "status_code": 204,
 
188
            "text": '',
97
189
        })
98
190
 
99
191
        method = 'DELETE'
100
 
        httplib2.Http.request(
 
192
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
193
        kwargs['headers'] = self.headers[method]
 
194
        requests.request(
 
195
            method,
101
196
            urlparse.urljoin(
102
197
                self.TEST_URL,
103
198
                'v3/domains/%s/users/%s/%s/%s' % (
104
199
                    domain_id, user_id, self.collection_key, ref['id'])),
105
 
            method,
106
 
            headers=self.headers[method]) \
107
 
            .AndReturn((resp, resp['body']))
 
200
            **kwargs).AndReturn((resp))
108
201
        self.mox.ReplayAll()
109
202
 
110
203
        self.manager.revoke(role=ref['id'], domain=domain_id, user=user_id)
111
204
 
 
205
    def test_domain_group_role_revoke(self):
 
206
        group_id = uuid.uuid4().hex
 
207
        domain_id = uuid.uuid4().hex
 
208
        ref = self.new_ref()
 
209
        resp = utils.TestResponse({
 
210
            "status_code": 204,
 
211
            "text": '',
 
212
        })
 
213
 
 
214
        method = 'DELETE'
 
215
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
216
        kwargs['headers'] = self.headers[method]
 
217
        requests.request(
 
218
            method,
 
219
            urlparse.urljoin(
 
220
                self.TEST_URL,
 
221
                'v3/domains/%s/groups/%s/%s/%s' % (
 
222
                    domain_id, group_id, self.collection_key, ref['id'])),
 
223
            **kwargs).AndReturn((resp))
 
224
        self.mox.ReplayAll()
 
225
 
 
226
        self.manager.revoke(role=ref['id'], domain=domain_id, group=group_id)
 
227
 
112
228
    def test_project_role_grant(self):
113
229
        user_id = uuid.uuid4().hex
114
230
        project_id = uuid.uuid4().hex
115
231
        ref = self.new_ref()
116
 
        resp = httplib2.Response({
117
 
            'status': 201,
118
 
            'body': '',
 
232
        resp = utils.TestResponse({
 
233
            "status_code": 201,
 
234
            "text": '',
119
235
        })
120
236
 
121
237
        method = 'PUT'
122
 
        httplib2.Http.request(
 
238
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
239
        kwargs['headers'] = self.headers[method]
 
240
        requests.request(
 
241
            method,
123
242
            urlparse.urljoin(
124
243
                self.TEST_URL,
125
244
                'v3/projects/%s/users/%s/%s/%s' % (
126
245
                    project_id, user_id, self.collection_key, ref['id'])),
127
 
            method,
128
 
            headers=self.headers[method]) \
129
 
            .AndReturn((resp, resp['body']))
 
246
            **kwargs).AndReturn((resp))
130
247
        self.mox.ReplayAll()
131
248
 
132
249
        self.manager.grant(role=ref['id'], project=project_id, user=user_id)
133
250
 
 
251
    def test_project_group_role_grant(self):
 
252
        group_id = uuid.uuid4().hex
 
253
        project_id = uuid.uuid4().hex
 
254
        ref = self.new_ref()
 
255
        resp = utils.TestResponse({
 
256
            "status_code": 201,
 
257
            "text": '',
 
258
        })
 
259
 
 
260
        method = 'PUT'
 
261
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
262
        kwargs['headers'] = self.headers[method]
 
263
        requests.request(
 
264
            method,
 
265
            urlparse.urljoin(
 
266
                self.TEST_URL,
 
267
                'v3/projects/%s/groups/%s/%s/%s' % (
 
268
                    project_id, group_id, self.collection_key, ref['id'])),
 
269
            **kwargs).AndReturn((resp))
 
270
        self.mox.ReplayAll()
 
271
 
 
272
        self.manager.grant(role=ref['id'], project=project_id, group=group_id)
 
273
 
134
274
    def test_project_role_list(self):
135
275
        user_id = uuid.uuid4().hex
136
276
        project_id = uuid.uuid4().hex
137
277
        ref_list = [self.new_ref(), self.new_ref()]
138
 
        resp = httplib2.Response({
139
 
            'status': 200,
140
 
            'body': self.serialize(ref_list),
 
278
        resp = utils.TestResponse({
 
279
            "status_code": 200,
 
280
            "text": self.serialize(ref_list),
141
281
        })
142
282
 
143
283
        method = 'GET'
144
 
        httplib2.Http.request(
 
284
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
285
        kwargs['headers'] = self.headers[method]
 
286
        requests.request(
 
287
            method,
145
288
            urlparse.urljoin(
146
289
                self.TEST_URL,
147
290
                'v3/projects/%s/users/%s/%s' % (
148
291
                    project_id, user_id, self.collection_key)),
149
 
            method,
150
 
            headers=self.headers[method]) \
151
 
            .AndReturn((resp, resp['body']))
 
292
            **kwargs).AndReturn((resp))
152
293
        self.mox.ReplayAll()
153
294
 
154
295
        self.manager.list(project=project_id, user=user_id)
155
296
 
 
297
    def test_project_group_role_list(self):
 
298
        group_id = uuid.uuid4().hex
 
299
        project_id = uuid.uuid4().hex
 
300
        ref_list = [self.new_ref(), self.new_ref()]
 
301
        resp = utils.TestResponse({
 
302
            "status_code": 200,
 
303
            "text": self.serialize(ref_list),
 
304
        })
 
305
 
 
306
        method = 'GET'
 
307
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
308
        kwargs['headers'] = self.headers[method]
 
309
        requests.request(
 
310
            method,
 
311
            urlparse.urljoin(
 
312
                self.TEST_URL,
 
313
                'v3/projects/%s/groups/%s/%s' % (
 
314
                    project_id, group_id, self.collection_key)),
 
315
            **kwargs).AndReturn((resp))
 
316
        self.mox.ReplayAll()
 
317
 
 
318
        self.manager.list(project=project_id, group=group_id)
 
319
 
156
320
    def test_project_role_check(self):
157
321
        user_id = uuid.uuid4().hex
158
322
        project_id = uuid.uuid4().hex
159
323
        ref = self.new_ref()
160
 
        resp = httplib2.Response({
161
 
            'status': 200,
162
 
            'body': '',
 
324
        resp = utils.TestResponse({
 
325
            "status_code": 200,
 
326
            "text": '',
163
327
        })
164
328
 
165
329
        method = 'HEAD'
166
 
        httplib2.Http.request(
 
330
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
331
        kwargs['headers'] = self.headers[method]
 
332
        requests.request(
 
333
            method,
167
334
            urlparse.urljoin(
168
335
                self.TEST_URL,
169
336
                'v3/projects/%s/users/%s/%s/%s' % (
170
337
                    project_id, user_id, self.collection_key, ref['id'])),
171
 
            method,
172
 
            headers=self.headers[method]) \
173
 
            .AndReturn((resp, resp['body']))
 
338
            **kwargs).AndReturn((resp))
174
339
        self.mox.ReplayAll()
175
340
 
176
341
        self.manager.check(role=ref['id'], project=project_id, user=user_id)
177
342
 
 
343
    def test_project_group_role_check(self):
 
344
        group_id = uuid.uuid4().hex
 
345
        project_id = uuid.uuid4().hex
 
346
        ref = self.new_ref()
 
347
        resp = utils.TestResponse({
 
348
            "status_code": 200,
 
349
            "text": '',
 
350
        })
 
351
 
 
352
        method = 'HEAD'
 
353
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
354
        kwargs['headers'] = self.headers[method]
 
355
        requests.request(
 
356
            method,
 
357
            urlparse.urljoin(
 
358
                self.TEST_URL,
 
359
                'v3/projects/%s/groups/%s/%s/%s' % (
 
360
                    project_id, group_id, self.collection_key, ref['id'])),
 
361
            **kwargs).AndReturn((resp))
 
362
        self.mox.ReplayAll()
 
363
 
 
364
        self.manager.check(role=ref['id'], project=project_id, group=group_id)
 
365
 
178
366
    def test_project_role_revoke(self):
179
367
        user_id = uuid.uuid4().hex
180
368
        project_id = uuid.uuid4().hex
181
369
        ref = self.new_ref()
182
 
        resp = httplib2.Response({
183
 
            'status': 204,
184
 
            'body': '',
 
370
        resp = utils.TestResponse({
 
371
            "status_code": 204,
 
372
            "text": '',
185
373
        })
186
374
 
187
375
        method = 'DELETE'
188
 
        httplib2.Http.request(
 
376
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
377
        kwargs['headers'] = self.headers[method]
 
378
        requests.request(
 
379
            method,
189
380
            urlparse.urljoin(
190
381
                self.TEST_URL,
191
382
                'v3/projects/%s/users/%s/%s/%s' % (
192
383
                    project_id, user_id, self.collection_key, ref['id'])),
193
 
            method,
194
 
            headers=self.headers[method]) \
195
 
            .AndReturn((resp, resp['body']))
 
384
            **kwargs).AndReturn((resp))
196
385
        self.mox.ReplayAll()
197
386
 
198
387
        self.manager.revoke(role=ref['id'], project=project_id, user=user_id)
199
388
 
 
389
    def test_project_group_role_revoke(self):
 
390
        group_id = uuid.uuid4().hex
 
391
        project_id = uuid.uuid4().hex
 
392
        ref = self.new_ref()
 
393
        resp = utils.TestResponse({
 
394
            "status_code": 204,
 
395
            "text": '',
 
396
        })
 
397
 
 
398
        method = 'DELETE'
 
399
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
400
        kwargs['headers'] = self.headers[method]
 
401
        requests.request(
 
402
            method,
 
403
            urlparse.urljoin(
 
404
                self.TEST_URL,
 
405
                'v3/projects/%s/groups/%s/%s/%s' % (
 
406
                    project_id, group_id, self.collection_key, ref['id'])),
 
407
            **kwargs).AndReturn((resp))
 
408
        self.mox.ReplayAll()
 
409
 
 
410
        self.manager.revoke(role=ref['id'], project=project_id, group=group_id)
 
411
 
200
412
    def test_domain_project_role_grant_fails(self):
201
413
        user_id = uuid.uuid4().hex
202
414
        project_id = uuid.uuid4().hex
250
462
            domain=domain_id,
251
463
            project=project_id,
252
464
            user=user_id)
 
465
 
 
466
    def test_user_group_role_grant_fails(self):
 
467
        user_id = uuid.uuid4().hex
 
468
        group_id = uuid.uuid4().hex
 
469
        project_id = uuid.uuid4().hex
 
470
        ref = self.new_ref()
 
471
 
 
472
        self.assertRaises(
 
473
            exceptions.ValidationError,
 
474
            self.manager.grant,
 
475
            role=ref['id'],
 
476
            project=project_id,
 
477
            group=group_id,
 
478
            user=user_id)
 
479
 
 
480
    def test_user_group_role_list_fails(self):
 
481
        user_id = uuid.uuid4().hex
 
482
        group_id = uuid.uuid4().hex
 
483
        project_id = uuid.uuid4().hex
 
484
 
 
485
        self.assertRaises(
 
486
            exceptions.ValidationError,
 
487
            self.manager.list,
 
488
            project=project_id,
 
489
            group=group_id,
 
490
            user=user_id)
 
491
 
 
492
    def test_user_group_role_check_fails(self):
 
493
        user_id = uuid.uuid4().hex
 
494
        group_id = uuid.uuid4().hex
 
495
        project_id = uuid.uuid4().hex
 
496
        ref = self.new_ref()
 
497
 
 
498
        self.assertRaises(
 
499
            exceptions.ValidationError,
 
500
            self.manager.check,
 
501
            role=ref['id'],
 
502
            project=project_id,
 
503
            group=group_id,
 
504
            user=user_id)
 
505
 
 
506
    def test_user_group_role_revoke_fails(self):
 
507
        user_id = uuid.uuid4().hex
 
508
        group_id = uuid.uuid4().hex
 
509
        project_id = uuid.uuid4().hex
 
510
        ref = self.new_ref()
 
511
 
 
512
        self.assertRaises(
 
513
            exceptions.ValidationError,
 
514
            self.manager.revoke,
 
515
            role=ref['id'],
 
516
            project=project_id,
 
517
            group=group_id,
 
518
            user=user_id)