189
190
'application/json; charset=utf-8', response["content-type"])
190
191
parsed_result = json_load_bytes(response.content)
191
192
self.assertEqual(
192
['consumer_key', 'token_key', 'token_secret'],
193
['consumer_key', 'name', 'token_key', 'token_secret'],
193
194
sorted(parsed_result))
194
195
self.assertIsInstance(parsed_result['consumer_key'], str)
195
196
self.assertIsInstance(parsed_result['token_key'], str)
196
197
self.assertIsInstance(parsed_result['token_secret'], str)
198
self.assertIsInstance(parsed_result['name'], str)
200
def test_create_authorisation_token_with_token_name(self):
201
# The api operation create_authorisation_token can also accept
202
# a new name for the generated token.
203
token_name = 'Test_Token'
204
response = self.client.post(
205
reverse('account_handler'), {
206
'op': 'create_authorisation_token',
209
self.assertEqual(http.client.OK, response.status_code)
211
'application/json; charset=utf-8', response["content-type"])
212
parsed_result = json_load_bytes(response.content)
213
self.assertEqual(parsed_result['name'], token_name)
198
215
def test_delete_authorisation_token_not_found(self):
199
216
# If the provided token_key does not exist (for the currently
209
226
# delete_authorisation_token. It it is not present in the request's
210
227
# parameters, the api returns a 'Bad Request' (400) error.
211
228
response = self.client.post(
212
reverse('account_handler'), {'op': 'delete_authorisation_token'})
214
self.assertEqual(http.client.BAD_REQUEST, response.status_code)
229
reverse('account_handler'), {
230
'op': 'delete_authorisation_token'
233
self.assertEqual(http.client.BAD_REQUEST, response.status_code)
235
def test_update_authorisation_token(self):
236
token_name_orig = 'Test_Token'
237
token_name_updated = 'Test_Token update'
238
response_creation = self.client.post(
239
reverse('account_handler'), {
240
'op': 'create_authorisation_token',
241
'name': token_name_orig
243
parsed_creation_response = json_load_bytes(response_creation.content)
244
created_token = ":".join([
245
parsed_creation_response['consumer_key'],
246
parsed_creation_response['token_key'],
247
parsed_creation_response['token_secret']
250
reverse('account_handler'), {
251
'op': 'update_token_name', 'token': created_token,
252
'name': token_name_updated
254
response_list = self.client.get(
255
reverse('account_handler'), {
256
'op': 'list_authorisation_tokens'
258
parsed_list_response = json_load_bytes(response_list.content)
259
for token in parsed_list_response:
260
if token['token'] == created_token:
261
self.assertEqual(token['name'], token_name_updated)
263
def test_update_authorisation_token_with_token_key(self):
264
# We use only "token_key" portion of the authorisation token
265
# to update the token name.
266
token_name_orig = 'Test_Token'
267
token_name_updated = 'Test_Token update'
268
response_creation = self.client.post(
269
reverse('account_handler'), {
270
'op': 'create_authorisation_token',
271
'name': token_name_orig
273
parsed_creation_response = json_load_bytes(response_creation.content)
274
created_token = ":".join([
275
parsed_creation_response['consumer_key'],
276
parsed_creation_response['token_key'],
277
parsed_creation_response['token_secret']
280
reverse('account_handler'), {
281
'op': 'update_token_name',
282
'token': parsed_creation_response['token_key'],
283
'name': token_name_updated
285
response_list = self.client.get(
286
reverse('account_handler'), {
287
'op': 'list_authorisation_tokens'
289
parsed_list_response = json_load_bytes(response_list.content)
290
for token in parsed_list_response:
291
if token['token'] == created_token:
292
self.assertEqual(token['name'], token_name_updated)
294
def test_update_authorisation_token_name_not_found(self):
295
# If the provided token_key does not exist (for the currently
296
# logged-in user), the api returns a 'Not Found' (404) error.
297
response = self.client.post(
298
reverse('account_handler'), {
299
'op': 'update_token_name', 'token': 'no-such-token',
303
self.assertEqual(http.client.NOT_FOUND, response.status_code)
305
def test_update_authorisation_token_name_bad_request_no_token(self):
306
# `token` and `name` are mandatory parameters when calling
307
# update_token_name. If it is not present in the request's
308
# parameters, the api returns a 'Bad Request' (400) error.
309
response = self.client.post(
310
reverse('account_handler'), {
311
'op': 'update_token_name'
314
self.assertEqual(http.client.BAD_REQUEST, response.status_code)
316
def test_list_tokens(self):
317
token1_name = "Test Token 1"
318
response_creation = self.client.post(
319
reverse('account_handler'), {
320
'op': 'create_authorisation_token',
323
parsed_creation_response = json_load_bytes(response_creation.content)
324
response = self.client.get(
325
reverse('account_handler'), {
326
'op': 'list_authorisation_tokens'
328
parsed_list_response = json_load_bytes(response.content)
329
self.assertEqual(len(json_load_bytes(response.content)), 2)
330
for token in parsed_list_response:
331
if token['name'] == token1_name:
332
token_fields = token['token'].split(":")
335
parsed_creation_response['consumer_key'])
338
parsed_creation_response['token_key'])
341
parsed_creation_response['token_secret'])
343
def test_list_tokens_format(self):
345
reverse('account_handler'), {'op': 'create_authorisation_token'})
346
response = self.client.get(
347
reverse('account_handler'), {'op': 'list_authorisation_tokens'})
348
parsed_list_response = json_load_bytes(response.content)
349
self.assertIsInstance(parsed_list_response, list)
350
for token in parsed_list_response:
351
self.assertEqual(['name', 'token'], sorted(token))
352
self.assertIsInstance(token['name'], str)
353
self.assertIsInstance(token['token'], str)
217
356
class TestSSHKeyHandlers(APITestCase.ForUser):