def test_record_deletion(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.post_json('/models/test/records', MODEL_RECORD, headers=self.headers) record_id = resp.json['id'] # Test 200 resp = self.app.delete('/models/test/records/%s' % record_id, headers=self.headers) self.assertIn('id', resp.body.decode('utf-8')) self.assertRaises(RecordNotFound, self.db.get_record, 'test', record_id) # Test 404 resp = self.app.delete('/models/test/records/%s' % record_id, headers=self.headers, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "location": "path", "name": record_id, "description": "record not found" }], "status": "error" }))
def test_delete_model(self): # 1. Test that the model and its records have been dropped self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.put_json('/models/test/records/123456', MODEL_RECORD, headers=self.headers) record = MODEL_RECORD.copy() record["id"] = resp.json["id"] self.db.store_credentials('foo', {'id': 'alexis', 'key': 'bar'}) self.db.store_credentials('foobar', {'id': 'remy', 'key': 'bar'}) permissions = {self.credentials['id']: ["delete_all_records", "delete_model"], "alexis": ["read_permissions"], "remy": ["update_permissions"]} resp = self.app.put_json( '/models/test/permissions', permissions, headers=self.headers) resp = self.app.delete('/models/test', headers=self.headers) # 2. Test that the returned data is right self.assertEqual(resp.json, force_unicode({ 'definition': MODEL_DEFINITION["definition"], 'records': [record], 'permissions': permissions}))
def test_definition_retrieval(self): self.create_definition() # Verify that the schema is the same resp = self.app.get('/models/%s/definition' % self.model_id, headers=self.headers) definition = force_unicode(self.valid_definition) self.assertDictEqual(resp.json, definition)
def test_permissions_retrieval(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.get('/models/test/permissions', headers=self.headers) permissions = force_unicode( {self.credentials['id']: MODEL_PERMISSIONS}) self.assertDictEqual(resp.json, permissions)
def test_get_model_unknown(self): resp = self.app.get('/models/test', headers=self.headers, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "location": "path", "name": "test", "description": "model not found"}], "status": "error"}))
def test_unknown_model_raises_404(self): resp = self.app.get('/models/unknown/records', {}, headers=self.headers, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "location": "path", "name": "unknown", "description": "model not found"}], "status": "error"}))
def test_permissions_retrieval(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.get('/models/test/permissions', headers=self.headers) permissions = force_unicode( {self.credentials['id']: MODEL_PERMISSIONS} ) self.assertDictEqual(resp.json, permissions)
def test_unknown_record_returns_404(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.get('/models/test/records/1234', headers=self.headers, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "location": "path", "name": "1234", "description": "record not found"}], "status": "error"}))
def test_unknown_model_records_creation(self): resp = self.app.post_json('/models/unknown/records', {}, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "description": "Unknown model unknown", "location": "path", "name": "modelname" }], "status": 'error'} ))
def test_data_retrieval(self): self.create_definition() resp = self.create_data() # Put valid data against this definition self.assertIn('id', resp.body.decode('utf-8')) record_id = resp.json['id'] resp = self.app.get('/models/%s/records/%s' % (self.model_id, record_id), headers=self.headers) self.assertDataCorrect(resp.json, force_unicode(self.valid_data))
def test_record_retrieval(self): # Put valid data against this definition resp = self.create_record() self.assertIn('id', resp.body.decode('utf-8')) record_id = resp.json['id'] resp = self.app.get('/models/%s/records/%s' % (self.model_id, record_id), headers=self.headers) valid_record = force_unicode(self.valid_record) valid_record["id"] = record_id self.assertDataCorrect(resp.json, valid_record)
def test_definition_retrieval(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.get('/models/test/definition', headers=self.headers) definition = MODEL_DEFINITION['definition'].copy() definition['fields'][0].update(hint='An integer', label='', required=False) self.assertDictEqual(resp.json, force_unicode(definition))
def test_get_model_unknown(self): resp = self.app.get('/models/test', headers=self.headers, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "location": "path", "name": "test", "description": "model not found" }], "status": "error" }))
def test_unknown_model_records_creation(self): resp = self.app.post_json('/models/unknown/records', {}, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "description": "Unknown model unknown", "location": "path", "name": "modelname" }], "status": 'error' }))
def test_put_permissions(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) self.db.store_credentials('foo', {'id': 'alexis', 'key': 'bar'}) self.db.store_credentials('foobar', {'id': 'remy', 'key': 'bar'}) resp = self.app.put_json('/models/test/permissions', {"alexis": ["read_permissions"], "remy": ["update_permissions"]}, headers=self.headers) permissions = dict() permissions["alexis"] = ["read_permissions"] permissions["remy"] = ["update_permissions"] self.assertDictEqual(resp.json, force_unicode(permissions))
def test_unknown_model_raises_404(self): resp = self.app.get('/models/unknown/records', {}, headers=self.headers, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "location": "path", "name": "unknown", "description": "model not found" }], "status": "error" }))
def test_put_permissions(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) self.db.store_credentials('foo', {'id': 'alexis', 'key': 'bar'}) self.db.store_credentials('foobar', {'id': 'remy', 'key': 'bar'}) resp = self.app.put_json('/models/test/permissions', { "alexis": ["read_permissions"], "remy": ["update_permissions"] }, headers=self.headers) permissions = dict() permissions["alexis"] = ["read_permissions"] permissions["remy"] = ["update_permissions"] self.assertDictEqual(resp.json, force_unicode(permissions))
def test_unknown_record_returns_404(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.get('/models/test/records/1234', headers=self.headers, status=404) self.assertDictEqual( resp.json, force_unicode({ "errors": [{ "location": "path", "name": "1234", "description": "record not found" }], "status": "error" }))
def test_record_deletion(self): self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.post_json('/models/test/records', MODEL_RECORD, headers=self.headers) record_id = resp.json['id'] # Test 200 resp = self.app.delete('/models/test/records/%s' % record_id, headers=self.headers) self.assertIn('id', resp.body.decode('utf-8')) self.assertRaises(RecordNotFound, self.db.get_record, 'test', record_id) # Test 404 resp = self.app.delete('/models/test/records/%s' % record_id, headers=self.headers, status=404) self.assertDictEqual(resp.json, force_unicode({ "errors": [{ "location": "path", "name": record_id, "description": "record not found"}], "status": "error"}))
def test_delete_model(self): # 1. Test that the model and its records have been dropped self.app.put_json('/models/test', MODEL_DEFINITION, headers=self.headers) resp = self.app.put_json('/models/test/records/123456', MODEL_RECORD, headers=self.headers) record = MODEL_RECORD.copy() record["id"] = resp.json["id"] self.db.store_credentials('foo', {'id': 'alexis', 'key': 'bar'}) self.db.store_credentials('foobar', {'id': 'remy', 'key': 'bar'}) permissions = { self.credentials['id']: ["delete_all_records", "delete_model"], "alexis": ["read_permissions"], "remy": ["update_permissions"] } resp = self.app.put_json('/models/test/permissions', permissions, headers=self.headers) resp = self.app.delete('/models/test', headers=self.headers) # 2. Test that the returned data is right self.assertEqual( resp.json, force_unicode({ 'definition': MODEL_DEFINITION["definition"], 'records': [record], 'permissions': permissions }))