diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2012-05-14 07:57:09 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2012-05-14 07:57:09 (GMT) |
commit | bda83f7f2ebbc7df68287303497cb631402af73b (patch) | |
tree | 74228f76b784418eab193b592be09d274a98f2f7 | |
parent | b4f2c00fca0649c1aa378a2ed46f2ffb325c116f (diff) |
Do not delete documents immediately to let synchronization possible
-rw-r--r-- | restful_document/router.py | 13 | ||||
-rwxr-xr-x | tests/units/router.py | 41 |
2 files changed, 31 insertions, 23 deletions
diff --git a/restful_document/router.py b/restful_document/router.py index b325713..e84fc6f 100644 --- a/restful_document/router.py +++ b/restful_document/router.py @@ -51,13 +51,19 @@ class Router(object): result = None try: request = Request(environ) + request.principal = self._authenticate(request) _logger.debug('Processing %s request %s: %s', request.method, request.url, request.content or '(no sent data)') - self._authenticate(request) + if request.command == 'DELETE': + # Servers should not delete documents immediately + # to let synchronization possible + request.command = ('PUT', 'delete') + result = ad.call(self._volume, request, response) + except Exception, error: if isinstance(error, ad.Redirect): response.status = '303 See Other' @@ -98,8 +104,7 @@ class Router(object): user = request.envar('sugar_user') if not user: - ad.principal.user = None - return + return ad.Request.ANONYMOUS if user not in self._authenticated and \ (request.path != ['user'] or request.method != 'POST'): @@ -108,4 +113,4 @@ class Router(object): _('Principal user does not exist')) self._authenticated.add(user) - ad.principal.user = user + return user diff --git a/tests/units/router.py b/tests/units/router.py index 4f1499e..a373fc5 100755 --- a/tests/units/router.py +++ b/tests/units/router.py @@ -17,8 +17,8 @@ from restful_document.http import Request class RouterTest(tests.Test): def test_Walkthrough(self): - self.httpd(8000, [tests.User, Document]) - rest = Resource('http://localhost:8000') + self.httpd(8200, [tests.User, Document]) + rest = Resource('http://localhost:8200') guid_1 = rest.post('/document', {'term': 'term', 'stored': 'stored'}) @@ -104,8 +104,8 @@ class RouterTest(tests.Test): rest.get('/document', reply='guid,stored,term')) def test_ServerCrash(self): - self.httpd(8000, [tests.User, Document]) - rest = Resource('http://localhost:8000') + self.httpd(8200, [tests.User, Document]) + rest = Resource('http://localhost:8200') guid_1 = rest.post('/document', {'term': 'term', 'stored': 'stored'}) guid_2 = rest.post('/document', {'term': 'term2', 'stored': 'stored2'}) @@ -118,7 +118,7 @@ class RouterTest(tests.Test): ]), sorted(reply['result'])) - self.httpdown(8000) + self.httpdown(8200) self.httpd(8100, [tests.User, Document]) rest = Resource('http://localhost:8100') @@ -131,13 +131,13 @@ class RouterTest(tests.Test): sorted(reply['result'])) def test_Register(self): - self.httpd(8000, [tests.User]) + self.httpd(8200, [tests.User]) - self.assertRaises(RuntimeError, Resource, 'http://localhost:8000', + self.assertRaises(RuntimeError, Resource, 'http://localhost:8200', uid=tests.UID, privkey=tests.PRIVKEY, pubkey=tests.INVALID_PUBKEY) - rest = Resource('http://localhost:8000', + rest = Resource('http://localhost:8200', uid=tests.UID, privkey=tests.PRIVKEY, pubkey=tests.PUBKEY) self.assertEqual( {'total': 1, @@ -154,7 +154,6 @@ class RouterTest(tests.Test): with ad.SingleVolume(tests.tmpdir + '/db', [tests.User]) as documents: router = Router(documents) - ad.principal.user = None request = Request({ 'HTTP_SUGAR_USER': 'foo', @@ -163,26 +162,25 @@ class RouterTest(tests.Test): 'REQUEST_METHOD': 'GET', }) self.assertRaises(ad.Unauthorized, router._authenticate, request) - self.assertEqual(None, ad.principal.user) request.environ['HTTP_SUGAR_USER'] = tests.UID request.environ['HTTP_SUGAR_USER_SIGNATURE'] = tests.sign(tests.PRIVKEY, tests.UID) - router._authenticate(request) - self.assertEqual(tests.UID, ad.principal.user) + user = router._authenticate(request) + self.assertEqual(tests.UID, user) def test_Authorization(self): - self.httpd(8000, [tests.User, Document]) + self.httpd(8200, [tests.User, Document]) - rest_1 = Resource('http://localhost:8000') + rest_1 = Resource('http://localhost:8200') guid = rest_1.post('/document', {'term': '', 'stored': ''}) - rest_2 = Resource('http://localhost:8000', tests.UID2, tests.PRIVKEY2, tests.PUBKEY2) + rest_2 = Resource('http://localhost:8200', tests.UID2, tests.PRIVKEY2, tests.PUBKEY2) self.assertRaises(RuntimeError, rest_2.put, '/document/' + guid, {'term': 'new'}) self.assertRaises(RuntimeError, rest_2.delete, '/document/' + guid) def test_UrlPath(self): - self.httpd(8000, [tests.User, Document]) - rest = Resource('http://localhost:8000') + self.httpd(8200, [tests.User, Document]) + rest = Resource('http://localhost:8200') # Should `urllib.splithost('//foo')` return `('foo', '')` ? self.assertRaises(RuntimeError, rest.post, '//document/', {'term': 'probe'}) @@ -193,8 +191,8 @@ class RouterTest(tests.Test): rest.get('///document//%s/' % guid, reply='term')) def test_HandleDeletes(self): - self.httpd(8000, [tests.User, Document]) - rest = Resource('http://localhost:8000') + self.httpd(8200, [tests.User, Document]) + rest = Resource('http://localhost:8200') guid = rest.post('/document', {'term': 'probe'}) self.assertEqual( @@ -210,6 +208,11 @@ class RouterTest(tests.Test): rest.get('/document')) self.assertRaises(RuntimeError, rest.get, '/document/' + guid) + self.httpdown(8200) + + volume = ad.SingleVolume(tests.tmpdir + '/db', [Document]) + self.assertEqual(['deleted'], volume['document'].get(guid)['layers']) + class Document(ad.Document): |