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

« back to all changes in this revision

Viewing changes to tests/v2_0/test_roles.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
import copy
1
2
import urlparse
2
3
import json
3
4
 
4
 
import httplib2
 
5
import requests
5
6
 
6
7
from keystoneclient.v2_0 import roles
7
8
from tests import utils
46
47
                "id": 3,
47
48
            }
48
49
        }
49
 
        resp = httplib2.Response({
50
 
            "status": 200,
51
 
            "body": json.dumps(resp_body),
 
50
        resp = utils.TestResponse({
 
51
            "status_code": 200,
 
52
            "text": json.dumps(resp_body),
52
53
        })
53
54
 
54
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
55
 
                              'v2.0/OS-KSADM/roles'),
56
 
                              'POST',
57
 
                              body=json.dumps(req_body),
58
 
                              headers=self.TEST_POST_HEADERS) \
59
 
            .AndReturn((resp, resp['body']))
 
55
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
56
        kwargs['headers'] = self.TEST_POST_HEADERS
 
57
        kwargs['data'] = json.dumps(req_body)
 
58
        requests.request('POST',
 
59
                         urlparse.urljoin(self.TEST_URL,
 
60
                         'v2.0/OS-KSADM/roles'),
 
61
                         **kwargs).AndReturn((resp))
60
62
        self.mox.ReplayAll()
61
63
 
62
64
        role = self.client.roles.create(req_body['role']['name'])
65
67
        self.assertEqual(role.name, req_body['role']['name'])
66
68
 
67
69
    def test_delete(self):
68
 
        resp = httplib2.Response({
69
 
            "status": 204,
70
 
            "body": "",
 
70
        resp = utils.TestResponse({
 
71
            "status_code": 204,
 
72
            "text": "",
71
73
        })
72
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
73
 
                              'v2.0/OS-KSADM/roles/1'),
74
 
                              'DELETE',
75
 
                              headers=self.TEST_REQUEST_HEADERS) \
76
 
            .AndReturn((resp, resp['body']))
 
74
 
 
75
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
76
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
77
        requests.request('DELETE',
 
78
                         urlparse.urljoin(self.TEST_URL,
 
79
                         'v2.0/OS-KSADM/roles/1'),
 
80
                         **kwargs).AndReturn((resp))
77
81
        self.mox.ReplayAll()
78
82
 
79
83
        self.client.roles.delete(1)
80
84
 
81
85
    def test_get(self):
82
 
        resp = httplib2.Response({
83
 
            "status": 200,
84
 
            "body": json.dumps({
 
86
        resp = utils.TestResponse({
 
87
            "status_code": 200,
 
88
            "text": json.dumps({
85
89
                'role': self.TEST_ROLES['roles']['values'][0],
86
90
            }),
87
91
        })
88
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
89
 
                              'v2.0/OS-KSADM/roles/1'),
90
 
                              'GET',
91
 
                              headers=self.TEST_REQUEST_HEADERS) \
92
 
            .AndReturn((resp, resp['body']))
 
92
 
 
93
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
94
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
95
        requests.request('GET',
 
96
                         urlparse.urljoin(self.TEST_URL,
 
97
                         'v2.0/OS-KSADM/roles/1'),
 
98
                         **kwargs).AndReturn((resp))
93
99
        self.mox.ReplayAll()
94
100
 
95
101
        role = self.client.roles.get(1)
98
104
        self.assertEqual(role.name, 'admin')
99
105
 
100
106
    def test_list(self):
101
 
        resp = httplib2.Response({
102
 
            "status": 200,
103
 
            "body": json.dumps(self.TEST_ROLES),
 
107
        resp = utils.TestResponse({
 
108
            "status_code": 200,
 
109
            "text": json.dumps(self.TEST_ROLES),
104
110
        })
105
111
 
106
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
107
 
                              'v2.0/OS-KSADM/roles'),
108
 
                              'GET',
109
 
                              headers=self.TEST_REQUEST_HEADERS) \
110
 
            .AndReturn((resp, resp['body']))
 
112
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
113
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
114
        requests.request('GET',
 
115
                         urlparse.urljoin(self.TEST_URL,
 
116
                         'v2.0/OS-KSADM/roles'),
 
117
                         **kwargs).AndReturn((resp))
111
118
        self.mox.ReplayAll()
112
119
 
113
120
        role_list = self.client.roles.list()
114
121
        [self.assertTrue(isinstance(r, roles.Role)) for r in role_list]
115
122
 
116
123
    def test_roles_for_user(self):
117
 
        resp = httplib2.Response({
118
 
            "status": 200,
119
 
            "body": json.dumps(self.TEST_ROLES),
 
124
        resp = utils.TestResponse({
 
125
            "status_code": 200,
 
126
            "text": json.dumps(self.TEST_ROLES),
120
127
        })
121
128
 
122
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
123
 
                              'v2.0/users/foo/roles'),
124
 
                              'GET',
125
 
                              headers=self.TEST_REQUEST_HEADERS) \
126
 
            .AndReturn((resp, resp['body']))
 
129
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
130
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
131
        requests.request('GET',
 
132
                         urlparse.urljoin(self.TEST_URL,
 
133
                         'v2.0/users/foo/roles'),
 
134
                         **kwargs).AndReturn((resp))
127
135
        self.mox.ReplayAll()
128
136
 
129
137
        role_list = self.client.roles.roles_for_user('foo')
130
138
        [self.assertTrue(isinstance(r, roles.Role)) for r in role_list]
131
139
 
132
140
    def test_roles_for_user_tenant(self):
133
 
        resp = httplib2.Response({
134
 
            "status": 200,
135
 
            "body": json.dumps(self.TEST_ROLES),
 
141
        resp = utils.TestResponse({
 
142
            "status_code": 200,
 
143
            "text": json.dumps(self.TEST_ROLES),
136
144
        })
137
145
 
138
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
139
 
                              'v2.0/tenants/barrr/users/foo/roles'),
140
 
                              'GET',
141
 
                              headers=self.TEST_REQUEST_HEADERS) \
142
 
            .AndReturn((resp, resp['body']))
 
146
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
147
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
148
        requests.request('GET',
 
149
                         urlparse.urljoin(self.TEST_URL,
 
150
                         'v2.0/tenants/barrr/users/foo/roles'),
 
151
                         **kwargs).AndReturn((resp))
143
152
        self.mox.ReplayAll()
144
153
 
145
154
        role_list = self.client.roles.roles_for_user('foo', 'barrr')
146
155
        [self.assertTrue(isinstance(r, roles.Role)) for r in role_list]
147
156
 
148
157
    def test_add_user_role(self):
149
 
        resp = httplib2.Response({
150
 
            "status": 204,
151
 
            "body": '',
 
158
        resp = utils.TestResponse({
 
159
            "status_code": 204,
 
160
            "text": '',
152
161
        })
153
162
 
154
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
155
 
                              'v2.0/users/foo/roles/OS-KSADM/barrr'),
156
 
                              'PUT',
157
 
                              headers=self.TEST_REQUEST_HEADERS) \
158
 
            .AndReturn((resp, resp['body']))
 
163
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
164
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
165
        requests.request('PUT',
 
166
                         urlparse.urljoin(self.TEST_URL,
 
167
                         'v2.0/users/foo/roles/OS-KSADM/barrr'),
 
168
                         **kwargs).AndReturn((resp))
159
169
        self.mox.ReplayAll()
160
170
 
161
171
        self.client.roles.add_user_role('foo', 'barrr')
162
172
 
163
173
    def test_add_user_role_tenant(self):
164
 
        resp = httplib2.Response({
165
 
            "status": 204,
166
 
            "body": '',
 
174
        resp = utils.TestResponse({
 
175
            "status_code": 204,
 
176
            "text": '',
167
177
        })
168
178
 
169
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
170
 
                              'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
171
 
                              'PUT',
172
 
                              headers=self.TEST_REQUEST_HEADERS) \
173
 
            .AndReturn((resp, resp['body']))
 
179
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
180
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
181
        requests.request('PUT',
 
182
                         urlparse.urljoin(self.TEST_URL,
 
183
                         'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
 
184
                         **kwargs).AndReturn((resp))
174
185
        self.mox.ReplayAll()
175
186
 
176
187
        self.client.roles.add_user_role('foo', 'barrr', '4')
177
188
 
178
189
    def test_remove_user_role(self):
179
 
        resp = httplib2.Response({
180
 
            "status": 204,
181
 
            "body": '',
 
190
        resp = utils.TestResponse({
 
191
            "status_code": 204,
 
192
            "text": '',
182
193
        })
183
194
 
184
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
185
 
                              'v2.0/users/foo/roles/OS-KSADM/barrr'),
186
 
                              'DELETE',
187
 
                              headers=self.TEST_REQUEST_HEADERS) \
188
 
            .AndReturn((resp, resp['body']))
 
195
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
196
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
197
        requests.request('DELETE',
 
198
                         urlparse.urljoin(self.TEST_URL,
 
199
                         'v2.0/users/foo/roles/OS-KSADM/barrr'),
 
200
                         **kwargs).AndReturn((resp))
189
201
        self.mox.ReplayAll()
190
202
 
191
203
        self.client.roles.remove_user_role('foo', 'barrr')
192
204
 
193
205
    def test_remove_user_role_tenant(self):
194
 
        resp = httplib2.Response({
195
 
            "status": 204,
196
 
            "body": '',
 
206
        resp = utils.TestResponse({
 
207
            "status_code": 204,
 
208
            "text": '',
197
209
        })
198
210
 
199
 
        httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
200
 
                              'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
201
 
                              'DELETE',
202
 
                              headers=self.TEST_REQUEST_HEADERS) \
203
 
            .AndReturn((resp, resp['body']))
 
211
        kwargs = copy.copy(self.TEST_REQUEST_BASE)
 
212
        kwargs['headers'] = self.TEST_REQUEST_HEADERS
 
213
        requests.request('DELETE',
 
214
                         urlparse.urljoin(self.TEST_URL,
 
215
                         'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
 
216
                         **kwargs).AndReturn((resp))
204
217
        self.mox.ReplayAll()
205
218
 
206
219
        self.client.roles.remove_user_role('foo', 'barrr', '4')