aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/mailman_pgp/rest/addresses.py38
-rw-r--r--src/mailman_pgp/rest/tests/test_addresses.py33
2 files changed, 64 insertions, 7 deletions
diff --git a/src/mailman_pgp/rest/addresses.py b/src/mailman_pgp/rest/addresses.py
index 6d99a04..ff1393a 100644
--- a/src/mailman_pgp/rest/addresses.py
+++ b/src/mailman_pgp/rest/addresses.py
@@ -17,14 +17,15 @@
""""""
-from mailman.rest.helpers import CollectionMixin, etag, not_found, okay
+from mailman.rest.helpers import (child, CollectionMixin, etag, not_found,
+ NotFound, okay)
from public.public import public
from mailman_pgp.config import config
from mailman_pgp.model.address import PGPAddress
-class _EncryptedBase(CollectionMixin):
+class _PGPAddressBase(CollectionMixin):
def _resource_as_dict(self, address):
"""See `CollectionMixin`."""
return dict(email=address.email,
@@ -41,7 +42,9 @@ class _EncryptedBase(CollectionMixin):
@public
-class AllAddresses(_EncryptedBase):
+class AllAddresses(_PGPAddressBase):
+ """"""
+
def on_get(self, request, response):
"""/addresses"""
resource = self._make_collection(request)
@@ -49,7 +52,9 @@ class AllAddresses(_EncryptedBase):
@public
-class AnAddress(_EncryptedBase):
+class AnAddress(_PGPAddressBase):
+ """"""
+
def __init__(self, email):
self._address = PGPAddress.for_email(email)
@@ -58,3 +63,28 @@ class AnAddress(_EncryptedBase):
return not_found(response)
else:
okay(response, self._resource_as_json(self._address))
+
+ @child()
+ def key(self, context, segments):
+ if self._address is None:
+ return NotFound(), []
+ return AnAddressKey(self._address), []
+
+
+@public
+class AnAddressKey:
+ """"""
+
+ def __init__(self, address):
+ self._address = address
+
+ def on_get(self, request, response):
+ """/addresses/<email>/key"""
+ key = self._address.key
+ if key is None:
+ not_found(response)
+ else:
+ resource = dict(key=str(key),
+ key_fingerprint=str(key.fingerprint),
+ key_confirmed=self._address.key_confirmed)
+ okay(response, etag(resource))
diff --git a/src/mailman_pgp/rest/tests/test_addresses.py b/src/mailman_pgp/rest/tests/test_addresses.py
index 1730618..ba8ba0e 100644
--- a/src/mailman_pgp/rest/tests/test_addresses.py
+++ b/src/mailman_pgp/rest/tests/test_addresses.py
@@ -23,19 +23,25 @@ from mailman.interfaces.usermanager import IUserManager
from mailman.testing.helpers import call_api
from zope.component import getUtility
-from mailman_pgp.database import transaction
+from mailman_pgp.database import mm_transaction, transaction
from mailman_pgp.model.address import PGPAddress
from mailman_pgp.testing.layers import PGPRESTLayer
+from mailman_pgp.testing.pgp import load_key
+from mailman_pgp.utils.pgp import key_from_blob
class TestAddresses(unittest.TestCase):
layer = PGPRESTLayer
def setUp(self):
- self.mm_address = getUtility(IUserManager).create_address(
- 'anne@example.com')
+ with mm_transaction():
+ self.mm_address = getUtility(IUserManager).create_address(
+ 'anne@example.com')
+ self.address_key = load_key('ecc_p256.pub.asc')
with transaction() as t:
self.address = PGPAddress(self.mm_address)
+ self.address.key = self.address_key
+ self.address.key_confirmed = True
t.add(self.address)
def test_missing_address(self):
@@ -58,3 +64,24 @@ class TestAddresses(unittest.TestCase):
'http://localhost:9001/3.1/plugins/pgp/addresses/'
'anne@example.com')
self.assertEqual(json['email'], self.address.email)
+
+ def test_address_key(self):
+ json, response = call_api(
+ 'http://localhost:9001/3.1/plugins/pgp/addresses/'
+ 'anne@example.com/key')
+ key = key_from_blob(json['key'])
+ self.assertEqual(key.fingerprint, self.address_key.fingerprint)
+ self.assertEqual(json['key_fingerprint'], self.address_key.fingerprint)
+ self.assertEqual(json['key_confirmed'], True)
+
+ def test_address_no_key(self):
+ with mm_transaction():
+ mm_address = getUtility(IUserManager).create_address(
+ 'bart@example.com')
+ with transaction() as t:
+ address = PGPAddress(mm_address)
+ t.add(address)
+ with self.assertRaises(HTTPError) as cm:
+ call_api('http://localhost:9001/3.1/plugins/pgp/addresses/'
+ 'bart@example.com/key')
+ self.assertEqual(cm.exception.code, 404)