From fdcd7aab332d43e5115770bdf9e1f007643521ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Wed, 1 Jul 2015 13:32:12 +0200 Subject: [PATCH] Make sure we don't raise on 412. --- kinto/tests/test_default_bucket.py | 8 ++++++ kinto/views/buckets.py | 45 +++++++++++++++++++----------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/kinto/tests/test_default_bucket.py b/kinto/tests/test_default_bucket.py index 5f0cb7f3bf..349b6661c1 100644 --- a/kinto/tests/test_default_bucket.py +++ b/kinto/tests/test_default_bucket.py @@ -43,3 +43,11 @@ def test_bucket_id_is_an_uuid(self): uuid.UUID(bucket_id) except ValueError: self.fail('bucket_id: %s is not a valid UUID.' % bucket_id) + + def test_second_call_on_default_bucket_doesnt_raise_a_412(self): + self.app.get(self.bucket_url, headers=self.headers) + self.app.get(self.bucket_url, headers=self.headers) + + def test_second_call_on_default_bucket_collection_doesnt_raise_a_412(self): + self.app.get(self.collection_url, headers=self.headers) + self.app.get(self.collection_url, headers=self.headers) diff --git a/kinto/views/buckets.py b/kinto/views/buckets.py index 0aa743f54b..ac34caa94d 100644 --- a/kinto/views/buckets.py +++ b/kinto/views/buckets.py @@ -1,4 +1,4 @@ -from pyramid.httpexceptions import HTTPForbidden +from pyramid.httpexceptions import HTTPForbidden, HTTPPreconditionFailed from pyramid.security import NO_PERMISSION_REQUIRED from pyramid.view import view_config @@ -21,27 +21,38 @@ def default_bucket(request): path = request.path.replace('default', bucket_id) # Make sure bucket exists - # XXX: Is there a better way to do a GET or CREATE? - subrequest = build_request(request, { - 'method': 'PUT', - 'path': '/buckets/%s' % bucket_id, - 'body': {"data": {}}, - 'headers': {'If-None-Match': '*'} - }) - request.invoke_subrequest(subrequest) - - # Make sure the collection exists - subpath = request.matchdict['subpath'] - if subpath.startswith('/collections/'): - # XXX: Is there a better way to do a GET or CREATE? - collection_id = subpath.split('/')[2] + if request.method.lower() != 'put' or \ + not request.path.endswith('buckets/default'): subrequest = build_request(request, { 'method': 'PUT', - 'path': '/buckets/%s/collections/%s' % (bucket_id, collection_id), + 'path': '/buckets/%s' % bucket_id, 'body': {"data": {}}, 'headers': {'If-None-Match': '*'} }) - request.invoke_subrequest(subrequest) + try: + request.invoke_subrequest(subrequest) + except HTTPPreconditionFailed: + # The bucket already existed + pass + + # Make sure the collection exists + subpath = request.matchdict['subpath'] + if subpath.startswith('/collections/'): + collection_id = subpath.split('/')[2] + if request.method.lower() != 'put' or \ + not request.path.endswith(collection_id): + subrequest = build_request(request, { + 'method': 'PUT', + 'path': '/buckets/%s/collections/%s' % ( + bucket_id, collection_id), + 'body': {"data": {}}, + 'headers': {'If-None-Match': '*'} + }) + try: + request.invoke_subrequest(subrequest) + except HTTPPreconditionFailed: + # The bucket already existed + pass subrequest = build_request(request, { 'method': request.method,