def test_sshkey_display_with_real_life_key(self): # With a real-life ssh-rsa key, the key_string part is cropped. key_string = get_data('data/test_rsa0.pub') user = factory.make_user() key = SSHKey(key=key_string, user=user) display = key.display_html() self.assertEqual( 'ssh-rsa AAAAB3NzaC1yc2E… ubuntu@server-7476', display)
def test_sshkey_user_and_key_unique_together(self): key_string = get_data('data/test_rsa0.pub') user = factory.make_user() key = SSHKey(key=key_string, user=user) key.save() key2 = SSHKey(key=key_string, user=user) self.assertRaises( ValidationError, key2.full_clean)
def test_sshkey_display_with_real_life_key(self): # With a real-life ssh-rsa key, the key_string part is cropped. key_string = get_data('data/test_rsa0.pub') user = factory.make_user() key = SSHKey(key=key_string, user=user) display = key.display_html() self.assertEqual( 'ssh-rsa AAAAB3NzaC1yc… ubuntu@test_rsa0.pub', display)
def test_key_can_be_added_if_same_key_already_setup_for_other_user(self): key_string = get_data('data/test_rsa0.pub') key = SSHKey(user=factory.make_user(), key=key_string) key.save() response = self.client.post( reverse('prefs-add-sshkey'), {'key': key_string}) new_key = SSHKey.objects.get(key=key_string, user=self.logged_in_user) self.assertEqual(httplib.FOUND, response.status_code) self.assertItemsEqual( [key, new_key], SSHKey.objects.filter(key=key_string))
def test_key_can_be_added_if_same_key_already_setup_for_other_user(self): key_string = get_data('data/test_rsa0.pub') key = SSHKey(user=factory.make_user(), key=key_string) key.save() response = self.client.post(reverse('prefs-add-sshkey'), {'key': key_string}) new_key = SSHKey.objects.get(key=key_string, user=self.logged_in_user) self.assertEqual(httplib.FOUND, response.status_code) self.assertItemsEqual([key, new_key], SSHKey.objects.filter(key=key_string))
def test_sshkey_user_and_key_unique_together(self): protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) key_string = get_data('data/test_rsa0.pub') user = factory.make_User() key = SSHKey(key=key_string, user=user, keysource=keysource) key.save() key2 = SSHKey(key=key_string, user=user, keysource=keysource) self.assertRaises(ValidationError, key2.full_clean)
def test_add_key_POST_fails_if_key_already_exists_for_the_user(self): key_string = get_data('data/test_rsa0.pub') key = SSHKey(user=self.logged_in_user, key=key_string) key.save() response = self.client.post(reverse('prefs-add-sshkey'), {'key': key_string}) self.assertEqual(httplib.OK, response.status_code) self.assertIn("This key has already been added for this user.", response.content) self.assertItemsEqual([key], SSHKey.objects.filter(key=key_string))
def copy_ssh_keys(user_from, user_dest): """Copies SSH keys from one user to another. This is idempotent, and does not clobber the destination user's existing keys. """ user_from_keys = get_ssh_keys(user_from) user_dest_keys = get_ssh_keys(user_dest) for key in set(user_from_keys).difference(user_dest_keys): ssh_key = SSHKey(user=user_dest, key=key) ssh_key.save()
def test_add_key_POST_fails_if_key_already_exists_for_the_user(self): key_string = get_data('data/test_rsa0.pub') key = SSHKey(user=self.logged_in_user, key=key_string) key.save() response = self.client.post( reverse('prefs-add-sshkey'), {'key': key_string}) self.assertEqual(httplib.OK, response.status_code) self.assertIn( "This key has already been added for this user.", response.content) self.assertItemsEqual([key], SSHKey.objects.filter(key=key_string))
def test_sshkey_same_key_can_be_used_by_different_users(self): key_string = get_data('data/test_rsa0.pub') user = factory.make_user() key = SSHKey(key=key_string, user=user) key.save() user2 = factory.make_user() key2 = SSHKey(key=key_string, user=user2) key2.full_clean()
def test_sshkey_same_key_can_be_used_by_different_sources(self): auth_id = factory.make_name('auth_id') keysource1 = factory.make_KeySource(protocol=KEYS_PROTOCOL_TYPE.LP, auth_id=auth_id) keysource2 = factory.make_KeySource(protocol=KEYS_PROTOCOL_TYPE.GH, auth_id=auth_id) key_string = get_data('data/test_rsa0.pub') user = factory.make_User() key1 = SSHKey(key=key_string, user=user, keysource=keysource1) key1.save() key2 = SSHKey(key=key_string, user=user, keysource=keysource2) key2.save() self.assertIsNone(key2.full_clean())
def test_sshkey_user_and_key_unique_together_change_key(self): protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH] ) auth_id = factory.make_name("auth_id") keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) key_string1 = get_data("data/test_rsa1.pub") key_string2 = get_data("data/test_rsa2.pub") user = factory.make_User() key1 = SSHKey(key=key_string1, user=user, keysource=keysource) key1.save() key2 = SSHKey(key=key_string2, user=user, keysource=keysource) key2.save() key2.key = key1.key self.assertRaises(ValidationError, key2.full_clean)
def make_user_with_keys(self, n_keys=2, user=None, **kwargs): """Create a user with n `SSHKey`. If user is not None, use this user instead of creating one. Additional keyword arguments are passed to `make_user()`. """ if n_keys > MAX_PUBLIC_KEYS: raise RuntimeError( "Cannot create more than %d public keys. If you need more: " "add more keys in src/maasserver/tests/data/." % MAX_PUBLIC_KEYS) if user is None: user = self.make_user(**kwargs) keys = [] for i in range(n_keys): key_string = get_data('data/test_rsa%d.pub' % i) key = SSHKey(user=user, key=key_string) key.save() keys.append(key) return user, keys
def test_sshkey_user_and_key_unique_together_db_level(self): # Even if we hack our way around model-level checks, uniqueness # of the user/key combination is enforced at the database level. key_string = get_data('data/test_rsa0.pub') user = factory.make_user() existing_key = SSHKey(key=key_string, user=user) existing_key.save() # The trick to hack around the model-level checks: create a # duplicate key for another user, then attach it to the same # user as the existing key by updating it directly in the # database. redundant_key = SSHKey(key=key_string, user=factory.make_user()) redundant_key.save() self.assertRaises( IntegrityError, SSHKey.objects.filter(id=redundant_key.id).update, user=user)
def test_sshkey_user_and_key_unique_together_db_level(self): # Even if we hack our way around model-level checks, uniqueness # of the user/key combination is enforced at the database level. protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) key_string = get_data('data/test_rsa0.pub') user = factory.make_User() existing_key = SSHKey(key=key_string, user=user, keysource=keysource) existing_key.save() # The trick to hack around the model-level checks: create a # duplicate key for another user, then attach it to the same # user as the existing key by updating it directly in the # database. redundant_key = SSHKey(key=key_string, user=factory.make_User(), keysource=keysource) redundant_key.save() self.assertRaises(IntegrityError, SSHKey.objects.filter(id=redundant_key.id).update, user=user)
def make_sshkey(self, user, key_string=None): if key_string is None: key_string = get_data('data/test_rsa0.pub') key = SSHKey(key=key_string, user=user) key.save() return key
def test_sshkey_validation_with_valid_key(self): key_string = get_data('data/test_rsa0.pub') user = factory.make_user() key = SSHKey(key=key_string, user=user) key.full_clean()
def test_sshkey_display_is_marked_as_HTML_safe(self): key_string = get_data('data/test_rsa0.pub') user = factory.make_user() key = SSHKey(key=key_string, user=user) display = key.display_html() self.assertIsInstance(display, SafeUnicode)
def test_sshkey_validation_fails_if_key_is_invalid(self): key_string = factory.getRandomString() user = factory.make_user() key = SSHKey(key=key_string, user=user) self.assertRaises( ValidationError, key.full_clean)