blob: f3c744007d1492a0b6bbbd61a1610cd21c658f40 [file] [log] [blame]
Matthew Treinisha33037e2013-12-05 23:16:39 +00001# Copyright 2013 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import httplib2
16
17from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000018from tempest import config
Matthew Treinisha33037e2013-12-05 23:16:39 +000019from tempest import exceptions
20from tempest.openstack.common.fixture import mockpatch
21from tempest.tests import base
22from tempest.tests import fake_config
23from tempest.tests import fake_http
24
25
26class BaseRestClientTestClass(base.TestCase):
27
28 def _set_token(self):
29 self.rest_client.token = 'fake token'
30
31 def setUp(self):
32 super(BaseRestClientTestClass, self).setUp()
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakeConfig)
34 self.rest_client = rest_client.RestClient('fake_user', 'fake_pass',
Matthew Treinisha33037e2013-12-05 23:16:39 +000035 'http://fake_url/v2.0')
36 self.stubs.Set(httplib2.Http, 'request', self.fake_http.request)
37 self.useFixture(mockpatch.PatchObject(self.rest_client, '_set_auth',
38 side_effect=self._set_token()))
39 self.useFixture(mockpatch.PatchObject(self.rest_client,
40 '_log_response'))
41
42
43class TestRestClientHTTPMethods(BaseRestClientTestClass):
44 def setUp(self):
45 self.fake_http = fake_http.fake_httplib2()
46 super(TestRestClientHTTPMethods, self).setUp()
47 self.useFixture(mockpatch.PatchObject(self.rest_client,
48 '_error_checker'))
49
50 def test_post(self):
51 __, return_dict = self.rest_client.post('fake_endpoint', {},
52 {})
53 self.assertEqual('POST', return_dict['method'])
54
55 def test_get(self):
56 __, return_dict = self.rest_client.get('fake_endpoint')
57 self.assertEqual('GET', return_dict['method'])
58
59 def test_delete(self):
60 __, return_dict = self.rest_client.delete('fake_endpoint')
61 self.assertEqual('DELETE', return_dict['method'])
62
63 def test_patch(self):
64 __, return_dict = self.rest_client.patch('fake_endpoint', {},
65 {})
66 self.assertEqual('PATCH', return_dict['method'])
67
68 def test_put(self):
69 __, return_dict = self.rest_client.put('fake_endpoint', {},
70 {})
71 self.assertEqual('PUT', return_dict['method'])
72
73 def test_head(self):
74 self.useFixture(mockpatch.PatchObject(self.rest_client,
75 'response_checker'))
76 __, return_dict = self.rest_client.head('fake_endpoint')
77 self.assertEqual('HEAD', return_dict['method'])
78
79 def test_copy(self):
80 __, return_dict = self.rest_client.copy('fake_endpoint')
81 self.assertEqual('COPY', return_dict['method'])
82
83
84class TestRestClientNotFoundHandling(BaseRestClientTestClass):
85 def setUp(self):
86 self.fake_http = fake_http.fake_httplib2(404)
87 super(TestRestClientNotFoundHandling, self).setUp()
88
89 def test_post(self):
90 self.assertRaises(exceptions.NotFound, self.rest_client.post,
91 'fake_endpoint', {}, {})