diff --git a/kinto/tests/support.py b/kinto/tests/support.py index a822047cf..0d8a7dda2 100644 --- a/kinto/tests/support.py +++ b/kinto/tests/support.py @@ -3,40 +3,24 @@ except ImportError: import unittest # NOQA -import webtest - -from cliquet.tests.support import FakeAuthentMixin +from cliquet.tests.support import BaseWebTest as CliquetBaseWebTest from kinto import API_VERSION -def get_request_class(prefix): - - class PrefixedRequestClass(webtest.app.TestRequest): - - @classmethod - def blank(cls, path, *args, **kwargs): - path = '/%s%s' % (prefix, path) - return webtest.app.TestRequest.blank(path, *args, **kwargs) - - return PrefixedRequestClass - - -class BaseWebTest(FakeAuthentMixin): - """Base Web Test to test your cornice service. - - It setups the database before each test and delete it after. - """ +class BaseWebTest(CliquetBaseWebTest): - def __init__(self, *args, **kwargs): - super(BaseWebTest, self).__init__(*args, **kwargs) - self.app = webtest.TestApp("config:config/kinto.ini", - relative_to='.') - self.app.RequestClass = get_request_class(prefix=API_VERSION) - self.db = self.app.app.registry.storage - self.headers.update({ - 'Content-Type': 'application/json', - }) + api_prefix = API_VERSION - def tearDown(self): - super(BaseWebTest, self).tearDown() - self.db.flush() + def get_app_settings(self): + return { + 'cliquet.project_name': 'cloud storage', + 'cliquet.project_docs': 'https://kinto.rtfd.org/', + 'cliquet.basic_auth_enabled': 'true', + 'cliquet.storage_backend': 'cliquet.storage.redis', + 'cliquet.session_backend': 'cliquet.session.redis', + 'fxa-oauth.client_id': '89513028159972bc', + 'fxa-oauth.client_secret': '9aced230585cc0aa2932e2eb871c9a3a7d6458' + 'e59ccf57eb610ea0a3467dd800', + 'fxa-oauth.oauth_uri': 'https://oauth-stable.dev.lcip.org', + 'fxa-oauth.scope': 'profile' + } diff --git a/kinto/tests/test_views_collections.py b/kinto/tests/test_views_collections.py new file mode 100644 index 000000000..65fcf696d --- /dev/null +++ b/kinto/tests/test_views_collections.py @@ -0,0 +1,54 @@ +from .support import BaseWebTest, unittest +from base64 import b64encode + + +def get_user_headers(user): + return { + 'Authorization': 'Basic {0}'.format(b64encode("%s:secret" % user)), + } + +MINIMALIST_ITEM = dict(name="Hulled Barley", + type="Whole Grain") + + +class CollectionViewTest(BaseWebTest, unittest.TestCase): + def test_empty_collection_returns_an_empty_list(self): + response = self.app.get('/collections/barley/records', + 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.delete('/collections/barley/records', + headers=self.headers) + + def test_items_can_be_added_to_collections(self): + response = self.app.post_json('/collections/barley/records', + 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) + + item = response.json + del item['id'] + del item['last_modified'] + self.assertEquals(item, MINIMALIST_ITEM) + + def test_collections_are_user_bound(self): + # Add items in the collections. + response = self.app.post_json('/collections/barley/records', + MINIMALIST_ITEM, + headers=self.headers) + self.app.get('/collections/barley/records/%s' % response.json['id'], + 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', + MINIMALIST_ITEM, + headers=self.headers) + self.app.get('/collections/barley/records/%s' % response.json['id'], + headers=self.headers) diff --git a/kinto/views/collection.py b/kinto/views/collection.py new file mode 100644 index 000000000..20d51d932 --- /dev/null +++ b/kinto/views/collection.py @@ -0,0 +1,10 @@ +from cliquet.resource import crud, BaseResource + + +@crud(path="/collections/{collection_id}/records/{id}", + collection_path="/collections/{collection_id}/records") +class Collection(BaseResource): + + @property + def name(self): + return self.request.matchdict['collection_id']