1
# Copyright 2010 OpenStack LLC.
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
8
# http://www.apache.org/licenses/LICENSE-2.0
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
16
from lxml import etree
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
23
from nova.tests.api.openstack import fakes
27
self.manager = fakes.FakeAuthManager()
30
class AccountsTest(test.TestCase):
32
super(AccountsTest, self).setUp()
33
self.flags(verbose=True)
34
self.stubs.Set(accounts.Controller, '__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)
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)
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)
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')
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)
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'))
70
def test_account_delete(self):
71
req = webob.Request.blank('/v2/fake/accounts/test1')
73
res = req.get_response(fakes.wsgi_app())
74
self.assertTrue('test1' not in fakes.FakeAuthManager.projects)
75
self.assertEqual(res.status_int, 200)
77
def test_account_create(self):
78
body = dict(account=dict(description='test account',
80
req = webob.Request.blank('/v2/fake/accounts/newacct')
81
req.headers["Content-Type"] = "application/json"
83
req.body = jsonutils.dumps(body)
85
res = req.get_response(fakes.wsgi_app())
86
res_dict = jsonutils.loads(res.body)
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)
97
def test_account_create_xml(self):
98
body = dict(account=dict(description='test account',
100
req = webob.Request.blank('/v2/fake/accounts/newacct.xml')
101
req.headers["Content-Type"] = "application/json"
103
req.body = jsonutils.dumps(body)
105
res = req.get_response(fakes.wsgi_app())
106
res_tree = etree.fromstring(res.body)
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)
118
def test_account_update(self):
119
body = dict(account=dict(description='test account',
121
req = webob.Request.blank('/v2/fake/accounts/test1')
122
req.headers["Content-Type"] = "application/json"
124
req.body = jsonutils.dumps(body)
126
res = req.get_response(fakes.wsgi_app())
127
res_dict = jsonutils.loads(res.body)
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)
136
def test_account_update_xml(self):
137
body = dict(account=dict(description='test account',
139
req = webob.Request.blank('/v2/fake/accounts/test1.xml')
140
req.headers["Content-Type"] = "application/json"
142
req.body = jsonutils.dumps(body)
144
res = req.get_response(fakes.wsgi_app())
145
res_tree = etree.fromstring(res.body)
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)