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