~ubuntu-branches/ubuntu/trusty/python-keystoneclient/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/test_https.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Adam Gandelman, Chuck Short
  • Date: 2013-11-14 10:51:32 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20131114105132-p1o428l7fclasv9e
Tags: 1:0.4.1-0ubuntu1
[ Adam Gandelman ]
* debian/patches: Refreshed.
* debian/patches/use-mox-dependency.patch: Use mox instead of mox3
  dependency.

[ Chuck Short ]
* New upstream release.
* debian/control:
  - open icehouse release.
  - Dropped python-d2to1 and python-httplib2 dependency.
* debian/patches/skip-tests-ubuntu.patch: Dropped no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import copy
2
 
import mock
3
 
 
4
 
import requests
5
 
 
6
 
from keystoneclient import httpclient
7
 
from tests import utils
8
 
 
9
 
 
10
 
FAKE_RESPONSE = utils.TestResponse({
11
 
    "status_code": 200,
12
 
    "text": '{"hi": "there"}',
13
 
})
14
 
MOCK_REQUEST = mock.Mock(return_value=(FAKE_RESPONSE))
15
 
 
16
 
 
17
 
def get_client():
18
 
    cl = httpclient.HTTPClient(username="username", password="password",
19
 
                               tenant_id="tenant", auth_url="auth_test",
20
 
                               cacert="ca.pem", key="key.pem", cert="cert.pem")
21
 
    return cl
22
 
 
23
 
 
24
 
def get_authed_client():
25
 
    cl = get_client()
26
 
    cl.management_url = "https://127.0.0.1:5000"
27
 
    cl.auth_token = "token"
28
 
    return cl
29
 
 
30
 
 
31
 
class ClientTest(utils.TestCase):
32
 
 
33
 
    def test_get(self):
34
 
        cl = get_authed_client()
35
 
 
36
 
        with mock.patch.object(requests, "request", MOCK_REQUEST):
37
 
            with mock.patch('time.time', mock.Mock(return_value=1234)):
38
 
                resp, body = cl.get("/hi")
39
 
                headers = {"X-Auth-Token": "token",
40
 
                           "User-Agent": httpclient.USER_AGENT}
41
 
                kwargs = copy.copy(self.TEST_REQUEST_BASE)
42
 
                kwargs['cert'] = ('cert.pem', 'key.pem')
43
 
                kwargs['verify'] = 'ca.pem'
44
 
                MOCK_REQUEST.assert_called_with(
45
 
                    "GET",
46
 
                    "https://127.0.0.1:5000/hi",
47
 
                    headers=headers,
48
 
                    **kwargs)
49
 
                # Automatic JSON parsing
50
 
                self.assertEqual(body, {"hi": "there"})
51
 
 
52
 
    def test_post(self):
53
 
        cl = get_authed_client()
54
 
 
55
 
        with mock.patch.object(requests, "request", MOCK_REQUEST):
56
 
            cl.post("/hi", body=[1, 2, 3])
57
 
            headers = {
58
 
                "X-Auth-Token": "token",
59
 
                "Content-Type": "application/json",
60
 
                "User-Agent": httpclient.USER_AGENT
61
 
            }
62
 
            kwargs = copy.copy(self.TEST_REQUEST_BASE)
63
 
            kwargs['cert'] = ('cert.pem', 'key.pem')
64
 
            kwargs['verify'] = 'ca.pem'
65
 
            MOCK_REQUEST.assert_called_with(
66
 
                "POST",
67
 
                "https://127.0.0.1:5000/hi",
68
 
                headers=headers,
69
 
                data='[1, 2, 3]',
70
 
                **kwargs)
71
 
 
72
 
    def test_post_auth(self):
73
 
        with mock.patch.object(requests, "request", MOCK_REQUEST):
74
 
            cl = httpclient.HTTPClient(
75
 
                username="username", password="password", tenant_id="tenant",
76
 
                auth_url="auth_test", cacert="ca.pem", key="key.pem",
77
 
                cert="cert.pem")
78
 
            cl.management_url = "https://127.0.0.1:5000"
79
 
            cl.auth_token = "token"
80
 
            cl.post("/hi", body=[1, 2, 3])
81
 
            headers = {
82
 
                "X-Auth-Token": "token",
83
 
                "Content-Type": "application/json",
84
 
                "User-Agent": httpclient.USER_AGENT
85
 
            }
86
 
            kwargs = copy.copy(self.TEST_REQUEST_BASE)
87
 
            kwargs['cert'] = ('cert.pem', 'key.pem')
88
 
            kwargs['verify'] = 'ca.pem'
89
 
            MOCK_REQUEST.assert_called_with(
90
 
                "POST",
91
 
                "https://127.0.0.1:5000/hi",
92
 
                headers=headers,
93
 
                data='[1, 2, 3]',
94
 
                **kwargs)