launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #20498
[Merge] lp:~thomir/launchpad/devel-death-to-doctests into lp:launchpad
Thomi Richards has proposed merging lp:~thomir/launchpad/devel-death-to-doctests into lp:launchpad.
Commit message:
Remove sshkey.txt, reimplementing those tests in test_ssh.py.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~thomir/launchpad/devel-death-to-doctests/+merge/295774
Remove sshkey.txt, reimplementing those tests in test_ssh.py.
--
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~thomir/launchpad/devel-death-to-doctests into lp:launchpad.
=== removed file 'lib/lp/registry/doc/sshkey.txt'
--- lib/lp/registry/doc/sshkey.txt 2016-05-25 05:12:52 +0000
+++ lib/lp/registry/doc/sshkey.txt 1970-01-01 00:00:00 +0000
@@ -1,59 +0,0 @@
-SSH Keys
-========
-
-Launchpad models SSH keys in a SSHKey class.
-
- >>> from lp.testing import login_person
- >>> from zope.component import getUtility
- >>> from lp.registry.interfaces.person import IPersonSet
- >>> from lp.registry.interfaces.ssh import ISSHKeySet
- >>> personset = getUtility(IPersonSet)
- >>> name12 = personset.getByName('name12')
- >>> ignored = login_person(name12)
- >>> [(key.keytype, key.comment) for key in name12.sshkeys]
- [(<DBItem SSHKeyType.DSA, (2) DSA>, u'andrew@trogdor')]
-
-You can fetch SSH keys by ID:
-
- >>> sshkeyset = getUtility(ISSHKeySet)
- >>> key = sshkeyset.getByID(1)
- >>> key.keytype, key.keytext
- (<DBItem SSHKeyType.DSA, (2) DSA>, u'AAAAB3NzaC1k...9SG1gBOiI=')
-
-Adding new keys is pretty easy:
-
- >>> foobar = personset.getByName('name16')
- >>> ignored = login_person(foobar)
- >>> key = sshkeyset.new(
- ... foobar, "ssh-rsa zzzNOT-REALLY This is just a test key")
- >>> key, key.keytext
- (<SSHKey at ...>, u'zzzNOT-REALLY')
-
- >>> key = sshkeyset.new(
- ... name12, "ssh-rsa zzzNOT-EITHER This is just a test key.")
- >>> key, key.keytext
- (<SSHKey at ...>, u'zzzNOT-EITHER')
-
-Bad keys raise a SSHKeyAdditionError.
-
- >>> sshkeyset.new(foobar, None)
- Traceback (most recent call last):
- ...
- SSHKeyAdditionError: Invalid SSH key data: 'None'
-
- >>> bad_key = "thiskeyhasnospaces"
- >>> sshkeyset.new(foobar, bad_key)
- Traceback (most recent call last):
- ...
- SSHKeyAdditionError: Invalid SSH key data: 'thiskeyhasnospaces'
-
-
-There's also a convenience method for fetching multiple SSH keys at
-once:
-
- >>> cprov = personset.getByName('cprov')
- >>> keys = sshkeyset.getByPeople([foobar, cprov, name12])
- >>> [(key.person.name, key.keytype, key.keytext) for key in keys]
- [(u'name12', <DBItem SSHKeyType.RSA, (1) RSA>, u'zzzNOT-EITHER'),
- (u'name12', <DBItem SSHKeyType.DSA, (2) DSA>, u'AAAAB3...vz9SG1gBOiI='),
- (u'name16', <DBItem SSHKeyType.RSA, (1) RSA>, u'zzzNOT-REALLY')]
=== modified file 'lib/lp/registry/tests/test_ssh.py'
--- lib/lp/registry/tests/test_ssh.py 2016-05-25 05:20:30 +0000
+++ lib/lp/registry/tests/test_ssh.py 2016-05-25 23:02:45 +0000
@@ -10,12 +10,14 @@
from lp.registry.interfaces.ssh import (
ISSHKeySet,
+ SSH_TEXT_TO_KEY_TYPE,
SSHKeyAdditionError,
SSHKeyCompromisedError,
SSHKeyType,
)
from lp.testing import (
person_logged_in,
+ admin_logged_in,
TestCaseWithFactory,
)
from lp.testing.layers import DatabaseFunctionalLayer
@@ -131,3 +133,56 @@
keyset.getByPersonAndKeyText,
person, invalid_keytext
)
+
+ def test_can_retrieve_keys_by_id(self):
+ keyset = getUtility(ISSHKeySet)
+ person = self.factory.makePerson()
+ with person_logged_in(person):
+ new_key = self.factory.makeSSHKey(person)
+
+ retrieved_new_key = keyset.getByID(new_key.id)
+
+ self.assertEqual(retrieved_new_key, new_key)
+
+ def test_can_add_new_key(self):
+ keyset = getUtility(ISSHKeySet)
+ person = self.factory.makePerson()
+ keytype = 'ssh-rsa'
+ keytext = 'ThisIsAFakeSSHKey'
+ comment = 'This is a key comment.'
+ full_key = ' '.join((keytype, keytext, comment))
+ with person_logged_in(person):
+ key = keyset.new(person, full_key)
+ self.assertEqual([key], list(person.sshkeys))
+ self.assertEqual(SSH_TEXT_TO_KEY_TYPE[keytype], key.keytype)
+ self.assertEqual(keytext, key.keytext)
+ self.assertEqual(comment, key.comment)
+
+ def test_new_raises_KeyAdditionError_on_bad_key_data(self):
+ person = self.factory.makePerson()
+ keyset = getUtility(ISSHKeySet)
+ self.assertRaises(
+ SSHKeyAdditionError,
+ keyset.new,
+ person, 'thiskeyhasnospaces'
+ )
+ self.assertRaises(
+ SSHKeyAdditionError,
+ keyset.new,
+ person, 'bad_key_type keytext comment'
+ )
+
+ def test_can_retrieve_keys_for_multiple_people(self):
+ with admin_logged_in():
+ person1 = self.factory.makePerson()
+ person1_key1 = self.factory.makeSSHKey(person1)
+ person1_key2 = self.factory.makeSSHKey(person1)
+ person2 = self.factory.makePerson()
+ person2_key1 = self.factory.makeSSHKey(person2)
+
+ keyset = getUtility(ISSHKeySet)
+ keys = keyset.getByPeople([person1, person2])
+ self.assertEqual(3, keys.count())
+ self.assertIn(person1_key1, keys)
+ self.assertIn(person1_key2, keys)
+ self.assertIn(person2_key1, keys)
Follow ups