| @@ -21,11 +21,13 @@ import mock | |||||
| import os.path | import os.path | ||||
| import shutil | import shutil | ||||
| import tempfile | import tempfile | ||||
| import uuid | |||||
| defaultfile = 'mediaserver.store.pasn1' | defaultfile = 'mediaserver.store.pasn1' | ||||
| class MEDAServer: | class MEDAServer: | ||||
| def __init__(self, fname): | def __init__(self, fname): | ||||
| self._trustedkeys = {} | self._trustedkeys = {} | ||||
| self._objstore = {} | |||||
| app = Klein() | app = Klein() | ||||
| @@ -37,9 +39,15 @@ class MEDAServer: | |||||
| def store(self): | def store(self): | ||||
| pass | pass | ||||
| @app.route('/') | |||||
| def home(request): | |||||
| return 'hello' | |||||
| @app.route('/obj/<id>') | |||||
| def obj_lookup(self, request, id): | |||||
| try: | |||||
| id = uuid.UUID(id) | |||||
| return self._objstore[id][-1].encode() | |||||
| except ValueError: | |||||
| request.setResponseCode(400) | |||||
| except KeyError: | |||||
| request.setResponseCode(404) | |||||
| @app.route('/store') | @app.route('/store') | ||||
| def storeobj(self, request): | def storeobj(self, request): | ||||
| @@ -54,6 +62,8 @@ class MEDAServer: | |||||
| persona = self._trustedkeys[keyuuid] | persona = self._trustedkeys[keyuuid] | ||||
| persona.verify(obj) | persona.verify(obj) | ||||
| self._objstore.setdefault(obj.uuid, []).append(obj) | |||||
| request.setResponseCode(201) | request.setResponseCode(201) | ||||
| except Exception: | except Exception: | ||||
| @@ -104,6 +114,19 @@ class _TestCases(unittest.TestCase): | |||||
| r = self.requests.get('/chash/%s' % h) | r = self.requests.get('/chash/%s' % h) | ||||
| self.assertEqual(r.status_code, 404) | self.assertEqual(r.status_code, 404) | ||||
| def test_objlookup(self): | |||||
| # that when fetching an non-existant object | |||||
| r = self.requests.get('/obj/%s' % str(uuid.uuid4())) | |||||
| # it is 404, not found | |||||
| self.assertEqual(r.status_code, 404) | |||||
| # that passing an invalid uuid | |||||
| r = self.requests.get('/obj/bogusuuid') | |||||
| # it is 400, bad request | |||||
| self.assertEqual(r.status_code, 400) | |||||
| def test_pubkeystorage(self): | def test_pubkeystorage(self): | ||||
| import cli | import cli | ||||
| @@ -133,6 +156,18 @@ class _TestCases(unittest.TestCase): | |||||
| # is rejected | # is rejected | ||||
| self.assertEqual(r.status_code, 401) | self.assertEqual(r.status_code, 401) | ||||
| # that when fetching the object back | |||||
| r = self.requests.get('/obj/%s' % str(persona.uuid)) | |||||
| # it is successful | |||||
| self.assertEqual(r.status_code, 200) | |||||
| # that the returned data | |||||
| fetchobj = MDBase.decode(r.text) | |||||
| # matches what was stored | |||||
| self.assertEqual(fetchobj, persona.get_identity()) | |||||
| # that when stored | # that when stored | ||||
| self.medaserver.store() | self.medaserver.store() | ||||