~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
  • Date: 2013-01-18 07:44:54 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20130118074454-g7w5blpynohn1s48
Tags: 1:0.2.2-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import time
2
2
 
3
 
import httplib2
4
3
import mox
5
 
import unittest2 as unittest
 
4
import requests
 
5
import testtools
6
6
 
7
7
from keystoneclient.v2_0 import client
8
8
 
9
9
 
10
 
class TestCase(unittest.TestCase):
 
10
class TestCase(testtools.TestCase):
11
11
    TEST_TENANT_ID = '1'
12
12
    TEST_TENANT_NAME = 'aTenant'
13
13
    TEST_TOKEN = 'aToken'
16
16
    TEST_URL = '%s%s' % (TEST_ROOT_URL, 'v2.0')
17
17
    TEST_ROOT_ADMIN_URL = 'http://127.0.0.1:35357/'
18
18
    TEST_ADMIN_URL = '%s%s' % (TEST_ROOT_ADMIN_URL, 'v2.0')
 
19
    TEST_REQUEST_BASE = {
 
20
        'config': {'danger_mode': False},
 
21
        'verify': True,
 
22
    }
19
23
 
20
24
    TEST_SERVICE_CATALOG = [{
21
25
        "endpoints": [{
69
73
        self.mox = mox.Mox()
70
74
        self._original_time = time.time
71
75
        time.time = lambda: 1234
72
 
        httplib2.Http.request = self.mox.CreateMockAnything()
 
76
        requests.request = self.mox.CreateMockAnything()
73
77
        self.client = client.Client(username=self.TEST_USER,
74
78
                                    token=self.TEST_TOKEN,
75
79
                                    tenant_name=self.TEST_TENANT_NAME,
78
82
 
79
83
    def tearDown(self):
80
84
        time.time = self._original_time
 
85
        self.mox.UnsetStubs()
 
86
        self.mox.VerifyAll()
81
87
        super(TestCase, self).tearDown()
82
 
        self.mox.UnsetStubs()
83
 
        self.mox.VerifyAll()
84
 
 
85
 
 
86
 
class UnauthenticatedTestCase(unittest.TestCase):
 
88
 
 
89
 
 
90
class UnauthenticatedTestCase(testtools.TestCase):
87
91
    """ Class used as base for unauthenticated calls """
88
92
    TEST_ROOT_URL = 'http://127.0.0.1:5000/'
89
93
    TEST_URL = '%s%s' % (TEST_ROOT_URL, 'v2.0')
90
94
    TEST_ROOT_ADMIN_URL = 'http://127.0.0.1:35357/'
91
95
    TEST_ADMIN_URL = '%s%s' % (TEST_ROOT_ADMIN_URL, 'v2.0')
 
96
    TEST_REQUEST_BASE = {
 
97
        'config': {'danger_mode': False},
 
98
        'verify': True,
 
99
    }
92
100
 
93
101
    def setUp(self):
94
102
        super(UnauthenticatedTestCase, self).setUp()
95
103
        self.mox = mox.Mox()
96
104
        self._original_time = time.time
97
105
        time.time = lambda: 1234
98
 
        httplib2.Http.request = self.mox.CreateMockAnything()
 
106
        requests.request = self.mox.CreateMockAnything()
99
107
 
100
108
    def tearDown(self):
101
109
        time.time = self._original_time
 
110
        self.mox.UnsetStubs()
 
111
        self.mox.VerifyAll()
102
112
        super(UnauthenticatedTestCase, self).tearDown()
103
 
        self.mox.UnsetStubs()
104
 
        self.mox.VerifyAll()
 
113
 
 
114
 
 
115
class TestResponse(requests.Response):
 
116
    """ Class used to wrap requests.Response and provide some
 
117
        convenience to initialize with a dict """
 
118
 
 
119
    def __init__(self, data):
 
120
        self._text = None
 
121
        super(TestResponse, self)
 
122
        if isinstance(data, dict):
 
123
            self.status_code = data.get('status_code', None)
 
124
            self.headers = data.get('headers', None)
 
125
            # Fake the text attribute to streamline Response creation
 
126
            self._text = data.get('text', None)
 
127
        else:
 
128
            self.status_code = data
 
129
 
 
130
    def __eq__(self, other):
 
131
        return self.__dict__ == other.__dict__
 
132
 
 
133
    @property
 
134
    def text(self):
 
135
        return self._text