diff options
| author | J08nY | 2017-07-11 22:38:20 +0200 |
|---|---|---|
| committer | J08nY | 2017-07-11 22:38:20 +0200 |
| commit | 44bde427fdcc78344892f3413597c7be66eca8e9 (patch) | |
| tree | 40a996698e9ec164e7c51dd678ca189033e74e55 /src/mailman_pgp/rest | |
| parent | 4bab3532e38f6fd4826e0af2f96b6060286e3ed7 (diff) | |
| download | mailman-pgp-44bde427fdcc78344892f3413597c7be66eca8e9.tar.gz mailman-pgp-44bde427fdcc78344892f3413597c7be66eca8e9.tar.zst mailman-pgp-44bde427fdcc78344892f3413597c7be66eca8e9.zip | |
Diffstat (limited to 'src/mailman_pgp/rest')
| -rw-r--r-- | src/mailman_pgp/rest/addresses.py | 37 | ||||
| -rw-r--r-- | src/mailman_pgp/rest/lists.py | 2 | ||||
| -rw-r--r-- | src/mailman_pgp/rest/tests/test_addresses.py | 60 | ||||
| -rw-r--r-- | src/mailman_pgp/rest/tests/test_lists.py | 2 |
4 files changed, 93 insertions, 8 deletions
diff --git a/src/mailman_pgp/rest/addresses.py b/src/mailman_pgp/rest/addresses.py index 4f7a04d..93be6d3 100644 --- a/src/mailman_pgp/rest/addresses.py +++ b/src/mailman_pgp/rest/addresses.py @@ -17,19 +17,44 @@ """""" -from mailman.rest.helpers import CollectionMixin +from mailman.rest.helpers import CollectionMixin, okay, etag, not_found from public.public import public +from mailman_pgp.config import config +from mailman_pgp.model.address import PGPAddress + class _EncryptedBase(CollectionMixin): - pass + def _resource_as_dict(self, address): + """See `CollectionMixin`.""" + return dict(email=address.email, + key_fingerprint=address.key_fingerprint, + key_confirmed=address.key_confirmed, + self_link=self.api.path_to( + '/plugins/{}/addresses/{}'.format(config.name, + address.email) + )) + + def _get_collection(self, request): + """See `CollectionMixin`.""" + return PGPAddress.query().all() @public -class AllAddresses: - pass +class AllAddresses(_EncryptedBase): + def on_get(self, request, response): + """/addresses""" + resource = self._make_collection(request) + return okay(response, etag(resource)) @public -class AnAddress: - pass +class AnAddress(_EncryptedBase): + def __init__(self, email): + self._address = PGPAddress.for_email(email) + + def on_get(self, request, response): + if self._address is None: + return not_found(response) + else: + okay(response, self._resource_as_json(self._address)) diff --git a/src/mailman_pgp/rest/lists.py b/src/mailman_pgp/rest/lists.py index 9141bce..37b8d28 100644 --- a/src/mailman_pgp/rest/lists.py +++ b/src/mailman_pgp/rest/lists.py @@ -73,7 +73,7 @@ class AnEncryptedList(_EncryptedBase): okay(response, self._resource_as_json(self._mlist)) @child() - def key(self, context, segments): + def pubkey(self, context, segments): return AListPubkey(self._mlist), [] diff --git a/src/mailman_pgp/rest/tests/test_addresses.py b/src/mailman_pgp/rest/tests/test_addresses.py new file mode 100644 index 0000000..1730618 --- /dev/null +++ b/src/mailman_pgp/rest/tests/test_addresses.py @@ -0,0 +1,60 @@ +# Copyright (C) 2017 Jan Jancar +# +# This file is a part of the Mailman PGP plugin. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see <http://www.gnu.org/licenses/>. + +"""""" +import unittest +from urllib.error import HTTPError + +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.model.address import PGPAddress +from mailman_pgp.testing.layers import PGPRESTLayer + + +class TestAddresses(unittest.TestCase): + layer = PGPRESTLayer + + def setUp(self): + self.mm_address = getUtility(IUserManager).create_address( + 'anne@example.com') + with transaction() as t: + self.address = PGPAddress(self.mm_address) + t.add(self.address) + + def test_missing_address(self): + with self.assertRaises(HTTPError) as cm: + call_api('http://localhost:9001/3.1/plugins/pgp/addresses/' + 'bart@example.com') + self.assertEqual(cm.exception.code, 404) + + def test_all_addresses(self): + json, response = call_api( + 'http://localhost:9001/3.1/plugins/pgp/addresses/') + self.assertEqual(json['total_size'], 1) + self.assertEqual(len(json['entries']), 1) + addresses = json['entries'] + address = addresses[0] + self.assertEqual(address['email'], self.address.email) + + def test_get_address(self): + json, response = call_api( + 'http://localhost:9001/3.1/plugins/pgp/addresses/' + 'anne@example.com') + self.assertEqual(json['email'], self.address.email) diff --git a/src/mailman_pgp/rest/tests/test_lists.py b/src/mailman_pgp/rest/tests/test_lists.py index a674cc9..c5c9854 100644 --- a/src/mailman_pgp/rest/tests/test_lists.py +++ b/src/mailman_pgp/rest/tests/test_lists.py @@ -68,7 +68,7 @@ class TestLists(TestCase): json, response = call_api( 'http://localhost:9001/3.1/plugins/pgp/lists/' - 'another.example.com/key') + 'another.example.com/pubkey') json.pop('http_etag') self.assertEqual(len(json.keys()), 2) |
