~ltrager/maas/lp1654432_2.1

« back to all changes in this revision

Viewing changes to src/maasserver/api/tests/test_api.py

  • Committer: LaMont Jones
  • Date: 2016-10-12 15:26:17 UTC
  • mfrom: (5469 maas)
  • mto: This revision was merged to the branch mainline in revision 5482.
  • Revision ID: lamont@canonical.com-20161012152617-t14n2jt7y5b7hidb
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
181
181
 
182
182
    def test_create_authorisation_token(self):
183
183
        # The api operation create_authorisation_token returns a json dict
184
 
        # with the consumer_key, the token_key and the token_secret in it.
 
184
        # with the consumer_key, the token_key, the token_secret and the
 
185
        # consumer_name in it.
185
186
        response = self.client.post(
186
187
            reverse('account_handler'), {'op': 'create_authorisation_token'})
187
188
        self.assertEqual(http.client.OK, response.status_code)
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)
 
199
 
 
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',
 
207
                'name': token_name
 
208
            })
 
209
        self.assertEqual(http.client.OK, response.status_code)
 
210
        self.assertEqual(
 
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)
197
214
 
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'})
213
 
 
214
 
        self.assertEqual(http.client.BAD_REQUEST, response.status_code)
 
229
            reverse('account_handler'), {
 
230
                'op': 'delete_authorisation_token'
 
231
            })
 
232
 
 
233
        self.assertEqual(http.client.BAD_REQUEST, response.status_code)
 
234
 
 
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
 
242
            })
 
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']
 
248
        ])
 
249
        self.client.post(
 
250
            reverse('account_handler'), {
 
251
                'op': 'update_token_name', 'token': created_token,
 
252
                'name': token_name_updated
 
253
            })
 
254
        response_list = self.client.get(
 
255
            reverse('account_handler'), {
 
256
                'op': 'list_authorisation_tokens'
 
257
            })
 
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)
 
262
 
 
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
 
272
            })
 
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']
 
278
        ])
 
279
        self.client.post(
 
280
            reverse('account_handler'), {
 
281
                'op': 'update_token_name',
 
282
                'token': parsed_creation_response['token_key'],
 
283
                'name': token_name_updated
 
284
            })
 
285
        response_list = self.client.get(
 
286
            reverse('account_handler'), {
 
287
                'op': 'list_authorisation_tokens'
 
288
            })
 
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)
 
293
 
 
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',
 
300
                'name': 'test_name'
 
301
            })
 
302
 
 
303
        self.assertEqual(http.client.NOT_FOUND, response.status_code)
 
304
 
 
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'
 
312
            })
 
313
 
 
314
        self.assertEqual(http.client.BAD_REQUEST, response.status_code)
 
315
 
 
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',
 
321
                'name': token1_name
 
322
            })
 
323
        parsed_creation_response = json_load_bytes(response_creation.content)
 
324
        response = self.client.get(
 
325
            reverse('account_handler'), {
 
326
                'op': 'list_authorisation_tokens'
 
327
            })
 
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(":")
 
333
                self.assertEqual(
 
334
                    token_fields[0],
 
335
                    parsed_creation_response['consumer_key'])
 
336
                self.assertEqual(
 
337
                    token_fields[1],
 
338
                    parsed_creation_response['token_key'])
 
339
                self.assertEqual(
 
340
                    token_fields[2],
 
341
                    parsed_creation_response['token_secret'])
 
342
 
 
343
    def test_list_tokens_format(self):
 
344
        self.client.post(
 
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)
215
354
 
216
355
 
217
356
class TestSSHKeyHandlers(APITestCase.ForUser):