~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/tests/test_access.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-01-21 11:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110121114806-v8fvnnl6az4m4ohv
Tags: upstream-2011.1~bzr597
ImportĀ upstreamĀ versionĀ 2011.1~bzr597

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 United States Government as represented by the
 
4
# Administrator of the National Aeronautics and Space Administration.
 
5
# All Rights Reserved.
 
6
#
 
7
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
#    not use this file except in compliance with the License. You may obtain
 
9
#    a copy of the License at
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
#    Unless required by applicable law or agreed to in writing, software
 
14
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
#    License for the specific language governing permissions and limitations
 
17
#    under the License.
 
18
 
 
19
import unittest
 
20
import webob
 
21
 
 
22
from nova import context
 
23
from nova import flags
 
24
from nova import test
 
25
from nova.api import ec2
 
26
from nova.auth import manager
 
27
 
 
28
FLAGS = flags.FLAGS
 
29
 
 
30
 
 
31
class FakeControllerClass(object):
 
32
    pass
 
33
 
 
34
 
 
35
class FakeApiRequest(object):
 
36
    def __init__(self, action):
 
37
        self.controller = FakeControllerClass()
 
38
        self.action = action
 
39
 
 
40
 
 
41
class AccessTestCase(test.TestCase):
 
42
    def _env_for(self, ctxt, action):
 
43
        env = {}
 
44
        env['ec2.context'] = ctxt
 
45
        env['ec2.request'] = FakeApiRequest(action)
 
46
        return env
 
47
 
 
48
    def setUp(self):
 
49
        super(AccessTestCase, self).setUp()
 
50
        um = manager.AuthManager()
 
51
        self.context = context.get_admin_context()
 
52
        # Make test users
 
53
        self.testadmin = um.create_user('testadmin')
 
54
        self.testpmsys = um.create_user('testpmsys')
 
55
        self.testnet = um.create_user('testnet')
 
56
        self.testsys = um.create_user('testsys')
 
57
        # Assign some rules
 
58
        um.add_role('testadmin', 'cloudadmin')
 
59
        um.add_role('testpmsys', 'sysadmin')
 
60
        um.add_role('testnet', 'netadmin')
 
61
        um.add_role('testsys', 'sysadmin')
 
62
 
 
63
        # Make a test project
 
64
        self.project = um.create_project('testproj',
 
65
                                         'testpmsys',
 
66
                                         'a test project',
 
67
                                         ['testpmsys', 'testnet', 'testsys'])
 
68
        self.project.add_role(self.testnet, 'netadmin')
 
69
        self.project.add_role(self.testsys, 'sysadmin')
 
70
        #user is set in each test
 
71
 
 
72
        def noopWSGIApp(environ, start_response):
 
73
            start_response('200 OK', [])
 
74
            return ['']
 
75
 
 
76
        self.mw = ec2.Authorizer(noopWSGIApp)
 
77
        self.mw.action_roles = {'FakeControllerClass': {
 
78
                '_allow_all': ['all'],
 
79
                '_allow_none': [],
 
80
                '_allow_project_manager': ['projectmanager'],
 
81
                '_allow_sys_and_net': ['sysadmin', 'netadmin'],
 
82
                '_allow_sysadmin': ['sysadmin']}}
 
83
 
 
84
    def tearDown(self):
 
85
        um = manager.AuthManager()
 
86
        # Delete the test project
 
87
        um.delete_project('testproj')
 
88
        # Delete the test user
 
89
        um.delete_user('testadmin')
 
90
        um.delete_user('testpmsys')
 
91
        um.delete_user('testnet')
 
92
        um.delete_user('testsys')
 
93
        super(AccessTestCase, self).tearDown()
 
94
 
 
95
    def response_status(self, user, methodName):
 
96
        ctxt = context.RequestContext(user, self.project)
 
97
        environ = self._env_for(ctxt, methodName)
 
98
        req = webob.Request.blank('/', environ)
 
99
        resp = req.get_response(self.mw)
 
100
        return resp.status_int
 
101
 
 
102
    def shouldAllow(self, user, methodName):
 
103
        self.assertEqual(200, self.response_status(user, methodName))
 
104
 
 
105
    def shouldDeny(self, user, methodName):
 
106
        self.assertEqual(401, self.response_status(user, methodName))
 
107
 
 
108
    def test_001_allow_all(self):
 
109
        users = [self.testadmin, self.testpmsys, self.testnet, self.testsys]
 
110
        for user in users:
 
111
            self.shouldAllow(user, '_allow_all')
 
112
 
 
113
    def test_002_allow_none(self):
 
114
        self.shouldAllow(self.testadmin, '_allow_none')
 
115
        users = [self.testpmsys, self.testnet, self.testsys]
 
116
        for user in users:
 
117
            self.shouldDeny(user, '_allow_none')
 
118
 
 
119
    def test_003_allow_project_manager(self):
 
120
        for user in [self.testadmin, self.testpmsys]:
 
121
            self.shouldAllow(user, '_allow_project_manager')
 
122
        for user in [self.testnet, self.testsys]:
 
123
            self.shouldDeny(user, '_allow_project_manager')
 
124
 
 
125
    def test_004_allow_sys_and_net(self):
 
126
        for user in [self.testadmin, self.testnet, self.testsys]:
 
127
            self.shouldAllow(user, '_allow_sys_and_net')
 
128
        # denied because it doesn't have the per project sysadmin
 
129
        for user in [self.testpmsys]:
 
130
            self.shouldDeny(user, '_allow_sys_and_net')
 
131
 
 
132
if __name__ == "__main__":
 
133
    # TODO: Implement use_fake as an option
 
134
    unittest.main()