aboutsummaryrefslogtreecommitdiffstats
path: root/keystone-moon/keystone/tests/unit/test_v2_controller.py
blob: 6cf8bc53afe3901c82d50f1d6533ee88f1dbaff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright 2014 IBM Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


import copy
import uuid

from testtools import matchers

from keystone.assignment import controllers as assignment_controllers
from keystone import exception
from keystone.resource import controllers as resource_controllers
from keystone.tests import unit
from keystone.tests.unit import default_fixtures
from keystone.tests.unit.ksfixtures import database


_ADMIN_CONTEXT = {'is_admin': True, 'query_string': {}}


class TenantTestCase(unit.TestCase):
    """Tests for the V2 Tenant controller.

    These tests exercise :class:`keystone.assignment.controllers.Tenant`.

    """

    def setUp(self):
        super(TenantTestCase, self).setUp()
        self.useFixture(database.Database())
        self.load_backends()
        self.load_fixtures(default_fixtures)
        self.tenant_controller = resource_controllers.Tenant()
        self.assignment_tenant_controller = (
            assignment_controllers.TenantAssignment())
        self.assignment_role_controller = (
            assignment_controllers.RoleAssignmentV2())

    def test_get_project_users_no_user(self):
        """get_project_users when user doesn't exist.

        When a user that's not known to `identity` has a role on a project,
        then `get_project_users` just skips that user.

        """
        project_id = self.tenant_bar['id']

        orig_project_users = (
            self.assignment_tenant_controller.get_project_users(_ADMIN_CONTEXT,
                                                                project_id))

        # Assign a role to a user that doesn't exist to the `bar` project.

        user_id = uuid.uuid4().hex
        self.assignment_role_controller.add_role_to_user(
            _ADMIN_CONTEXT, user_id, self.role_other['id'], project_id)

        new_project_users = (
            self.assignment_tenant_controller.get_project_users(_ADMIN_CONTEXT,
                                                                project_id))

        # The new user isn't included in the result, so no change.
        # asserting that the expected values appear in the list,
        # without asserting the order of the results
        self.assertEqual(sorted(orig_project_users), sorted(new_project_users))

    def test_list_projects_default_domain(self):
        """Test that list projects only returns those in the default domain."""
        domain = unit.new_domain_ref()
        self.resource_api.create_domain(domain['id'], domain)
        project1 = unit.new_project_ref(domain_id=domain['id'])
        self.resource_api.create_project(project1['id'], project1)
        # Check the real total number of projects, we should have the:
        # - tenants in the default fixtures
        # - the project representing the default domain
        # - the project representing the domain we created above
        # - the project we created above
        refs = self.resource_api.list_projects()
        self.assertThat(
            refs, matchers.HasLength(len(default_fixtures.TENANTS) + 3))

        # Now list all projects using the v2 API - we should only get
        # back those in the default features, since only those are in the
        # default domain.
        refs = self.tenant_controller.get_all_projects(_ADMIN_CONTEXT)
        self.assertEqual(len(default_fixtures.TENANTS), len(refs['tenants']))
        for tenant in default_fixtures.TENANTS:
            tenant_copy = tenant.copy()
            tenant_copy.pop('domain_id')
            tenant_copy.pop('parent_id')
            tenant_copy.pop('is_domain')
            self.assertIn(tenant_copy, refs['tenants'])

    def _create_is_domain_project(self):
        project = unit.new_project_ref(is_domain=True)
        project_ref = self.resource_api.create_project(project['id'], project)
        return self.tenant_controller.v3_to_v2_project(project_ref)

    def test_get_is_domain_project_not_found(self):
        """Test that get project does not return is_domain projects."""
        project = self._create_is_domain_project()

        context = copy.deepcopy(_ADMIN_CONTEXT)
        context['query_string']['name'] = project['name']

        self.assertRaises(
            exception.ProjectNotFound,
            self.tenant_controller.get_all_projects,
            context)

        context = copy.deepcopy(_ADMIN_CONTEXT)
        context['query_string']['name'] = project['id']

        self.assertRaises(
            exception.ProjectNotFound,
            self.tenant_controller.get_all_projects,
            context)

    def test_create_is_domain_project_fails(self):
        """Test that the creation of a project acting as a domain fails."""
        project = {'name': uuid.uuid4().hex, 'domain_id': 'default',
                   'is_domain': True}

        self.assertRaises(
            exception.ValidationError,
            self.tenant_controller.create_project,
            _ADMIN_CONTEXT,
            project)

    def test_create_project_passing_is_domain_false_fails(self):
        """Test that passing is_domain=False is not allowed."""
        project = {'name': uuid.uuid4().hex, 'domain_id': 'default',
                   'is_domain': False}

        self.assertRaises(
            exception.ValidationError,
            self.tenant_controller.create_project,
            _ADMIN_CONTEXT,
            project)

    def test_update_is_domain_project_not_found(self):
        """Test that update is_domain project is not allowed in v2."""
        project = self._create_is_domain_project()

        project['name'] = uuid.uuid4().hex
        self.assertRaises(
            exception.ProjectNotFound,
            self.tenant_controller.update_project,
            _ADMIN_CONTEXT,
            project['id'],
            project)

    def test_delete_is_domain_project_not_found(self):
        """Test that delete is_domain project is not allowed in v2."""
        project = self._create_is_domain_project()

        self.assertRaises(
            exception.ProjectNotFound,
            self.tenant_controller.delete_project,
            _ADMIN_CONTEXT,
            project['id'])

    def test_list_is_domain_project_not_found(self):
        """Test v2 get_all_projects having projects that act as a domain.

        In v2 no project with the is_domain flag enabled should be returned.
        """
        project1 = self._create_is_domain_project()
        project2 = self._create_is_domain_project()

        refs = self.tenant_controller.get_all_projects(_ADMIN_CONTEXT)
        projects = refs.get('tenants')

        self.assertNotIn(project1, projects)
        self.assertNotIn(project2, projects)