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

« back to all changes in this revision

Viewing changes to tests/utils.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 time
2
 
 
3
 
import mock
4
 
import mox
5
 
import requests
6
 
import testtools
7
 
 
8
 
from keystoneclient.v2_0 import client
9
 
 
10
 
 
11
 
class TestCase(testtools.TestCase):
12
 
    TEST_DOMAIN_ID = '1'
13
 
    TEST_DOMAIN_NAME = 'aDomain'
14
 
    TEST_TENANT_ID = '1'
15
 
    TEST_TENANT_NAME = 'aTenant'
16
 
    TEST_TOKEN = 'aToken'
17
 
    TEST_USER = 'test'
18
 
    TEST_ROOT_URL = 'http://127.0.0.1:5000/'
19
 
    TEST_URL = '%s%s' % (TEST_ROOT_URL, 'v2.0')
20
 
    TEST_ROOT_ADMIN_URL = 'http://127.0.0.1:35357/'
21
 
    TEST_ADMIN_URL = '%s%s' % (TEST_ROOT_ADMIN_URL, 'v2.0')
22
 
    TEST_REQUEST_BASE = {
23
 
        'verify': True,
24
 
    }
25
 
 
26
 
    TEST_SERVICE_CATALOG = [{
27
 
        "endpoints": [{
28
 
            "adminURL": "http://cdn.admin-nets.local:8774/v1.0",
29
 
            "region": "RegionOne",
30
 
            "internalURL": "http://127.0.0.1:8774/v1.0",
31
 
            "publicURL": "http://cdn.admin-nets.local:8774/v1.0/"
32
 
        }],
33
 
        "type": "nova_compat",
34
 
        "name": "nova_compat"
35
 
    }, {
36
 
        "endpoints": [{
37
 
            "adminURL": "http://nova/novapi/admin",
38
 
            "region": "RegionOne",
39
 
            "internalURL": "http://nova/novapi/internal",
40
 
            "publicURL": "http://nova/novapi/public"
41
 
        }],
42
 
        "type": "compute",
43
 
        "name": "nova"
44
 
    }, {
45
 
        "endpoints": [{
46
 
            "adminURL": "http://glance/glanceapi/admin",
47
 
            "region": "RegionOne",
48
 
            "internalURL": "http://glance/glanceapi/internal",
49
 
            "publicURL": "http://glance/glanceapi/public"
50
 
        }],
51
 
        "type": "image",
52
 
        "name": "glance"
53
 
    }, {
54
 
        "endpoints": [{
55
 
            "adminURL": "http://127.0.0.1:35357/v2.0",
56
 
            "region": "RegionOne",
57
 
            "internalURL": "http://127.0.0.1:5000/v2.0",
58
 
            "publicURL": "http://127.0.0.1:5000/v2.0"
59
 
        }],
60
 
        "type": "identity",
61
 
        "name": "keystone"
62
 
    }, {
63
 
        "endpoints": [{
64
 
            "adminURL": "http://swift/swiftapi/admin",
65
 
            "region": "RegionOne",
66
 
            "internalURL": "http://swift/swiftapi/internal",
67
 
            "publicURL": "http://swift/swiftapi/public"
68
 
        }],
69
 
        "type": "object-store",
70
 
        "name": "swift"
71
 
    }]
72
 
 
73
 
    def setUp(self):
74
 
        super(TestCase, self).setUp()
75
 
        self.mox = mox.Mox()
76
 
        self.request_patcher = mock.patch.object(requests, 'request',
77
 
                                                 self.mox.CreateMockAnything())
78
 
        self.time_patcher = mock.patch.object(time, 'time',
79
 
                                              lambda: 1234)
80
 
        self.request_patcher.start()
81
 
        self.time_patcher.start()
82
 
        self.client = client.Client(username=self.TEST_USER,
83
 
                                    token=self.TEST_TOKEN,
84
 
                                    tenant_name=self.TEST_TENANT_NAME,
85
 
                                    auth_url=self.TEST_URL,
86
 
                                    endpoint=self.TEST_URL)
87
 
 
88
 
    def tearDown(self):
89
 
        self.request_patcher.stop()
90
 
        self.time_patcher.stop()
91
 
        self.mox.UnsetStubs()
92
 
        self.mox.VerifyAll()
93
 
        super(TestCase, self).tearDown()
94
 
 
95
 
 
96
 
class UnauthenticatedTestCase(testtools.TestCase):
97
 
    """Class used as base for unauthenticated calls."""
98
 
    TEST_ROOT_URL = 'http://127.0.0.1:5000/'
99
 
    TEST_URL = '%s%s' % (TEST_ROOT_URL, 'v2.0')
100
 
    TEST_ROOT_ADMIN_URL = 'http://127.0.0.1:35357/'
101
 
    TEST_ADMIN_URL = '%s%s' % (TEST_ROOT_ADMIN_URL, 'v2.0')
102
 
    TEST_REQUEST_BASE = {
103
 
        'verify': True,
104
 
    }
105
 
 
106
 
    def setUp(self):
107
 
        super(UnauthenticatedTestCase, self).setUp()
108
 
        self.mox = mox.Mox()
109
 
        self.request_patcher = mock.patch.object(requests, 'request',
110
 
                                                 self.mox.CreateMockAnything())
111
 
        self.time_patcher = mock.patch.object(time, 'time',
112
 
                                              lambda: 1234)
113
 
        self.request_patcher.start()
114
 
        self.time_patcher.start()
115
 
 
116
 
    def tearDown(self):
117
 
        self.request_patcher.stop()
118
 
        self.time_patcher.stop()
119
 
        self.mox.UnsetStubs()
120
 
        self.mox.VerifyAll()
121
 
        super(UnauthenticatedTestCase, self).tearDown()
122
 
 
123
 
 
124
 
class TestResponse(requests.Response):
125
 
    """Class used to wrap requests.Response and provide some
126
 
       convenience to initialize with a dict.
127
 
    """
128
 
 
129
 
    def __init__(self, data):
130
 
        self._text = None
131
 
        super(TestResponse, self).__init__()
132
 
        if isinstance(data, dict):
133
 
            self.status_code = data.get('status_code', None)
134
 
            headers = data.get('headers')
135
 
            if headers:
136
 
                self.headers.update(headers)
137
 
            # Fake the text attribute to streamline Response creation
138
 
            # _content is defined by requests.Response
139
 
            self._content = data.get('text', None)
140
 
        else:
141
 
            self.status_code = data
142
 
 
143
 
    def __eq__(self, other):
144
 
        return self.__dict__ == other.__dict__
145
 
 
146
 
    @property
147
 
    def text(self):
148
 
        return self.content