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

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/compute/contrib/test_accounts.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010 OpenStack LLC.
2
 
# All Rights Reserved.
3
 
#
4
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
 
#    not use this file except in compliance with the License. You may obtain
6
 
#    a copy of the License at
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
10
 
#    Unless required by applicable law or agreed to in writing, software
11
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 
#    License for the specific language governing permissions and limitations
14
 
#    under the License.
15
 
 
16
 
from lxml import etree
17
 
import webob
18
 
 
19
 
from nova.api.openstack.compute.contrib import accounts
20
 
from nova.auth import manager as auth_manager
21
 
from nova.openstack.common import jsonutils
22
 
from nova import test
23
 
from nova.tests.api.openstack import fakes
24
 
 
25
 
 
26
 
def fake_init(self):
27
 
    self.manager = fakes.FakeAuthManager()
28
 
 
29
 
 
30
 
class AccountsTest(test.TestCase):
31
 
    def setUp(self):
32
 
        super(AccountsTest, self).setUp()
33
 
        self.flags(verbose=True)
34
 
        self.stubs.Set(accounts.Controller, '__init__',
35
 
                       fake_init)
36
 
        fakes.FakeAuthManager.clear_fakes()
37
 
        fakes.FakeAuthDatabase.data = {}
38
 
        fakes.stub_out_networking(self.stubs)
39
 
        fakes.stub_out_rate_limiting(self.stubs)
40
 
 
41
 
        fakemgr = fakes.FakeAuthManager()
42
 
        joeuser = auth_manager.User('id1', 'guy1', 'acc1', 'secret1', False)
43
 
        superuser = auth_manager.User('id2', 'guy2', 'acc2', 'secret2', True)
44
 
        fakemgr.add_user(joeuser)
45
 
        fakemgr.add_user(superuser)
46
 
        fakemgr.create_project('test1', joeuser)
47
 
        fakemgr.create_project('test2', superuser)
48
 
 
49
 
    def test_get_account(self):
50
 
        req = webob.Request.blank('/v2/fake/accounts/test1')
51
 
        res = req.get_response(fakes.wsgi_app())
52
 
        res_dict = jsonutils.loads(res.body)
53
 
 
54
 
        self.assertEqual(res.status_int, 200)
55
 
        self.assertEqual(res_dict['account']['id'], 'test1')
56
 
        self.assertEqual(res_dict['account']['name'], 'test1')
57
 
        self.assertEqual(res_dict['account']['manager'], 'id1')
58
 
 
59
 
    def test_get_account_xml(self):
60
 
        req = webob.Request.blank('/v2/fake/accounts/test1.xml')
61
 
        res = req.get_response(fakes.wsgi_app())
62
 
        res_tree = etree.fromstring(res.body)
63
 
 
64
 
        self.assertEqual(res.status_int, 200)
65
 
        self.assertEqual('account', res_tree.tag)
66
 
        self.assertEqual('test1', res_tree.get('id'))
67
 
        self.assertEqual('test1', res_tree.get('name'))
68
 
        self.assertEqual('id1', res_tree.get('manager'))
69
 
 
70
 
    def test_account_delete(self):
71
 
        req = webob.Request.blank('/v2/fake/accounts/test1')
72
 
        req.method = 'DELETE'
73
 
        res = req.get_response(fakes.wsgi_app())
74
 
        self.assertTrue('test1' not in fakes.FakeAuthManager.projects)
75
 
        self.assertEqual(res.status_int, 200)
76
 
 
77
 
    def test_account_create(self):
78
 
        body = dict(account=dict(description='test account',
79
 
                                 manager='id1'))
80
 
        req = webob.Request.blank('/v2/fake/accounts/newacct')
81
 
        req.headers["Content-Type"] = "application/json"
82
 
        req.method = 'PUT'
83
 
        req.body = jsonutils.dumps(body)
84
 
 
85
 
        res = req.get_response(fakes.wsgi_app())
86
 
        res_dict = jsonutils.loads(res.body)
87
 
 
88
 
        self.assertEqual(res.status_int, 200)
89
 
        self.assertEqual(res_dict['account']['id'], 'newacct')
90
 
        self.assertEqual(res_dict['account']['name'], 'newacct')
91
 
        self.assertEqual(res_dict['account']['description'], 'test account')
92
 
        self.assertEqual(res_dict['account']['manager'], 'id1')
93
 
        self.assertTrue('newacct' in
94
 
                        fakes.FakeAuthManager.projects)
95
 
        self.assertEqual(len(fakes.FakeAuthManager.projects.values()), 3)
96
 
 
97
 
    def test_account_create_xml(self):
98
 
        body = dict(account=dict(description='test account',
99
 
                                 manager='id1'))
100
 
        req = webob.Request.blank('/v2/fake/accounts/newacct.xml')
101
 
        req.headers["Content-Type"] = "application/json"
102
 
        req.method = 'PUT'
103
 
        req.body = jsonutils.dumps(body)
104
 
 
105
 
        res = req.get_response(fakes.wsgi_app())
106
 
        res_tree = etree.fromstring(res.body)
107
 
 
108
 
        self.assertEqual(res.status_int, 200)
109
 
        self.assertEqual(res_tree.tag, 'account')
110
 
        self.assertEqual(res_tree.get('id'), 'newacct')
111
 
        self.assertEqual(res_tree.get('name'), 'newacct')
112
 
        self.assertEqual(res_tree.get('description'), 'test account')
113
 
        self.assertEqual(res_tree.get('manager'), 'id1')
114
 
        self.assertTrue('newacct' in
115
 
                        fakes.FakeAuthManager.projects)
116
 
        self.assertEqual(len(fakes.FakeAuthManager.projects.values()), 3)
117
 
 
118
 
    def test_account_update(self):
119
 
        body = dict(account=dict(description='test account',
120
 
                                 manager='id2'))
121
 
        req = webob.Request.blank('/v2/fake/accounts/test1')
122
 
        req.headers["Content-Type"] = "application/json"
123
 
        req.method = 'PUT'
124
 
        req.body = jsonutils.dumps(body)
125
 
 
126
 
        res = req.get_response(fakes.wsgi_app())
127
 
        res_dict = jsonutils.loads(res.body)
128
 
 
129
 
        self.assertEqual(res.status_int, 200)
130
 
        self.assertEqual(res_dict['account']['id'], 'test1')
131
 
        self.assertEqual(res_dict['account']['name'], 'test1')
132
 
        self.assertEqual(res_dict['account']['description'], 'test account')
133
 
        self.assertEqual(res_dict['account']['manager'], 'id2')
134
 
        self.assertEqual(len(fakes.FakeAuthManager.projects.values()), 2)
135
 
 
136
 
    def test_account_update_xml(self):
137
 
        body = dict(account=dict(description='test account',
138
 
                                 manager='id2'))
139
 
        req = webob.Request.blank('/v2/fake/accounts/test1.xml')
140
 
        req.headers["Content-Type"] = "application/json"
141
 
        req.method = 'PUT'
142
 
        req.body = jsonutils.dumps(body)
143
 
 
144
 
        res = req.get_response(fakes.wsgi_app())
145
 
        res_tree = etree.fromstring(res.body)
146
 
 
147
 
        self.assertEqual(res.status_int, 200)
148
 
        self.assertEqual(res_tree.tag, 'account')
149
 
        self.assertEqual(res_tree.get('id'), 'test1')
150
 
        self.assertEqual(res_tree.get('name'), 'test1')
151
 
        self.assertEqual(res_tree.get('description'), 'test account')
152
 
        self.assertEqual(res_tree.get('manager'), 'id2')
153
 
        self.assertEqual(len(fakes.FakeAuthManager.projects.values()), 2)