summaryrefslogtreecommitdiffstats
path: root/nova/tests/api/openstack/compute/contrib/test_server_password.py
blob: 6e7d66a0603aac5c41cdf0ca01f4eb1573f82370 (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
# Copyright 2012 Nebula, Inc.
# All Rights Reserved.
#
#    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.

from lxml import etree
from oslo.config import cfg
import webob

from nova.api.metadata import password
from nova import compute
from nova.openstack.common import jsonutils
from nova import test
from nova.tests.api.openstack import fakes


CONF = cfg.CONF
CONF.import_opt('osapi_compute_ext_list', 'nova.api.openstack.compute.contrib')


class ServerPasswordTest(test.TestCase):
    content_type = 'application/json'

    def setUp(self):
        super(ServerPasswordTest, self).setUp()
        fakes.stub_out_nw_api(self.stubs)
        self.stubs.Set(compute.api.API, 'get', lambda *a, **kw: {'uuid': ''})
        self.password = 'fakepass'

        def fake_extract_password(instance):
            return self.password

        def fake_convert_password(context, password):
            self.password = password
            return {}

        self.stubs.Set(password, 'extract_password', fake_extract_password)
        self.stubs.Set(password, 'convert_password', fake_convert_password)
        self.flags(
            osapi_compute_extension=[
                'nova.api.openstack.compute.contrib.select_extensions'],
            osapi_compute_ext_list=['Server_password'])

    def _make_request(self, url, method='GET'):
        req = webob.Request.blank(url)
        req.headers['Accept'] = self.content_type
        req.method = method
        res = req.get_response(
                fakes.wsgi_app(init_only=('servers', 'os-server-password')))
        return res

    def _get_pass(self, body):
        return jsonutils.loads(body).get('password')

    def test_get_password(self):
        url = '/v2/fake/servers/fake/os-server-password'
        res = self._make_request(url)

        self.assertEqual(res.status_int, 200)
        self.assertEqual(self._get_pass(res.body), 'fakepass')

    def test_reset_password(self):
        url = '/v2/fake/servers/fake/os-server-password'
        res = self._make_request(url, 'DELETE')
        self.assertEqual(res.status_int, 204)

        res = self._make_request(url)
        self.assertEqual(res.status_int, 200)
        self.assertEqual(self._get_pass(res.body), '')


class ServerPasswordXmlTest(ServerPasswordTest):
    content_type = 'application/xml'

    def _get_pass(self, body):
        # NOTE(vish): first element is password
        return etree.XML(body).text or ''