1# Copyright 2012 OpenStack Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7#      http://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import uuid
16
17from keystoneclient import exceptions
18from keystoneclient.tests.unit.v3 import utils
19from keystoneclient.v3 import roles
20from testtools import matchers
21
22
23class RoleTests(utils.ClientTestCase, utils.CrudTests):
24    def setUp(self):
25        super(RoleTests, self).setUp()
26        self.key = 'role'
27        self.collection_key = 'roles'
28        self.model = roles.Role
29        self.manager = self.client.roles
30
31    def new_ref(self, **kwargs):
32        kwargs = super(RoleTests, self).new_ref(**kwargs)
33        kwargs.setdefault('name', uuid.uuid4().hex)
34        return kwargs
35
36    def _new_domain_ref(self, **kwargs):
37        kwargs.setdefault('enabled', True)
38        kwargs.setdefault('name', uuid.uuid4().hex)
39        return kwargs
40
41    def test_create_with_domain_id(self):
42        ref = self.new_ref()
43        ref['domain_id'] = uuid.uuid4().hex
44        self.test_create(ref=ref)
45
46    def test_create_with_domain(self):
47        ref = self.new_ref()
48        domain_ref = self._new_domain_ref()
49        domain_ref['id'] = uuid.uuid4().hex
50        ref['domain_id'] = domain_ref['id']
51
52        self.stub_entity('POST', entity=ref, status_code=201)
53        returned = self.manager.create(name=ref['name'],
54                                       domain=domain_ref)
55        self.assertIsInstance(returned, self.model)
56        for attr in ref:
57            self.assertEqual(
58                getattr(returned, attr),
59                ref[attr],
60                'Expected different %s' % attr)
61
62    def test_domain_role_grant(self):
63        user_id = uuid.uuid4().hex
64        domain_id = uuid.uuid4().hex
65        ref = self.new_ref()
66
67        self.stub_url('PUT',
68                      ['domains', domain_id, 'users', user_id,
69                       self.collection_key, ref['id']],
70                      status_code=201)
71
72        self.manager.grant(role=ref['id'], domain=domain_id, user=user_id)
73
74    def test_domain_role_grant_inherited(self):
75        user_id = uuid.uuid4().hex
76        domain_id = uuid.uuid4().hex
77        ref = self.new_ref()
78
79        self.stub_url('PUT',
80                      ['OS-INHERIT', 'domains', domain_id, 'users', user_id,
81                       self.collection_key, ref['id'],
82                       'inherited_to_projects'],
83                      status_code=201)
84
85        self.manager.grant(role=ref['id'], domain=domain_id, user=user_id,
86                           os_inherit_extension_inherited=True)
87
88    def test_project_role_grant_inherited(self):
89        user_id = uuid.uuid4().hex
90        project_id = uuid.uuid4().hex
91        ref = self.new_ref()
92
93        self.stub_url('PUT',
94                      ['OS-INHERIT', 'projects', project_id, 'users', user_id,
95                       self.collection_key, ref['id'],
96                       'inherited_to_projects'],
97                      status_code=204)
98
99        self.manager.grant(role=ref['id'], project=project_id, user=user_id,
100                           os_inherit_extension_inherited=True)
101
102    def test_domain_group_role_grant(self):
103        group_id = uuid.uuid4().hex
104        domain_id = uuid.uuid4().hex
105        ref = self.new_ref()
106
107        self.stub_url('PUT',
108                      ['domains', domain_id, 'groups', group_id,
109                       self.collection_key, ref['id']],
110                      status_code=201)
111
112        self.manager.grant(role=ref['id'], domain=domain_id, group=group_id)
113
114    def test_domain_group_role_grant_inherited(self):
115        group_id = uuid.uuid4().hex
116        domain_id = uuid.uuid4().hex
117        ref = self.new_ref()
118
119        self.stub_url('PUT',
120                      ['OS-INHERIT', 'domains', domain_id, 'groups', group_id,
121                       self.collection_key, ref['id'],
122                       'inherited_to_projects'],
123                      status_code=201)
124
125        self.manager.grant(role=ref['id'], domain=domain_id, group=group_id,
126                           os_inherit_extension_inherited=True)
127
128    def test_project_group_role_grant_inherited(self):
129        group_id = uuid.uuid4().hex
130        project_id = uuid.uuid4().hex
131        ref = self.new_ref()
132
133        self.stub_url('PUT',
134                      ['OS-INHERIT', 'projects', project_id, 'groups',
135                       group_id, self.collection_key, ref['id'],
136                       'inherited_to_projects'],
137                      status_code=204)
138
139        self.manager.grant(role=ref['id'], project=project_id, group=group_id,
140                           os_inherit_extension_inherited=True)
141
142    def test_domain_role_list(self):
143        user_id = uuid.uuid4().hex
144        domain_id = uuid.uuid4().hex
145        ref_list = [self.new_ref(), self.new_ref()]
146
147        self.stub_entity('GET',
148                         ['domains', domain_id, 'users', user_id,
149                          self.collection_key], entity=ref_list)
150
151        self.manager.list(domain=domain_id, user=user_id)
152
153    def test_domain_role_list_inherited(self):
154        user_id = uuid.uuid4().hex
155        domain_id = uuid.uuid4().hex
156        ref_list = [self.new_ref(), self.new_ref()]
157
158        self.stub_entity('GET',
159                         ['OS-INHERIT',
160                          'domains', domain_id, 'users', user_id,
161                          self.collection_key, 'inherited_to_projects'],
162                         entity=ref_list)
163
164        returned_list = self.manager.list(domain=domain_id, user=user_id,
165                                          os_inherit_extension_inherited=True)
166
167        self.assertThat(ref_list, matchers.HasLength(len(returned_list)))
168        [self.assertIsInstance(r, self.model) for r in returned_list]
169
170    def test_project_user_role_list_inherited(self):
171        user_id = uuid.uuid4().hex
172        project_id = uuid.uuid4().hex
173        ref_list = [self.new_ref(), self.new_ref()]
174
175        self.stub_entity('GET',
176                         ['OS-INHERIT',
177                          'projects', project_id, 'users', user_id,
178                          self.collection_key, 'inherited_to_projects'],
179                         entity=ref_list)
180
181        returned_list = self.manager.list(project=project_id, user=user_id,
182                                          os_inherit_extension_inherited=True)
183
184        self.assertThat(ref_list, matchers.HasLength(len(returned_list)))
185        [self.assertIsInstance(r, self.model) for r in returned_list]
186
187    def test_domain_group_role_list(self):
188        group_id = uuid.uuid4().hex
189        domain_id = uuid.uuid4().hex
190        ref_list = [self.new_ref(), self.new_ref()]
191
192        self.stub_entity('GET',
193                         ['domains', domain_id, 'groups', group_id,
194                          self.collection_key], entity=ref_list)
195
196        self.manager.list(domain=domain_id, group=group_id)
197
198    def test_domain_group_role_list_inherited(self):
199        group_id = uuid.uuid4().hex
200        domain_id = uuid.uuid4().hex
201        ref_list = [self.new_ref(), self.new_ref()]
202
203        self.stub_entity('GET',
204                         ['OS-INHERIT',
205                          'domains', domain_id, 'groups', group_id,
206                          self.collection_key, 'inherited_to_projects'],
207                         entity=ref_list)
208
209        returned_list = self.manager.list(domain=domain_id, group=group_id,
210                                          os_inherit_extension_inherited=True)
211
212        self.assertThat(ref_list, matchers.HasLength(len(returned_list)))
213        [self.assertIsInstance(r, self.model) for r in returned_list]
214
215    def test_project_group_role_list_inherited(self):
216        group_id = uuid.uuid4().hex
217        project_id = uuid.uuid4().hex
218        ref_list = [self.new_ref(), self.new_ref()]
219
220        self.stub_entity('GET',
221                         ['OS-INHERIT',
222                          'projects', project_id, 'groups', group_id,
223                          self.collection_key, 'inherited_to_projects'],
224                         entity=ref_list)
225
226        returned_list = self.manager.list(project=project_id, group=group_id,
227                                          os_inherit_extension_inherited=True)
228
229        self.assertThat(ref_list, matchers.HasLength(len(returned_list)))
230        [self.assertIsInstance(r, self.model) for r in returned_list]
231
232    def test_domain_role_check(self):
233        user_id = uuid.uuid4().hex
234        domain_id = uuid.uuid4().hex
235        ref = self.new_ref()
236
237        self.stub_url('HEAD',
238                      ['domains', domain_id, 'users', user_id,
239                       self.collection_key, ref['id']],
240                      status_code=204)
241
242        self.manager.check(role=ref['id'], domain=domain_id,
243                           user=user_id)
244
245    def test_domain_role_check_inherited(self):
246        user_id = uuid.uuid4().hex
247        domain_id = uuid.uuid4().hex
248        ref = self.new_ref()
249
250        self.stub_url('HEAD',
251                      ['OS-INHERIT',
252                       'domains', domain_id, 'users', user_id,
253                       self.collection_key, ref['id'],
254                       'inherited_to_projects'],
255                      status_code=204)
256
257        self.manager.check(role=ref['id'], domain=domain_id,
258                           user=user_id, os_inherit_extension_inherited=True)
259
260    def test_project_role_check_inherited(self):
261        user_id = uuid.uuid4().hex
262        project_id = uuid.uuid4().hex
263        ref = self.new_ref()
264
265        self.stub_url('HEAD',
266                      ['OS-INHERIT',
267                       'projects', project_id, 'users', user_id,
268                       self.collection_key, ref['id'],
269                       'inherited_to_projects'],
270                      status_code=204)
271
272        self.manager.check(role=ref['id'], project=project_id,
273                           user=user_id, os_inherit_extension_inherited=True)
274
275    def test_domain_group_role_check(self):
276        return
277        group_id = uuid.uuid4().hex
278        domain_id = uuid.uuid4().hex
279        ref = self.new_ref()
280
281        self.stub_url('HEAD',
282                      ['domains', domain_id, 'groups', group_id,
283                       self.collection_key, ref['id']],
284                      status_code=204)
285
286        self.manager.check(role=ref['id'], domain=domain_id, group=group_id)
287
288    def test_domain_group_role_check_inherited(self):
289        group_id = uuid.uuid4().hex
290        domain_id = uuid.uuid4().hex
291        ref = self.new_ref()
292
293        self.stub_url('HEAD',
294                      ['OS-INHERIT',
295                       'domains', domain_id, 'groups', group_id,
296                       self.collection_key, ref['id'],
297                       'inherited_to_projects'],
298                      status_code=204)
299
300        self.manager.check(role=ref['id'], domain=domain_id,
301                           group=group_id, os_inherit_extension_inherited=True)
302
303    def test_project_group_role_check_inherited(self):
304        group_id = uuid.uuid4().hex
305        project_id = uuid.uuid4().hex
306        ref = self.new_ref()
307
308        self.stub_url('HEAD',
309                      ['OS-INHERIT',
310                       'projects', project_id, 'groups', group_id,
311                       self.collection_key, ref['id'],
312                       'inherited_to_projects'],
313                      status_code=204)
314
315        self.manager.check(role=ref['id'], project=project_id,
316                           group=group_id, os_inherit_extension_inherited=True)
317
318    def test_domain_role_revoke(self):
319        user_id = uuid.uuid4().hex
320        domain_id = uuid.uuid4().hex
321        ref = self.new_ref()
322
323        self.stub_url('DELETE',
324                      ['domains', domain_id, 'users', user_id,
325                       self.collection_key, ref['id']],
326                      status_code=204)
327
328        self.manager.revoke(role=ref['id'], domain=domain_id, user=user_id)
329
330    def test_domain_group_role_revoke(self):
331        group_id = uuid.uuid4().hex
332        domain_id = uuid.uuid4().hex
333        ref = self.new_ref()
334
335        self.stub_url('DELETE',
336                      ['domains', domain_id, 'groups', group_id,
337                       self.collection_key, ref['id']],
338                      status_code=204)
339
340        self.manager.revoke(role=ref['id'], domain=domain_id, group=group_id)
341
342    def test_domain_role_revoke_inherited(self):
343        user_id = uuid.uuid4().hex
344        domain_id = uuid.uuid4().hex
345        ref = self.new_ref()
346
347        self.stub_url('DELETE',
348                      ['OS-INHERIT', 'domains', domain_id, 'users', user_id,
349                       self.collection_key, ref['id'],
350                       'inherited_to_projects'],
351                      status_code=204)
352
353        self.manager.revoke(role=ref['id'], domain=domain_id,
354                            user=user_id, os_inherit_extension_inherited=True)
355
356    def test_project_role_revoke_inherited(self):
357        user_id = uuid.uuid4().hex
358        project_id = uuid.uuid4().hex
359        ref = self.new_ref()
360
361        self.stub_url('DELETE',
362                      ['OS-INHERIT', 'projects', project_id, 'users', user_id,
363                       self.collection_key, ref['id'],
364                       'inherited_to_projects'],
365                      status_code=204)
366
367        self.manager.revoke(role=ref['id'], project=project_id,
368                            user=user_id, os_inherit_extension_inherited=True)
369
370    def test_domain_group_role_revoke_inherited(self):
371        group_id = uuid.uuid4().hex
372        domain_id = uuid.uuid4().hex
373        ref = self.new_ref()
374
375        self.stub_url('DELETE',
376                      ['OS-INHERIT', 'domains', domain_id, 'groups', group_id,
377                       self.collection_key, ref['id'],
378                       'inherited_to_projects'],
379                      status_code=200)
380
381        self.manager.revoke(role=ref['id'], domain=domain_id,
382                            group=group_id,
383                            os_inherit_extension_inherited=True)
384
385    def test_project_group_role_revoke_inherited(self):
386        group_id = uuid.uuid4().hex
387        project_id = uuid.uuid4().hex
388        ref = self.new_ref()
389
390        self.stub_url('DELETE',
391                      ['OS-INHERIT', 'projects', project_id, 'groups',
392                       group_id, self.collection_key, ref['id'],
393                       'inherited_to_projects'],
394                      status_code=204)
395
396        self.manager.revoke(role=ref['id'], project=project_id,
397                            group=group_id,
398                            os_inherit_extension_inherited=True)
399
400    def test_project_role_grant(self):
401        user_id = uuid.uuid4().hex
402        project_id = uuid.uuid4().hex
403        ref = self.new_ref()
404
405        self.stub_url('PUT',
406                      ['projects', project_id, 'users', user_id,
407                       self.collection_key, ref['id']],
408                      status_code=201)
409
410        self.manager.grant(role=ref['id'], project=project_id, user=user_id)
411
412    def test_project_group_role_grant(self):
413        group_id = uuid.uuid4().hex
414        project_id = uuid.uuid4().hex
415        ref = self.new_ref()
416
417        self.stub_url('PUT',
418                      ['projects', project_id, 'groups', group_id,
419                       self.collection_key, ref['id']],
420                      status_code=201)
421
422        self.manager.grant(role=ref['id'], project=project_id, group=group_id)
423
424    def test_project_role_list(self):
425        user_id = uuid.uuid4().hex
426        project_id = uuid.uuid4().hex
427        ref_list = [self.new_ref(), self.new_ref()]
428
429        self.stub_entity('GET',
430                         ['projects', project_id, 'users', user_id,
431                          self.collection_key], entity=ref_list)
432
433        self.manager.list(project=project_id, user=user_id)
434
435    def test_project_group_role_list(self):
436        group_id = uuid.uuid4().hex
437        project_id = uuid.uuid4().hex
438        ref_list = [self.new_ref(), self.new_ref()]
439
440        self.stub_entity('GET',
441                         ['projects', project_id, 'groups', group_id,
442                          self.collection_key], entity=ref_list)
443
444        self.manager.list(project=project_id, group=group_id)
445
446    def test_project_role_check(self):
447        user_id = uuid.uuid4().hex
448        project_id = uuid.uuid4().hex
449        ref = self.new_ref()
450
451        self.stub_url('HEAD',
452                      ['projects', project_id, 'users', user_id,
453                       self.collection_key, ref['id']],
454                      status_code=200)
455
456        self.manager.check(role=ref['id'], project=project_id, user=user_id)
457
458    def test_project_group_role_check(self):
459        group_id = uuid.uuid4().hex
460        project_id = uuid.uuid4().hex
461        ref = self.new_ref()
462
463        self.stub_url('HEAD',
464                      ['projects', project_id, 'groups', group_id,
465                       self.collection_key, ref['id']],
466                      status_code=200)
467
468        self.manager.check(role=ref['id'], project=project_id, group=group_id)
469
470    def test_project_role_revoke(self):
471        user_id = uuid.uuid4().hex
472        project_id = uuid.uuid4().hex
473        ref = self.new_ref()
474
475        self.stub_url('DELETE',
476                      ['projects', project_id, 'users', user_id,
477                       self.collection_key, ref['id']],
478                      status_code=204)
479
480        self.manager.revoke(role=ref['id'], project=project_id, user=user_id)
481
482    def test_project_group_role_revoke(self):
483        group_id = uuid.uuid4().hex
484        project_id = uuid.uuid4().hex
485        ref = self.new_ref()
486
487        self.stub_url('DELETE',
488                      ['projects', project_id, 'groups', group_id,
489                       self.collection_key, ref['id']],
490                      status_code=204)
491
492        self.manager.revoke(role=ref['id'], project=project_id, group=group_id)
493
494    def test_domain_project_role_grant_fails(self):
495        user_id = uuid.uuid4().hex
496        project_id = uuid.uuid4().hex
497        domain_id = uuid.uuid4().hex
498        ref = self.new_ref()
499
500        self.assertRaises(
501            exceptions.ValidationError,
502            self.manager.grant,
503            role=ref['id'],
504            domain=domain_id,
505            project=project_id,
506            user=user_id)
507
508    def test_domain_project_role_list_fails(self):
509        user_id = uuid.uuid4().hex
510        project_id = uuid.uuid4().hex
511        domain_id = uuid.uuid4().hex
512
513        self.assertRaises(
514            exceptions.ValidationError,
515            self.manager.list,
516            domain=domain_id,
517            project=project_id,
518            user=user_id)
519
520    def test_domain_project_role_check_fails(self):
521        user_id = uuid.uuid4().hex
522        project_id = uuid.uuid4().hex
523        domain_id = uuid.uuid4().hex
524        ref = self.new_ref()
525
526        self.assertRaises(
527            exceptions.ValidationError,
528            self.manager.check,
529            role=ref['id'],
530            domain=domain_id,
531            project=project_id,
532            user=user_id)
533
534    def test_domain_project_role_revoke_fails(self):
535        user_id = uuid.uuid4().hex
536        project_id = uuid.uuid4().hex
537        domain_id = uuid.uuid4().hex
538        ref = self.new_ref()
539
540        self.assertRaises(
541            exceptions.ValidationError,
542            self.manager.revoke,
543            role=ref['id'],
544            domain=domain_id,
545            project=project_id,
546            user=user_id)
547
548    def test_user_group_role_grant_fails(self):
549        user_id = uuid.uuid4().hex
550        group_id = uuid.uuid4().hex
551        project_id = uuid.uuid4().hex
552        ref = self.new_ref()
553
554        self.assertRaises(
555            exceptions.ValidationError,
556            self.manager.grant,
557            role=ref['id'],
558            project=project_id,
559            group=group_id,
560            user=user_id)
561
562    def test_user_group_role_list_fails(self):
563        user_id = uuid.uuid4().hex
564        group_id = uuid.uuid4().hex
565        project_id = uuid.uuid4().hex
566
567        self.assertRaises(
568            exceptions.ValidationError,
569            self.manager.list,
570            project=project_id,
571            group=group_id,
572            user=user_id)
573
574    def test_user_group_role_check_fails(self):
575        user_id = uuid.uuid4().hex
576        group_id = uuid.uuid4().hex
577        project_id = uuid.uuid4().hex
578        ref = self.new_ref()
579
580        self.assertRaises(
581            exceptions.ValidationError,
582            self.manager.check,
583            role=ref['id'],
584            project=project_id,
585            group=group_id,
586            user=user_id)
587
588    def test_user_group_role_revoke_fails(self):
589        user_id = uuid.uuid4().hex
590        group_id = uuid.uuid4().hex
591        project_id = uuid.uuid4().hex
592        ref = self.new_ref()
593
594        self.assertRaises(
595            exceptions.ValidationError,
596            self.manager.revoke,
597            role=ref['id'],
598            project=project_id,
599            group=group_id,
600            user=user_id)
601
602
603class DeprecatedImpliedRoleTests(utils.ClientTestCase):
604    def setUp(self):
605        super(DeprecatedImpliedRoleTests, self).setUp()
606        self.key = 'role'
607        self.collection_key = 'roles'
608        self.model = roles.Role
609        self.manager = self.client.roles
610
611    def test_implied_create(self):
612        prior_id = uuid.uuid4().hex
613        prior_name = uuid.uuid4().hex
614        implied_id = uuid.uuid4().hex
615        implied_name = uuid.uuid4().hex
616
617        mock_response = {
618            "role_inference": {
619                "implies": {
620                    "id": implied_id,
621                    "links": {"self": "http://host/v3/roles/%s" % implied_id},
622                    "name": implied_name
623                },
624                "prior_role": {
625                    "id": prior_id,
626                    "links": {"self": "http://host/v3/roles/%s" % prior_id},
627                    "name": prior_name
628                }
629            }
630        }
631
632        self.stub_url('PUT',
633                      ['roles', prior_id, 'implies', implied_id],
634                      json=mock_response,
635                      status_code=201)
636
637        with self.deprecations.expect_deprecations_here():
638            manager_result = self.manager.create_implied(prior_id, implied_id)
639            self.assertIsInstance(manager_result, roles.InferenceRule)
640            self.assertEqual(mock_response['role_inference']['implies'],
641                             manager_result.implies)
642            self.assertEqual(mock_response['role_inference']['prior_role'],
643                             manager_result.prior_role)
644
645
646class ImpliedRoleTests(utils.ClientTestCase, utils.CrudTests):
647    def setUp(self):
648        super(ImpliedRoleTests, self).setUp()
649        self.key = 'role_inference'
650        self.collection_key = 'role_inferences'
651        self.model = roles.InferenceRule
652        self.manager = self.client.inference_rules
653
654    def test_check(self):
655        prior_role_id = uuid.uuid4().hex
656        implied_role_id = uuid.uuid4().hex
657        self.stub_url('HEAD',
658                      ['roles', prior_role_id, 'implies', implied_role_id],
659                      status_code=204)
660
661        result = self.manager.check(prior_role_id, implied_role_id)
662        self.assertTrue(result)
663
664    def test_get(self):
665        prior_id = uuid.uuid4().hex
666        prior_name = uuid.uuid4().hex
667        implied_id = uuid.uuid4().hex
668        implied_name = uuid.uuid4().hex
669
670        mock_response = {
671            "role_inference": {
672                "implies": {
673                    "id": implied_id,
674                    "links": {"self": "http://host/v3/roles/%s" % implied_id},
675                    "name": implied_name
676                },
677                "prior_role": {
678                    "id": prior_id,
679                    "links": {"self": "http://host/v3/roles/%s" % prior_id},
680                    "name": prior_name
681                }
682            }
683        }
684
685        self.stub_url('GET',
686                      ['roles', prior_id, 'implies', implied_id],
687                      json=mock_response,
688                      status_code=200)
689
690        manager_result = self.manager.get(prior_id, implied_id)
691        self.assertIsInstance(manager_result, roles.InferenceRule)
692        self.assertEqual(mock_response['role_inference']['implies'],
693                         manager_result.implies)
694        self.assertEqual(mock_response['role_inference']['prior_role'],
695                         manager_result.prior_role)
696
697    def test_create(self):
698        prior_id = uuid.uuid4().hex
699        prior_name = uuid.uuid4().hex
700        implied_id = uuid.uuid4().hex
701        implied_name = uuid.uuid4().hex
702
703        mock_response = {
704            "role_inference": {
705                "implies": {
706                    "id": implied_id,
707                    "links": {"self": "http://host/v3/roles/%s" % implied_id},
708                    "name": implied_name
709                },
710                "prior_role": {
711                    "id": prior_id,
712                    "links": {"self": "http://host/v3/roles/%s" % prior_id},
713                    "name": prior_name
714                }
715            }
716        }
717
718        self.stub_url('PUT',
719                      ['roles', prior_id, 'implies', implied_id],
720                      json=mock_response,
721                      status_code=201)
722
723        manager_result = self.manager.create(prior_id, implied_id)
724
725        self.assertIsInstance(manager_result, roles.InferenceRule)
726        self.assertEqual(mock_response['role_inference']['implies'],
727                         manager_result.implies)
728        self.assertEqual(mock_response['role_inference']['prior_role'],
729                         manager_result.prior_role)
730
731    def test_delete(self):
732        prior_role_id = uuid.uuid4().hex
733        implied_role_id = uuid.uuid4().hex
734        self.stub_url('DELETE',
735                      ['roles', prior_role_id, 'implies', implied_role_id],
736                      status_code=204)
737
738        status, body = self.manager.delete(prior_role_id, implied_role_id)
739        self.assertEqual(204, status.status_code)
740        self.assertIsNone(body)
741
742    def test_list_role_inferences(self):
743        prior_id = uuid.uuid4().hex
744        prior_name = uuid.uuid4().hex
745        implied_id = uuid.uuid4().hex
746        implied_name = uuid.uuid4().hex
747
748        mock_response = {
749            "role_inferences": [{
750                "implies": [{
751                    "id": implied_id,
752                    "links": {"self": "http://host/v3/roles/%s" % implied_id},
753                    "name": implied_name
754                }],
755                "prior_role": {
756                    "id": prior_id,
757                    "links": {"self": "http://host/v3/roles/%s" % prior_id},
758                    "name": prior_name
759                }
760            }]
761        }
762
763        self.stub_url('GET',
764                      ['role_inferences'],
765                      json=mock_response,
766                      status_code=200)
767        manager_result = self.manager.list_inference_roles()
768        self.assertEqual(1, len(manager_result))
769        self.assertIsInstance(manager_result[0], roles.InferenceRule)
770        self.assertEqual(mock_response['role_inferences'][0]['implies'],
771                         manager_result[0].implies)
772        self.assertEqual(mock_response['role_inferences'][0]['prior_role'],
773                         manager_result[0].prior_role)
774
775    def test_list(self):
776        prior_id = uuid.uuid4().hex
777        prior_name = uuid.uuid4().hex
778        implied_id = uuid.uuid4().hex
779        implied_name = uuid.uuid4().hex
780
781        mock_response = {
782            "role_inference": {
783                "implies": [{
784                    "id": implied_id,
785                    "links": {"self": "http://host/v3/roles/%s" % implied_id},
786                    "name": implied_name
787                }],
788                "prior_role": {
789                    "id": prior_id,
790                    "links": {"self": "http://host/v3/roles/%s" % prior_id},
791                    "name": prior_name
792                }
793            },
794            "links": {"self": "http://host/v3/roles/%s/implies" % prior_id}
795        }
796
797        self.stub_url('GET',
798                      ['roles', prior_id, 'implies'],
799                      json=mock_response,
800                      status_code=200)
801
802        manager_result = self.manager.list(prior_id)
803        self.assertIsInstance(manager_result, roles.InferenceRule)
804        self.assertEqual(1, len(manager_result.implies))
805        self.assertEqual(mock_response['role_inference']['implies'],
806                         manager_result.implies)
807        self.assertEqual(mock_response['role_inference']['prior_role'],
808                         manager_result.prior_role)
809
810    def test_update(self):
811        # Update not supported for rule inferences
812        self.assertRaises(exceptions.MethodNotImplemented, self.manager.update)
813
814    def test_find(self):
815        # Find not supported for rule inferences
816        self.assertRaises(exceptions.MethodNotImplemented, self.manager.find)
817
818    def test_put(self):
819        # Put not supported for rule inferences
820        self.assertRaises(exceptions.MethodNotImplemented, self.manager.put)
821
822    def test_list_params(self):
823        # Put not supported for rule inferences
824        self.skipTest("list params not supported by rule inferences")
825