Repository: Kinto/kinto

Issue:
Crash when querying `GET /buckets/collections`
<details>
  <summary>

<code>ValueError: You cannot build children keys from its parent key.Trying to build type "collection" from object key "/buckets/collections"</code></summary>



```
    return authz_policy.permits(context, principals, permission)
  File "/home/mathieu/Code/Mozilla/kinto/kinto/core/authorization.py", line 76, in permits
    get_bound_permissions=self.get_bound_permissions)
  File "/home/mathieu/Code/Mozilla/kinto/kinto/core/authorization.py", line 169, in check_permission
    return self._check_permission(self.permission_object_id, *args, **kw)
  File "/home/mathieu/Code/Mozilla/kinto/kinto/core/permission/postgresql/__init__.py", line 237, in check_permission
    perms = get_bound_permissions(object_id, permission)
  File "/home/mathieu/Code/Mozilla/kinto/kinto/authorization.py", line 144, in get_bound_permissions
    return build_permissions_set(*args, **kwargs)
  File "/home/mathieu/Code/Mozilla/kinto/kinto/authorization.py", line 136, in build_permissions_set
    granters.add(build_permission_tuple(obj, permission, obj_parts))
  File "/home/mathieu/Code/Mozilla/kinto/kinto/authorization.py", line 105, in build_permission_tuple
    obj_type, '/'.join(obj_parts)))
ValueError: You cannot build children keys from its parent key.Trying to build type "collection" from object key "/buckets/collections". lang=None uid=7447fd40326a7840a5135b3fa2e1acef4c516ece4635b6ebad0522aaa9331c3a
```

</details>



Discussion:

Resolving patch:
diff --git a/kinto/authorization.py b/kinto/authorization.py
--- a/kinto/authorization.py
+++ b/kinto/authorization.py
@@ -1,7 +1,9 @@
-from kinto.core import authorization as core_authorization
+import re
+
 from pyramid.security import IAuthorizationPolicy, Authenticated
 from zope.interface import implementer
 
+from kinto.core import authorization as core_authorization
 
 # Vocab really matters when you deal with permissions. Let's do a quick recap
 # of the terms used here:
@@ -69,23 +71,15 @@
 
 def get_object_type(object_uri):
     """Return the type of an object from its id."""
-
-    obj_parts = object_uri.split('/')
-    if len(obj_parts) % 2 == 0:
-        object_uri = '/'.join(obj_parts[:-1])
-
-    # Order matters here. More precise is tested first.
-    if 'records' in object_uri:
-        obj_type = 'record'
-    elif 'collections' in object_uri:
-        obj_type = 'collection'
-    elif 'groups' in object_uri:
-        obj_type = 'group'
-    elif 'buckets' in object_uri:
-        obj_type = 'bucket'
-    else:
-        obj_type = None
-    return obj_type
+    if re.match(r'/buckets/(.+)/collections/(.+)/records/(.+)?', object_uri):
+        return 'record'
+    if re.match(r'/buckets/(.+)/collections/(.+)?', object_uri):
+        return 'collection'
+    if re.match(r'/buckets/(.+)/groups/(.+)?', object_uri):
+        return 'group'
+    if re.match(r'/buckets/(.+)?', object_uri):
+        return 'bucket'
+    return None
 
 
 def build_permission_tuple(obj_type, unbound_permission, obj_parts):


Test patch:
diff --git a/kinto/tests/test_authorization.py b/kinto/tests/test_authorization.py
--- a/kinto/tests/test_authorization.py
+++ b/kinto/tests/test_authorization.py
@@ -55,6 +55,11 @@ def test_build_perm_set_uri_can_construct_parents_set_uris(self):
             'bucket', 'write', obj_parts),
             (self.bucket_uri, 'write'))
 
+    def test_build_perm_set_supports_buckets_named_collections(self):
+        uri = '/buckets/collections'
+        self.assertEquals(build_permissions_set(uri, 'write'),
+                          set([(uri, 'write')]))
+
     def test_build_permission_tuple_fail_construct_children_set_uris(self):
         obj_parts = self.bucket_uri.split('/')
         # Cannot build record_uri from bucket obj_parts