class Kinto_Buckets(unittest.TestCase): """ Tests to verify the Kinto buckets function Docs: http://kinto.readthedocs.org/en/latest/api/buckets.html """ def setUp(self): self.client = MockClient() def tearDown(self): self.client = None def assert_not_allowed(self, response): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 115) self.assertEqual(response['message'], "Method not allowed on this endpoint.") self.assertEqual(response['code'], 405) self.assertEqual(response['error'], 'Method Not Allowed') def test_create_bucket(self): resource = 'buckets' data = '{"data": {"id": "test_bucket", "foo": "bar"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.post_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') def test_replace_bucket(self): resource = 'buckets/test_bucket' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') def test_retrieve_bucket(self): resource = 'buckets/test_bucket' response = self.client.get_request(resource) self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') def test_retrieve_all_buckets(self): resource = 'buckets' response = self.client.get_request(resource) self.assertIn('data', response) for item in response['data']: self.assertIn('last_modified', item) self.assertIn('id', item) def test_update_bucket_no_data(self): resource = 'buckets/test_bucket' data = '' expected_status_code = 405 if self.client.is_read_only() else 400 response = self.client.patch_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 107) self.assertEqual(response['message'], "Provide at least one of data or permissions") self.assertEqual(response['code'], 400) self.assertEqual(response['error'], 'Invalid parameters') def test_update_bucket_with_data(self): # For now bucket patch only allow permission updates # See kinto#239 resource = 'buckets/test_bucket' data = '{"permissions": {"read": ["basicauth:foobar"]}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.patch_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['permissions']['read'], ['basicauth:foobar']) def test_delete_bucket(self): # Create the bucket resource = 'buckets' data = '{"data": {"id": "delete_bucket"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.post_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'delete_bucket') # Delete the bucket resource = 'buckets/delete_bucket' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('deleted', response['data']) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'delete_bucket') self.assertEqual(response['data']['deleted'], True)
class Kinto_Collections(unittest.TestCase): """ Tests to verify the Kinto collections function Docs: http://kinto.readthedocs.org/en/latest/api/collections.html """ def setUp(self): self.client = MockClient() def tearDown(self): self.client = None def assert_not_allowed(self, response): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 115) self.assertEqual(response['message'], "Method not allowed on this endpoint.") self.assertEqual(response['code'], 405) self.assertEqual(response['error'], 'Method Not Allowed') def test_create_collection(self): # Create the bucket resource = 'buckets/test_bucket' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') # Create the collection resource = 'buckets/test_bucket/collections/test_collection' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_collection') def test_update_collection_no_data(self): resource = 'buckets/test_bucket/collections/test_collection' data = '' expected_status_code = 405 if self.client.is_read_only() else 400 response = self.client.patch_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 107) self.assertEqual(response['message'], "Provide at least one of data or permissions") self.assertEqual(response['code'], 400) self.assertEqual(response['error'], 'Invalid parameters') def test_update_collection_with_data(self): resource = 'buckets/test_bucket/collections/test_collection' data = '{"data": {"fingerprint": "a_new_fingerprint"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.patch_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('fingerprint', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['fingerprint'], 'a_new_fingerprint') self.assertEqual(response['data']['id'], 'test_collection') def test_retrieve_collection(self): resource = 'buckets/test_bucket/collections/test_collection' response = self.client.get_request(resource) self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_collection') def test_delete_collection(self): resource = 'buckets/test_bucket/collections/delete_collection' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'delete_collection') resource = 'buckets/test_bucket/collections/delete_collection' response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('deleted', response['data']) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'delete_collection') self.assertEqual(response['data']['deleted'], True) def test_delete_collection_not_exist(self): resource = 'buckets/test_bucket/collections/invalid_delete' expected_status_code = 405 if self.client.is_read_only() else 404 response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 111) self.assertEqual( response['message'], "The resource you are looking for could not be found.") self.assertEqual(response['code'], 404) self.assertEqual(response['error'], 'Not Found')
class Kinto_Records(unittest.TestCase): """ Tests to verify the Kinto records function Docs: http://kinto.readthedocs.org/en/latest/api/records.html """ def setUp(self): self.client = MockClient() self.record_id = "" def tearDown(self): self.client = None def assert_not_allowed(self, response): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 115) self.assertEqual(response['message'], "Method not allowed on this endpoint.") self.assertEqual(response['code'], 405) self.assertEqual(response['error'], 'Method Not Allowed') def create_record(self, data=""): resource = 'buckets/test_bucket/collections/test_collection/records' data = '{"data": {"test": "%s"}}' % data expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.post_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) self.record_id = "invalid_group" else: self.record_id = response['data']['id'] return response def test_upload_record(self): # Create bucket resource = 'buckets/test_bucket' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') # Create collection resource = 'buckets/test_bucket/collections/test_collection' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_collection') # Create record response = self.create_record("sample_record") if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('test', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['test'], 'sample_record') self.assertEqual(response['data']['id'], self.record_id) def test_replace_record(self): if not self.record_id: self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/'\ + self.record_id data = '{"data": {"test": "new_record"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], self.record_id) self.assertEqual(response['data']['test'], "new_record") def test_update_record(self): if not self.record_id: self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/'\ + self.record_id data = '{"data": {"test": "updated_record"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.patch_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], self.record_id) self.assertEqual(response['data']['test'], "updated_record") # should return one specific record in data, with id and last_modified def test_retrieve_record(self): if not self.record_id or self.record_id == '': self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/'\ + self.record_id expected_status_code = 400 if self.client.is_read_only() else 200 response = self.client.get_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 107) self.assertEqual(response['message'], "path: Invalid record id") self.assertEqual(response['code'], 400) self.assertEqual(response['error'], 'Invalid parameters') else: self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], self.record_id) # should return an array in data, each item being a record with a unique # id and last_modified def test_retrieve_all_records(self): resource = 'buckets/test_bucket/collections/test_collection/records' response = self.client.get_request(resource) self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', record) self.assertIn('id', record) def test_delete_record(self): if not self.record_id or self.record_id == '': self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/'\ + self.record_id expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('deleted', response['data']) self.assertEqual(response['data']['deleted'], True) def test_delete_all_records(self): resource = 'buckets/test_bucket/collections/test_collection/records' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', record) self.assertIn('id', record) self.assertIn('deleted', record) self.assertEqual(record['deleted'], True) def test_badly_formed_records(self): resource = 'buckets/test_bucket/collections/test_collection/records' expected_status_code = 405 if self.client.is_read_only() else 400 data = '{"data": {"test": }' self.client.post_request(resource, data, status_code=expected_status_code) def test_utf8_stored_correctly(self): resource = 'buckets/test_bucket/collections/test_collection/records' expected_status_code = 405 if self.client.is_read_only() else 200 greeting = u'Comment ça va ? Très bien' data = {'data': {'greeting': greeting}} response = self.client.post_json_request( resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertEqual(greeting, response['data']['greeting'])
class Kinto_Records(unittest.TestCase): """ Tests to verify the Kinto records function Docs: http://kinto.readthedocs.org/en/latest/api/records.html """ def setUp(self): self.client = MockClient() self.record_id = "" def tearDown(self): self.client = None def assert_not_allowed(self, response): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 115) self.assertEqual(response['message'], "Method not allowed on this endpoint.") self.assertEqual(response['code'], 405) self.assertEqual(response['error'], 'Method Not Allowed') def create_record(self, data=""): resource = 'buckets/test_bucket/collections/test_collection/records' data = '{"data": {"test": "%s"}}' % data expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.post_request( resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) self.record_id = "invalid_group" else: self.record_id = response['data']['id'] return response def test_upload_record(self): # Create bucket resource = 'buckets/test_bucket' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request( resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') # Create collection resource = 'buckets/test_bucket/collections/test_collection' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request( resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_collection') # Create record response = self.create_record("sample_record") if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('test', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['test'], 'sample_record') self.assertEqual(response['data']['id'], self.record_id) def test_replace_record(self): if not self.record_id: self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/' + self.record_id data = '{"data": {"test": "new_record"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request( resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], self.record_id) self.assertEqual(response['data']['test'], "new_record") def test_update_record(self): if not self.record_id: self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/' + self.record_id data = '{"data": {"test": "updated_record"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.patch_request( resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], self.record_id) self.assertEqual(response['data']['test'], "updated_record") # should return one specific record in data, with id and last_modified def test_retrieve_record(self): if not self.record_id or self.record_id == '': self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/' + self.record_id expected_status_code = 400 if self.client.is_read_only() else 200 response = self.client.get_request( resource, status_code=expected_status_code) if self.client.is_read_only(): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 107) self.assertEqual(response['message'], "path: Invalid record id") self.assertEqual(response['code'], 400) self.assertEqual(response['error'], 'Invalid parameters') else: self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], self.record_id) # should return an array in data, each item being a record with a unique # id and last_modified def test_retrieve_all_records(self): resource = 'buckets/test_bucket/collections/test_collection/records' response = self.client.get_request(resource) self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', record) self.assertIn('id', record) def test_delete_record(self): if not self.record_id or self.record_id == '': self.create_record("sample_record") resource = 'buckets/test_bucket/collections/test_collection/records/' + self.record_id expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.delete_request( resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('deleted', response['data']) self.assertEqual(response['data']['deleted'], True) def test_delete_all_records(self): resource = 'buckets/test_bucket/collections/test_collection/records' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.delete_request( resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', record) self.assertIn('id', record) self.assertIn('deleted', record) self.assertEqual(record['deleted'], True) def test_badly_formed_records(self): resource = 'buckets/test_bucket/collections/test_collection/records' expected_status_code = 405 if self.client.is_read_only() else 400 data = '{"data": {"test": }' response = self.client.post_request( resource, data, status_code=expected_status_code) def test_utf8_stored_correctly(self): resource = 'buckets/test_bucket/collections/test_collection/records' expected_status_code = 405 if self.client.is_read_only() else 200 greeting = u'Comment ça va ? Très bien' data = {'data': {'greeting': greeting}} response = self.client.post_json_request( resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertEqual(greeting, response['data']['greeting'])
class Kinto_Groups(unittest.TestCase): """ Tests to verify the Kinto groups function Docs: http://kinto.readthedocs.org/en/latest/api/groups.html """ def setUp(self): self.client = MockClient() self.group_id = "" def tearDown(self): self.client = None def assert_not_allowed(self, response): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 115) self.assertEqual(response['message'], "Method not allowed on this endpoint.") self.assertEqual(response['code'], 405) self.assertEqual(response['error'], 'Method Not Allowed') def create_group(self, group_id="", data=""): resource = 'buckets/test_bucket/groups/%s' % group_id data = '{"data": {"members": ["%s"]}}' % data expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) self.group_id = "invalid_group" else: self.group_id = response['data']['id'] return response def test_create_group(self): resource = 'buckets/test_bucket' # Create test_bucket expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') # Create group response = self.create_group("test_group", "member_auth") if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('members', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_group') self.assertEqual(response['data']['members'], ['member_auth']) def test_replace_group(self): resource = 'buckets/test_bucket/groups/test_group' data = '{"data": {"members": ["member_auth2"]}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.group_id = response['data']['id'] self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('members', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_group') self.assertEqual(response['data']['members'], ['member_auth2']) def test_retrieve_group(self): if not self.group_id or self.group_id == '': self.create_group("test_group", "member_auth") resource = 'buckets/test_bucket/groups/' + self.group_id expected_status_code = 404 if self.client.is_read_only() else 200 response = self.client.get_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 111) self.assertEqual(response['message'], "The resource you are looking for could not be found.") self.assertEqual(response['code'], 404) self.assertEqual(response['error'], 'Not Found') else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], self.group_id) def test_retrieve_all_groups(self): resource = 'buckets/test_bucket/groups' response = self.client.get_request(resource) self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', record) self.assertIn('id', record) def test_delete_group(self): if not self.group_id or self.group_id == '': self.create_group("test_group", "member_auth") resource = 'buckets/test_bucket/groups/' + self.group_id expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('deleted', response['data']) self.assertEqual(response['data']['id'], self.group_id) self.assertEqual(response['data']['deleted'], True)
class Kinto_Groups(unittest.TestCase): """ Tests to verify the Kinto groups function Docs: http://kinto.readthedocs.org/en/latest/api/groups.html """ def setUp(self): self.client = MockClient() self.group_id = "" def tearDown(self): self.client = None def assert_not_allowed(self, response): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 115) self.assertEqual(response['message'], "Method not allowed on this endpoint.") self.assertEqual(response['code'], 405) self.assertEqual(response['error'], 'Method Not Allowed') def create_group(self, group_id="", data=""): resource = 'buckets/test_bucket/groups/%s' % group_id data = '{"data": {"members": ["%s"]}}' % data expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) self.group_id = "invalid_group" else: self.group_id = response['data']['id'] return response def test_create_group(self): resource = 'buckets/test_bucket' # Create test_bucket expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') # Create group response = self.create_group("test_group", "member_auth") if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('members', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_group') self.assertEqual(response['data']['members'], ['member_auth']) def test_replace_group(self): resource = 'buckets/test_bucket/groups/test_group' data = '{"data": {"members": ["member_auth2"]}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.group_id = response['data']['id'] self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('members', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_group') self.assertEqual(response['data']['members'], ['member_auth2']) def test_retrieve_group(self): if not self.group_id or self.group_id == '': self.create_group("test_group", "member_auth") resource = 'buckets/test_bucket/groups/' + self.group_id expected_status_code = 404 if self.client.is_read_only() else 200 response = self.client.get_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 111) self.assertEqual( response['message'], "The resource you are looking for could not be found.") self.assertEqual(response['code'], 404) self.assertEqual(response['error'], 'Not Found') else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], self.group_id) def test_retrieve_all_groups(self): resource = 'buckets/test_bucket/groups' response = self.client.get_request(resource) self.assertIn('data', response) for record in response['data']: self.assertIn('last_modified', record) self.assertIn('id', record) def test_delete_group(self): if not self.group_id or self.group_id == '': self.create_group("test_group", "member_auth") resource = 'buckets/test_bucket/groups/' + self.group_id expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('deleted', response['data']) self.assertEqual(response['data']['id'], self.group_id) self.assertEqual(response['data']['deleted'], True)
class Kinto_Collections(unittest.TestCase): """ Tests to verify the Kinto collections function Docs: http://kinto.readthedocs.org/en/latest/api/collections.html """ def setUp(self): self.client = MockClient() def tearDown(self): self.client = None def assert_not_allowed(self, response): self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 115) self.assertEqual(response['message'], "Method not allowed on this endpoint.") self.assertEqual(response['code'], 405) self.assertEqual(response['error'], 'Method Not Allowed') def test_create_collection(self): # Create the bucket resource = 'buckets/test_bucket' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'test_bucket') # Create the collection resource = 'buckets/test_bucket/collections/test_collection' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_collection') def test_update_collection_no_data(self): resource = 'buckets/test_bucket/collections/test_collection' data = '' expected_status_code = 405 if self.client.is_read_only() else 400 response = self.client.patch_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 107) self.assertEqual(response['message'], "Provide at least one of data or permissions") self.assertEqual(response['code'], 400) self.assertEqual(response['error'], 'Invalid parameters') def test_update_collection_with_data(self): resource = 'buckets/test_bucket/collections/test_collection' data = '{"data": {"fingerprint": "a_new_fingerprint"}}' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.patch_request(resource, data, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('fingerprint', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['fingerprint'], 'a_new_fingerprint') self.assertEqual(response['data']['id'], 'test_collection') def test_retrieve_collection(self): resource = 'buckets/test_bucket/collections/test_collection' response = self.client.get_request(resource) self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'test_collection') def test_delete_collection(self): resource = 'buckets/test_bucket/collections/delete_collection' expected_status_code = 405 if self.client.is_read_only() else 200 response = self.client.put_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('permissions', response) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertIn('schema', response['data']) self.assertIn('write', response['permissions']) self.assertEqual(response['data']['id'], 'delete_collection') resource = 'buckets/test_bucket/collections/delete_collection' response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('data', response) self.assertIn('deleted', response['data']) self.assertIn('last_modified', response['data']) self.assertIn('id', response['data']) self.assertEqual(response['data']['id'], 'delete_collection') self.assertEqual(response['data']['deleted'], True) def test_delete_collection_not_exist(self): resource = 'buckets/test_bucket/collections/invalid_delete' expected_status_code = 405 if self.client.is_read_only() else 404 response = self.client.delete_request(resource, status_code=expected_status_code) if self.client.is_read_only(): self.assert_not_allowed(response) else: self.assertIn('errno', response) self.assertIn('message', response) self.assertIn('code', response) self.assertIn('error', response) self.assertEqual(response['errno'], 111) self.assertEqual(response['message'], "The resource you are looking for could not be found.") self.assertEqual(response['code'], 404) self.assertEqual(response['error'], 'Not Found')