def handle(self, *args, **options): if len(args) != 3: raise CommandError( "must provide a public keyfile, private keyfile and label") public_key, private_key, name = args import_sshkeypair( name, public_key, private_key, update=options["update"], stdout=self.stdout) transaction.commit_unless_managed()
def test_import_sshkeypair(self): """ We can import a new keypair with a name. """ stdout = StringIO() import_sshkeypair( "my test key", self.public, self.private, stdout=stdout) keypair = SshKeyPair.objects.get(label="my test key") self.assertEqual("public key", keypair.public_key) self.assertEqual("private key", keypair.private_key) self.assertEqual("Key pair created\n", stdout.getvalue())
def test_import_sshkeypair_fails_with_preexisting_label(self): """ import_sshkeypair should fail if we have a keypair with the label. """ SshKeyPair.objects.create( label="my test", public_key="test", private_key="test") with self.assertRaises(CommandError) as cm: import_sshkeypair("my test", self.public, self.private) self.assertEqual("Key pair already exists", str(cm.exception)) keypair = SshKeyPair.objects.get(label="my test") self.assertEqual("test", keypair.public_key) self.assertEqual("test", keypair.private_key)
def test_import_sshkeypair_fails_with_preexisting_label(self): """ import_sshkeypair should fail if we have a keypair with the label. """ SshKeyPair.objects.create(label="my test", public_key="test", private_key="test") with self.assertRaises(CommandError) as cm: import_sshkeypair("my test", self.public, self.private) self.assertEqual("Key pair already exists", str(cm.exception)) keypair = SshKeyPair.objects.get(label="my test") self.assertEqual("test", keypair.public_key) self.assertEqual("test", keypair.private_key)
def test_import_sshkeypair(self): """ We can import a new keypair with a name. """ stdout = StringIO() import_sshkeypair("my test key", self.public, self.private, stdout=stdout) keypair = SshKeyPair.objects.get(label="my test key") self.assertEqual("public key", keypair.public_key) self.assertEqual("private key", keypair.private_key) self.assertEqual("Key pair created\n", stdout.getvalue())
def test_import_sshkeypair_updates_existing_keypair(self): """ import_sshkeypair should update the content if we provide a true value for the update parameter. """ stdout = StringIO() SshKeyPair.objects.create( label="my test", public_key="test", private_key="test") import_sshkeypair( "my test", self.public, self.private, update=True, stdout=stdout) keypair = SshKeyPair.objects.get(label="my test") self.assertEqual("public key", keypair.public_key) self.assertEqual("private key", keypair.private_key) self.assertEqual("Key pair updated\n", stdout.getvalue())
def test_import_sshkeypair_updates_existing_keypair(self): """ import_sshkeypair should update the content if we provide a true value for the update parameter. """ stdout = StringIO() SshKeyPair.objects.create(label="my test", public_key="test", private_key="test") import_sshkeypair("my test", self.public, self.private, update=True, stdout=stdout) keypair = SshKeyPair.objects.get(label="my test") self.assertEqual("public key", keypair.public_key) self.assertEqual("private key", keypair.private_key) self.assertEqual("Key pair updated\n", stdout.getvalue())