Debian Codegraph @

keystoneclient/tests/unit/test_https.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import requests
14 from unittest import mock
15
16 from keystoneclient import httpclient
17 from keystoneclient.tests.unit import utils
18
19
20 FAKE_RESPONSE = utils.test_response(json={'hi': 'there'})
21
22 REQUEST_URL = 'https://127.0.0.1:5000/hi'
23 RESPONSE_BODY = '{"hi": "there"}'
24
25
26 def get_client():
27 cl = httpclient.HTTPClient(username="username", password="password",
28 project_id="tenant", auth_url="auth_test",
29 cacert="ca.pem", cert=('cert.pem', "key.pem"))
30 return cl
31
32
33 def get_authed_client():
34 cl = get_client()
35 cl.management_url = "https://127.0.0.1:5000"
36 cl.auth_token = "token"
37 return cl
38
39
40 class ClientTest(utils.TestCase):
41
42 @mock.patch.object(requests, 'request')
43 def test_get(self, MOCK_REQUEST):
44 MOCK_REQUEST.return_value = FAKE_RESPONSE
45 # Creating a HTTPClient not using session is deprecated.
46 with self.deprecations.expect_deprecations_here():
47 cl = get_authed_client()
48
49 with self.deprecations.expect_deprecations_here():
50 resp, body = cl.get("/hi")
51
52 # this may become too tightly couple later
53 mock_args, mock_kwargs = MOCK_REQUEST.call_args
54
55 self.assertEqual(mock_args[0], 'GET')
56 self.assertEqual(mock_args[1], REQUEST_URL)
57 self.assertEqual(mock_kwargs['headers']['X-Auth-Token'], 'token')
58 self.assertEqual(mock_kwargs['cert'], ('cert.pem', 'key.pem'))
59 self.assertEqual(mock_kwargs['verify'], 'ca.pem')
60
61 # Automatic JSON parsing
62 self.assertEqual(body, {"hi": "there"})
63
64 @mock.patch.object(requests, 'request')
65 def test_post(self, MOCK_REQUEST):
66 MOCK_REQUEST.return_value = FAKE_RESPONSE
67 # Creating a HTTPClient not using session is deprecated.
68 with self.deprecations.expect_deprecations_here():
69 cl = get_authed_client()
70
71 with self.deprecations.expect_deprecations_here():
72 cl.post("/hi", body=[1, 2, 3])
73
74 # this may become too tightly couple later
75 mock_args, mock_kwargs = MOCK_REQUEST.call_args
76
77 self.assertEqual(mock_args[0], 'POST')
78 self.assertEqual(mock_args[1], REQUEST_URL)
79 self.assertEqual(mock_kwargs['data'], '[1, 2, 3]')
80 self.assertEqual(mock_kwargs['headers']['X-Auth-Token'], 'token')
81 self.assertEqual(mock_kwargs['cert'], ('cert.pem', 'key.pem'))
82 self.assertEqual(mock_kwargs['verify'], 'ca.pem')
83
84 @mock.patch.object(requests, 'request')
85 def test_post_auth(self, MOCK_REQUEST):
86 MOCK_REQUEST.return_value = FAKE_RESPONSE
87 # Creating a HTTPClient not using session is deprecated.
88 with self.deprecations.expect_deprecations_here():
89 cl = httpclient.HTTPClient(
90 username="username", password="password", project_id="tenant",
91 auth_url="auth_test", cacert="ca.pem",
92 cert=('cert.pem', 'key.pem'))
93 cl.management_url = "https://127.0.0.1:5000"
94 cl.auth_token = "token"
95 with self.deprecations.expect_deprecations_here():
96 cl.post("/hi", body=[1, 2, 3])
97
98 # this may become too tightly couple later
99 mock_args, mock_kwargs = MOCK_REQUEST.call_args
100
101 self.assertEqual(mock_args[0], 'POST')
102 self.assertEqual(mock_args[1], REQUEST_URL)
103 self.assertEqual(mock_kwargs['data'], '[1, 2, 3]')
104 self.assertEqual(mock_kwargs['headers']['X-Auth-Token'], 'token')
105 self.assertEqual(mock_kwargs['cert'], ('cert.pem', 'key.pem'))
106 self.assertEqual(mock_kwargs['verify'], 'ca.pem')