From 862174914d3a85fecf946ae58578a9080c18a631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Wed, 25 Feb 2015 17:06:51 +0100 Subject: [PATCH 1/3] Expose a /collections/ endpoint to deal with collections. --- config/kinto.ini | 2 +- kinto/tests/test_views_collections.py | 40 +++++++++++++++++++++++++++ kinto/views/collection.py | 10 +++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 kinto/tests/test_views_collections.py create mode 100644 kinto/views/collection.py diff --git a/config/kinto.ini b/config/kinto.ini index ebac1b1f2..64b374e54 100644 --- a/config/kinto.ini +++ b/config/kinto.ini @@ -10,7 +10,7 @@ cliquet.retry_after = 30 cliquet.eos = pyramid.debug_notfound = true -# cliquet.basic_auth_backdoor = true +cliquet.basic_auth_backdoor = true # cliquet.backoff = 10 cliquet.userid_hmac_secret = b4c96a8692291d88fe5a97dd91846eb4 # cliquet.batch_max_requests = 25 diff --git a/kinto/tests/test_views_collections.py b/kinto/tests/test_views_collections.py new file mode 100644 index 000000000..95c04b04d --- /dev/null +++ b/kinto/tests/test_views_collections.py @@ -0,0 +1,40 @@ +from .support import BaseWebTest, unittest +from base64 import b64encode + + +def get_user_headers(user): + return { + 'Authorization': 'Basic {0}'.format(b64encode("%s:secret" % user)), + } + + +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('/collections/barley/records', + headers=self.headers) + self.assertIsNotNone(response.json['id']) + + def test_collections_are_user_bound(self): + # Add items in the collections. + response = self.app.post('/collections/barley/records', + 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('/collections/barley/records', + 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'] From c6f2d09aab3197f432b3355e165635420cac3a27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Thu, 26 Feb 2015 17:29:27 +0100 Subject: [PATCH 2/3] Update the tests to actually create records --- kinto/tests/test_views_collections.py | 28 ++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/kinto/tests/test_views_collections.py b/kinto/tests/test_views_collections.py index 95c04b04d..65fcf696d 100644 --- a/kinto/tests/test_views_collections.py +++ b/kinto/tests/test_views_collections.py @@ -7,6 +7,9 @@ def get_user_headers(user): '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): @@ -22,19 +25,30 @@ def test_individual_collections_can_be_deleted(self): headers=self.headers) def test_items_can_be_added_to_collections(self): - response = self.app.post('/collections/barley/records', - headers=self.headers) - self.assertIsNotNone(response.json['id']) + 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('/collections/barley/records', - headers=self.headers) + 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('/collections/barley/records', - headers=self.headers) + 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) From f468020e8f3370127c5964cd105d57c74bf73403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Fri, 27 Feb 2015 13:27:18 +0100 Subject: [PATCH 3/3] update to last cliquet version --- config/kinto.ini | 2 +- kinto/tests/support.py | 48 ++++++++++++++---------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/config/kinto.ini b/config/kinto.ini index 64b374e54..ebac1b1f2 100644 --- a/config/kinto.ini +++ b/config/kinto.ini @@ -10,7 +10,7 @@ cliquet.retry_after = 30 cliquet.eos = pyramid.debug_notfound = true -cliquet.basic_auth_backdoor = true +# cliquet.basic_auth_backdoor = true # cliquet.backoff = 10 cliquet.userid_hmac_secret = b4c96a8692291d88fe5a97dd91846eb4 # cliquet.batch_max_requests = 25 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' + }