Commit b56c4574 by Paul Kilgo

delete underlying key when UserKey is deleted

parent a2c39f38
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db.models.signals import pre_save from django.db.models.signals import pre_save, post_delete
from django.dispatch import receiver from django.dispatch import receiver
try: try:
from django.utils.timezone import now from django.utils.timezone import now
...@@ -178,6 +178,10 @@ class UserKey(NamedKey): ...@@ -178,6 +178,10 @@ class UserKey(NamedKey):
message = 'Cannot associate key with two users.' message = 'Cannot associate key with two users.'
raise ValidationError({'basekey': [message]}) raise ValidationError({'basekey': [message]})
@receiver(post_delete, sender=UserKey)
def delete_user_key(sender, instance, **kwargs):
instance.basekey.delete()
@receiver(pre_save, sender=UserKey) @receiver(pre_save, sender=UserKey)
def send_email_add_key(sender, instance, **kwargs): def send_email_add_key(sender, instance, **kwargs):
if not settings.SSHKEY_EMAIL_ADD_KEY or instance.pk: if not settings.SSHKEY_EMAIL_ADD_KEY or instance.pk:
......
...@@ -310,6 +310,10 @@ class UserKeyTestCase(BaseTestCase): ...@@ -310,6 +310,10 @@ class UserKeyTestCase(BaseTestCase):
cls.key2_path = os.path.join(cls.key_dir, 'key2') cls.key2_path = os.path.join(cls.key_dir, 'key2')
ssh_keygen(comment='', file=cls.key2_path) ssh_keygen(comment='', file=cls.key2_path)
# key3 is safe to delete
cls.key3_path = os.path.join(cls.key_dir, 'key3')
ssh_keygen(comment='comment', file=cls.key3_path)
# make the Key models # make the Key models
cls.key1 = Key(key=open(cls.key1_path + '.pub').read()) cls.key1 = Key(key=open(cls.key1_path + '.pub').read())
cls.key1.full_clean() cls.key1.full_clean()
...@@ -385,7 +389,17 @@ class UserKeyTestCase(BaseTestCase): ...@@ -385,7 +389,17 @@ class UserKeyTestCase(BaseTestCase):
) )
self.assertRaises(ValidationError, key2.full_clean) self.assertRaises(ValidationError, key2.full_clean)
def test_delete(self):
basekey = Key(key=open(self.key3_path + '.pub').read())
basekey.full_clean()
basekey.save()
pk = basekey.pk
key1 = UserKey(basekey=basekey, user=self.user1, name='name1')
key1.full_clean()
key1.save()
key1.delete()
self.assertRaises(Key.DoesNotExist, Key.objects.get, pk=pk)
class RFC4716TestCase(BaseTestCase): class RFC4716TestCase(BaseTestCase):
@classmethod @classmethod
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment