github.com/blystad/deis@v0.11.0/controller/api/tests/test_key.py (about) 1 """ 2 Unit tests for the Deis api app. 3 4 Run the tests with "./manage.py test api" 5 """ 6 7 from __future__ import unicode_literals 8 9 import json 10 11 from django.test import TestCase 12 from django.test.utils import override_settings 13 14 from api.models import Key 15 from api.utils import fingerprint 16 17 18 RSA_PUBKEY = ( 19 "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfQkkUUoxpvcNMkvv7jqnfodgs37M2eBO" 20 "APgLK+KNBMaZaaKB4GF1QhTCMfFhoiTW3rqa0J75bHJcdkoobtTHlK8XUrFqsquWyg3XhsT" 21 "Yr/3RQQXvO86e2sF7SVDJqVtpnbQGc5SgNrHCeHJmf5HTbXSIjCO/AJSvIjnituT/SIAMGe" 22 "Bw0Nq/iSltwYAek1hiKO7wSmLcIQ8U4A00KEUtalaumf2aHOcfjgPfzlbZGP0S0cuBwSqLr" 23 "8b5XGPmkASNdUiuJY4MJOce7bFU14B7oMAy2xacODUs1momUeYtGI9T7X2WMowJaO7tP3Gl" 24 "sgBMP81VfYTfYChAyJpKp2yoP autotest@autotesting comment" 25 ) 26 27 ECDSA_PUBKEY = ( 28 "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAAB" 29 "BBCGB0x9lmubbLJTF5NekCI0Cgjyip6jJh/t/qQQi1LAZisbREBJ8Wy+hwSn3tnbf/Imh9X" 30 "+MQnrrza0jaQ3QUAQ= autotest@autotesting comment" 31 ) 32 33 34 @override_settings(CELERY_ALWAYS_EAGER=True) 35 class KeyTest(TestCase): 36 37 """Tests cloud provider credentials""" 38 39 fixtures = ['tests.json'] 40 41 def setUp(self): 42 self.assertTrue( 43 self.client.login(username='autotest', password='password')) 44 45 def _check_key(self, pubkey): 46 """ 47 Test that a user can add, remove and manage their SSH public keys 48 """ 49 url = '/api/keys' 50 body = {'id': 'mykey@box.local', 'public': pubkey} 51 response = self.client.post(url, json.dumps(body), content_type='application/json') 52 self.assertEqual(response.status_code, 201) 53 key_id = response.data['id'] 54 response = self.client.get(url) 55 self.assertEqual(response.status_code, 200) 56 self.assertEqual(len(response.data['results']), 1) 57 url = '/api/keys/{key_id}'.format(**locals()) 58 response = self.client.get(url) 59 self.assertEqual(response.status_code, 200) 60 self.assertEqual(body['id'], response.data['id']) 61 self.assertEqual(body['public'], response.data['public']) 62 response = self.client.delete(url) 63 self.assertEqual(response.status_code, 204) 64 65 def test_rsa_key(self): 66 self._check_key(RSA_PUBKEY) 67 68 def test_ecdsa_key(self): 69 self._check_key(ECDSA_PUBKEY) 70 71 def _check_duplicate_key(self, pubkey): 72 """ 73 Test that a user cannot add a duplicate key 74 """ 75 url = '/api/keys' 76 body = {'id': 'mykey@box.local', 'public': pubkey} 77 response = self.client.post(url, json.dumps(body), content_type='application/json') 78 self.assertEqual(response.status_code, 201) 79 response = self.client.post(url, json.dumps(body), content_type='application/json') 80 self.assertEqual(response.status_code, 400) 81 82 def test_rsa_duplicate_key(self): 83 self._check_duplicate_key(RSA_PUBKEY) 84 85 def test_ecdsa_duplicate_key(self): 86 self._check_duplicate_key(ECDSA_PUBKEY) 87 88 def test_rsa_key_str(self): 89 """Test the text representation of a key""" 90 url = '/api/keys' 91 body = {'id': 'autotest', 'public': 92 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzqPAwHN70xsB0LXG//KzO' 93 'gcPikyhdN/KRc4x3j/RA0pmFj63Ywv0PJ2b1LcMSqfR8F11WBlrW8c9xFua0' 94 'ZAKzI+gEk5uqvOR78bs/SITOtKPomW4e/1d2xEkJqOmYH30u94+NZZYwEBqY' 95 'aRb34fhtrnJS70XeGF0RhXE5Qea5eh7DBbeLxPfSYd8rfHgzMSb/wmx3h2vm' 96 'HdQGho20pfJktNu7DxeVkTHn9REMUphf85su7slTgTlWKq++3fASE8PdmFGz' 97 'b6PkOR4c+LS5WWXd2oM6HyBQBxxiwXbA2lSgQxOdgDiM2FzT0GVSFMUklkUH' 98 'MdsaG6/HJDw9QckTS0vN autotest@deis.io'} 99 response = self.client.post(url, json.dumps(body), content_type='application/json') 100 self.assertEqual(response.status_code, 201) 101 key = Key.objects.get(uuid=response.data['uuid']) 102 self.assertEqual(str(key), 'ssh-rsa AAAAB3NzaC.../HJDw9QckTS0vN autotest@deis.io') 103 104 def test_rsa_key_fingerprint(self): 105 fp = fingerprint(RSA_PUBKEY) 106 self.assertEquals(fp, '54:6d:da:1f:91:b5:2b:6f:a2:83:90:c4:f9:73:76:f5')