297
232
charset = '!@#$%^&*()'
298
233
string1 = generate_random_string(200, charset=charset)
299
234
self.assertTrue(all([ch in charset for ch in string1]))
303
LP_API_URL='http://api.launchpad.net/test',
304
LP_API_CONSUMER_KEY='SSO', LP_API_TOKEN='sso-token',
305
LP_API_TOKEN_SECRET='sso-secret')
306
class LPUsernameAPITestCase(SSOBaseTestCase):
309
super(LPUsernameAPITestCase, self).setUp()
310
self.lp_api_handler = MockHandler()
311
self.mock_lp_api_opener = self.patch(
312
'identityprovider.utils._lp_api_opener',
313
return_value=urllib2.build_opener(self.lp_api_handler))
315
def test_set_lp_username(self):
316
self.lp_api_handler.set_next_response(200, '')
317
self.assertIsNone(set_lp_username('foo', 'bar', dry_run=True))
319
[request] = self.lp_api_handler.requests
320
self.assertEqual('POST', request.get_method())
321
self.assertEqual(['Authorization'], request.headers.keys())
323
'OAuth oauth_nonce="...", oauth_timestamp="...", '
324
'oauth_version="1.0", oauth_signature_method="PLAINTEXT", '
325
'oauth_consumer_key="SSO", oauth_token="sso-token", '
326
'oauth_signature="%26sso-secret"',
327
re.sub(r'"\d+"', '"..."', request.headers['Authorization']))
329
'http://api.launchpad.net/test/people', request.get_full_url())
331
{'ws.op': ['setUsernameFromSSO'], 'openid_identifier': ['foo'],
332
'name': ['bar'], 'dry_run': ['true']},
333
urlparse.parse_qs(request.data))
335
def test_set_lp_username_taken(self):
336
self.lp_api_handler.set_next_response(
338
'name: taken-name is already in use by another person or team.')
340
NameAlreadyTaken, set_lp_username, 'foo', 'taken-name')
342
def test_set_lp_username_blacklisted(self):
343
self.lp_api_handler.set_next_response(
345
'name: The name 'private-name' has been blocked by the '
346
'Launchpad administrators. Contact Launchpad Support if you want '
349
NameAlreadyTaken, set_lp_username, 'foo', 'private-name')
351
def test_set_lp_username_existing(self):
352
self.lp_api_handler.set_next_response(
354
'An account for that OpenID identifier already exists.')
356
NotPlaceholderAccount, set_lp_username, 'taken-openid',
359
def test_get_lp_username(self):
360
self.lp_api_handler.set_next_response(200, json.dumps("bar"))
361
self.assertEqual("bar", get_lp_username('foo'))
363
[request] = self.lp_api_handler.requests
364
self.assertEqual('GET', request.get_method())
365
self.assertEqual(['Authorization'], request.headers.keys())
367
'OAuth oauth_nonce="...", oauth_timestamp="...", '
368
'oauth_version="1.0", oauth_signature_method="PLAINTEXT", '
369
'oauth_consumer_key="SSO", oauth_token="sso-token", '
370
'oauth_signature="%26sso-secret"',
371
re.sub(r'"\d+"', '"..."', request.headers['Authorization']))
373
'http://api.launchpad.net/test/people?'
374
'ws.op=getUsernameForSSO&openid_identifier=foo',
375
request.get_full_url())
379
LP_API_URL='http://api.launchpad.net/test',
380
LP_API_CONSUMER_KEY='SSO', LP_API_TOKEN='sso-token',
381
LP_API_TOKEN_SECRET='sso-secret')
382
class LPSSHKeyAPITestCase(SSOBaseTestCase):
385
super(LPSSHKeyAPITestCase, self).setUp()
386
self.lp_api_handler = MockHandler()
387
self.mock_lp_api_opener = self.patch(
388
'identityprovider.utils._lp_api_opener',
389
return_value=urllib2.build_opener(self.lp_api_handler))
391
def assert_authorization_headers(self, request):
392
self.assertEqual(['Authorization'], request.headers.keys())
394
'OAuth oauth_nonce="...", oauth_timestamp="...", '
395
'oauth_version="1.0", oauth_signature_method="PLAINTEXT", '
396
'oauth_consumer_key="SSO", oauth_token="sso-token", '
397
'oauth_signature="%26sso-secret"',
398
re.sub(r'"\d+"', '"..."', request.headers['Authorization']))
400
def test_get_ssh_keys_nonexistant(self):
401
self.lp_api_handler.set_next_response(200, 'null')
402
self.assertIsNone(get_lp_ssh_keys('openid'))
404
self.assertEqual(1, len(self.lp_api_handler.requests))
405
request = self.lp_api_handler.requests[0]
406
self.assertEqual('GET', request.get_method())
407
self.assert_authorization_headers(request)
410
'http://api.launchpad.net/test/people?'
411
'ws.op=getSSHKeysForSSO&openid_identifier=openid',
412
request.get_full_url())
414
def test_get_ssh_keys_no_keys(self):
415
self.lp_api_handler.set_next_response(200, '[]')
416
self.assertEqual([], get_lp_ssh_keys('openid'))
418
def test_add_ssh_key_with_nonexistant_user(self):
419
self.lp_api_handler.set_next_response(
420
400, "No account found for openid identifier 'openid'")
424
'openid', 'ssh-rsa keytext comment', False
426
[request] = self.lp_api_handler.requests
427
self.assertEqual('POST', request.get_method())
428
self.assert_authorization_headers(request)
430
def test_add_ssh_key_with_bad_key_type(self):
431
self.lp_api_handler.set_next_response(
432
400, "Invalid SSH key type: 'flub'")
434
SSHKeyAdditionTypeError,
436
'openid', 'flub keytext comment', False
438
[request] = self.lp_api_handler.requests
439
self.assertEqual('POST', request.get_method())
440
self.assert_authorization_headers(request)
442
def test_add_ssh_key_with_bad_key_data(self):
443
self.lp_api_handler.set_next_response(
444
400, "Invalid SSH key data: 'keywithnospaces'")
446
SSHKeyAdditionDataError,
448
'openid', 'keywithnospaces', False
450
[request] = self.lp_api_handler.requests
451
self.assertEqual('POST', request.get_method())
452
self.assert_authorization_headers(request)
454
def test_add_ssh_key_with_compromised_key(self):
455
self.lp_api_handler.set_next_response(
457
"This key cannot be added as it is known to be compromised.")
459
SSHKeyCompromisedError,
461
'openid', 'compromised_key', False
463
[request] = self.lp_api_handler.requests
464
self.assertEqual('POST', request.get_method())
465
self.assert_authorization_headers(request)
467
def test_add_ssh_key_works(self):
468
self.lp_api_handler.set_next_response(200, "")
469
self.assertIsNone(add_lp_ssh_key('openid', 'key_data', False))
470
[request] = self.lp_api_handler.requests
471
self.assertEqual('POST', request.get_method())
472
self.assert_authorization_headers(request)
474
def test_delete_ssh_key_with_nonexistant_user(self):
475
self.lp_api_handler.set_next_response(
476
400, "No account found for openid identifier 'openid'")
480
'openid', 'ssh-rsa keytext comment', False
482
[request] = self.lp_api_handler.requests
483
self.assertEqual('POST', request.get_method())
484
self.assert_authorization_headers(request)
486
def test_delete_ssh_key_with_bad_key_type(self):
487
self.lp_api_handler.set_next_response(
488
400, "Invalid SSH key type: 'flub'")
490
SSHKeyAdditionTypeError,
492
'openid', 'flub keytext comment', False
494
[request] = self.lp_api_handler.requests
495
self.assertEqual('POST', request.get_method())
496
self.assert_authorization_headers(request)
498
def test_delete_ssh_key_with_bad_key_data(self):
499
self.lp_api_handler.set_next_response(
500
400, "Invalid SSH key data: 'keywithnospaces'")
502
SSHKeyAdditionDataError,
504
'openid', 'keywithnospaces', False
506
[request] = self.lp_api_handler.requests
507
self.assertEqual('POST', request.get_method())
508
self.assert_authorization_headers(request)
510
def test_delete_ssh_key_works(self):
511
self.lp_api_handler.set_next_response(200, "")
512
self.assertIsNone(delete_lp_ssh_key('openid', 'key_data', False))
513
[request] = self.lp_api_handler.requests
514
self.assertEqual('POST', request.get_method())
515
self.assert_authorization_headers(request)