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

« back to all changes in this revision

Viewing changes to keystoneclient/tests/fakes.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
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
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
 
 
15
"""
 
16
A fake server that "responds" to API methods with pre-canned responses.
 
17
 
 
18
All of these responses come from the spec, so if for some reason the spec's
 
19
wrong the tests might raise AssertionError. I've indicated in comments the
 
20
places where actual behavior differs from the spec.
 
21
"""
 
22
 
 
23
from keystoneclient import access
 
24
 
 
25
 
 
26
def assert_has_keys(dict, required=[], optional=[]):
 
27
    keys = dict.keys()
 
28
    for k in required:
 
29
        try:
 
30
            assert k in keys
 
31
        except AssertionError:
 
32
            extra_keys = set(keys).difference(set(required + optional))
 
33
            raise AssertionError("found unexpected keys: %s" %
 
34
                                 list(extra_keys))
 
35
 
 
36
 
 
37
class FakeClient(object):
 
38
 
 
39
    def assert_called(self, method, url, body=None, pos=-1):
 
40
        """Assert than an API method was just called."""
 
41
        expected = (method, url)
 
42
        called = self.callstack[pos][0:2]
 
43
 
 
44
        assert self.callstack, ("Expected %s %s but no calls were made." %
 
45
                                expected)
 
46
        assert expected == called, ("Expected %s %s; got %s %s" %
 
47
                                    (expected + called))
 
48
 
 
49
        if body is not None:
 
50
            assert self.callstack[pos][2] == body
 
51
 
 
52
    def assert_called_anytime(self, method, url, body=None):
 
53
        """Assert than an API method was called anytime in the test."""
 
54
        expected = (method, url)
 
55
 
 
56
        assert self.callstack, ("Expected %s %s but no calls were made." %
 
57
                                expected)
 
58
 
 
59
        found = False
 
60
        for entry in self.callstack:
 
61
            if expected == entry[0:2]:
 
62
                found = True
 
63
                break
 
64
 
 
65
        assert found, ('Expected %s; got %s' %
 
66
                       (expected, self.callstack))
 
67
        if body is not None:
 
68
            if entry[2] != body:
 
69
                raise AssertionError('%s != %s' % (entry[2], body))
 
70
        self.callstack = []
 
71
 
 
72
    def clear_callstack(self):
 
73
        self.callstack = []
 
74
 
 
75
    def authenticate(self, cl_obj):
 
76
        cl_obj.user_id = '1'
 
77
        cl_obj.auth_user_id = '1'
 
78
        cl_obj.project_id = '1'
 
79
        cl_obj.auth_tenant_id = '1'
 
80
        cl_obj.auth_ref = access.AccessInfo.factory(None, {
 
81
            "access": {
 
82
                "token": {
 
83
                    "expires": "2012-02-05T00:00:00",
 
84
                    "id": "887665443383838",
 
85
                    "tenant": {
 
86
                        "id": "1",
 
87
                        "name": "customer-x"
 
88
                    }
 
89
                },
 
90
                "serviceCatalog": [{
 
91
                    "endpoints": [{
 
92
                        "adminURL": "http://swift.admin-nets.local:8080/",
 
93
                        "region": "RegionOne",
 
94
                        "internalURL": "http://127.0.0.1:8080/v1/AUTH_1",
 
95
                        "publicURL":
 
96
                        "http://swift.publicinternets.com/v1/AUTH_1"
 
97
                    }],
 
98
                    "type": "object-store",
 
99
                    "name": "swift"
 
100
                }, {
 
101
                    "endpoints": [{
 
102
                        "adminURL": "http://cdn.admin-nets.local/v1.1/1",
 
103
                        "region": "RegionOne",
 
104
                        "internalURL": "http://127.0.0.1:7777/v1.1/1",
 
105
                        "publicURL": "http://cdn.publicinternets.com/v1.1/1"
 
106
                    }],
 
107
                    "type": "object-store",
 
108
                    "name": "cdn"
 
109
                }],
 
110
                "user": {
 
111
                    "id": "1",
 
112
                    "roles": [{
 
113
                        "tenantId": "1",
 
114
                        "id": "3",
 
115
                        "name": "Member"
 
116
                    }],
 
117
                    "name": "joeuser"
 
118
                }
 
119
            }
 
120
        })