Skip to content

Commit

Permalink
Merge pull request #44 from mozilla-services/manage-collections-by-bu…
Browse files Browse the repository at this point in the history
…cket

Manage collections by bucket
  • Loading branch information
Natim committed Jun 5, 2015
2 parents 2faf7d1 + dc02a82 commit 84236e3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
15 changes: 7 additions & 8 deletions kinto/tests/test_views_flush.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@


class FlushViewTest(BaseWebTest, unittest.TestCase):

collection_url = '/buckets/beers/collections/barley/records'

def setUp(self):
super(FlushViewTest, self).setUp()

headers = self.headers.copy()
headers.update(**get_user_headers('bob'))
self.app.post('/collections/barley/records',
headers=headers, status=201)
self.app.post(self.collection_url, headers=headers, status=201)
headers.update(**get_user_headers('alice'))
self.app.post('/collections/chocolate/records',
headers=headers, status=201)
self.app.post(self.collection_url, headers=headers, status=201)

def test_returns_405_if_not_enabled_in_configuration(self):
self.app.post('/__flush__', headers=self.headers, status=405)
Expand All @@ -25,11 +26,9 @@ def test_removes_every_records_in_every_collection_for_everyone(self):
self.app.post('/__flush__', headers=self.headers, status=202)

headers.update(**get_user_headers('bob'))
results = self.app.get('/collections/barley/records',
headers=headers)
results = self.app.get(self.collection_url, headers=headers)
self.assertEqual(results.json['items'], [])

headers.update(**get_user_headers('alice'))
results = self.app.get('/collections/chocolate/records',
headers=headers)
results = self.app.get(self.collection_url, headers=headers)
self.assertEqual(results.json['items'], [])
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,65 @@
type="Whole Grain")


class CollectionViewTest(BaseWebTest, unittest.TestCase):
class RecordsViewTest(BaseWebTest, unittest.TestCase):

collection_url = '/buckets/beers/collections/barley/records'
record_url = '/buckets/beers/collections/barley/records/%s'

def test_empty_collection_returns_an_empty_list(self):
response = self.app.get('/collections/barley/records',
headers=self.headers)
response = self.app.get(self.collection_url, headers=self.headers)
self.assertEqual(response.json['items'], [])

def test_individual_collections_can_be_deleted(self):
self.app.post('/collections/barley/records',
headers=self.headers)
self.app.post(self.collection_url, headers=self.headers)
self.app.delete(self.collection_url, headers=self.headers)

self.app.delete('/collections/barley/records',
headers=self.headers)

def test_items_can_be_added_to_collections(self):
response = self.app.post_json('/collections/barley/records',
def test_records_can_be_added_to_collections(self):
response = self.app.post_json(self.collection_url,
MINIMALIST_ITEM,
headers=self.headers)
_id = response.json.get('id')
self.assertIsNotNone(_id)
response = self.app.get('/collections/barley/records/%s' % _id,
headers=self.headers)
response = self.app.get(self.record_url % _id, headers=self.headers)

item = response.json
del item['id']
del item['last_modified']
self.assertEquals(item, MINIMALIST_ITEM)

def test_collections_are_user_bound(self):
def test_collections_are_bound_by_bucket(self):
# Add items in the collections.
response = self.app.post_json('/collections/barley/records',
response = self.app.post_json(self.collection_url,
MINIMALIST_ITEM,
headers=self.headers)
self.app.get('/collections/barley/records/%s' % response.json['id'],
collection_url = self.record_url % response.json['id']
self.app.get(collection_url.replace('beers', 'sodas'),
headers=get_user_headers("alice"), status=404)

def test_collection_items_can_be_accessed_by_id(self):
response = self.app.post_json('/collections/barley/records',
def test_records_can_be_accessed_by_id(self):
response = self.app.post_json(self.collection_url,
MINIMALIST_ITEM,
headers=self.headers)
self.app.get('/collections/barley/records/%s' % response.json['id'],
self.app.get(self.record_url % response.json['id'],
headers=self.headers)

def test_collection_items_can_be_filtered_on_any_field(self):
self.app.post_json('/collections/barley/records',
def test_records_can_be_filtered_on_any_field(self):
self.app.post_json(self.collection_url,
MINIMALIST_ITEM,
headers=self.headers)
response = self.app.get('/collections/barley/records?unknown=1',
response = self.app.get(self.collection_url + '?unknown=1',
headers=self.headers)
self.assertEqual(len(response.json['items']), 0)

def test_collection_items_can_be_sorted_on_any_field(self):
def test_records_can_be_sorted_on_any_field(self):
for i in range(3):
record = MINIMALIST_ITEM.copy()
record['name'] = 'Stout %s' % i
self.app.post_json('/collections/barley/records',
self.app.post_json(self.collection_url,
record,
headers=self.headers)

response = self.app.get('/collections/barley/records?_sort=-name',
response = self.app.get(self.collection_url + '?_sort=-name',
headers=self.headers)
names = [i['name'] for i in response.json['items']]
self.assertEqual(names, ['Stout 2', 'Stout 1', 'Stout 0'])
13 changes: 8 additions & 5 deletions kinto/views/collection.py → kinto/views/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ class Options():


@resource.register(
record_path="/collections/{{collection_id}}/records/{{id}}",
collection_path="/collections/{{collection_id}}/records",
name="collection")
class Collection(resource.BaseResource):
name="record",
record_path=("/buckets/{{bucket_id}}"
"/collections/{{collection_id}}/records/{{id}}"),
collection_path=("/buckets/{{bucket_id}}"
"/collections/{{collection_id}}/records"))
class Record(resource.BaseResource):

mapping = RecordSchema()

def __init__(self, *args, **kwargs):
super(Collection, self).__init__(*args, **kwargs)
super(Record, self).__init__(*args, **kwargs)
self.collection.collection_id = self.request.matchdict['collection_id']
self.collection.parent_id = self.request.matchdict['bucket_id']

def is_known_field(self, field_name):
"""Without schema, any field is considered as known."""
Expand Down

0 comments on commit 84236e3

Please sign in to comment.