Skip to content

Commit

Permalink
Make sure we don't raise on 412.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy HUBSCHER committed Jul 1, 2015
1 parent b00df84 commit fdcd7aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
8 changes: 8 additions & 0 deletions kinto/tests/test_default_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
45 changes: 28 additions & 17 deletions kinto/views/buckets.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand Down

0 comments on commit fdcd7aa

Please sign in to comment.