def test_add_resource_type_is_ok_adding_type_again(self): spec = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'id': '', 'revision': '', 'name': '', }, }, ], } rt = qvarn.ResourceType() rt.from_spec(spec) store = qvarn.MemoryObjectStore() api = qvarn.QvarnAPI() api.set_object_store(store) api.add_resource_type(rt) self.assertEqual(api.add_resource_type(rt), None)
def test_get_resource_type_returns_it_when_it_is_known(self): spec = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'id': '', 'revision': '', 'name': '', }, }, ], } rt = qvarn.ResourceType() rt.from_spec(spec) store = qvarn.MemoryObjectStore() api = qvarn.QvarnAPI() api.set_object_store(store) api.add_resource_type(rt) rt2 = api.get_resource_type(spec['path']) self.assertTrue(isinstance(rt2, qvarn.ResourceType)) self.assertEqual(rt2.as_dict(), spec)
def test_updating_resource_type_with_new_version_works(self): spec1 = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'id': '', 'revision': '', 'name': '', }, }, ], } spec2 = copy.deepcopy(spec1) spec2['versions'].append({ 'version': 'v1', 'prototype': { 'id': '', 'revision': '', 'name': '', 'newfield': '', }, }) store = qvarn.MemoryObjectStore() api = qvarn.QvarnAPI() api.set_object_store(store) rt1 = qvarn.ResourceType() rt1.from_spec(spec1) api.add_resource_type(rt1) rt2 = qvarn.ResourceType() rt2.from_spec(spec2) api.add_resource_type(rt2) rt = api.get_resource_type(spec1['path']) self.assertEqual(rt.as_dict(), spec2)
def setUp(self): self.store = qvarn.MemoryObjectStore() spec = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'type': '', 'id': '', 'full_name': '', }, 'subpaths': { 'moar': { 'prototype': { 'nickname': '', }, }, }, }, ], } self.rt = qvarn.ResourceType() self.rt.from_spec(spec) self.coll = qvarn.CollectionAPI() self.coll.set_object_store(self.store) self.coll.set_resource_type(self.rt) self.next_id = 1 self.claims = { 'aud': 'test-client', 'sub': 'test-user', } self.client_id = 'test-client' self.user_id = 'test-user' rule = { 'method': 'GET', 'client_id': self.client_id, 'user_id': self.user_id, 'subpath': '', 'resource_id': '*', 'resource_type': 'subject', 'resource_field': None, 'resource_value': None, } self.store.add_allow_rule(rule)
def test_get_raises_error_if_resource_is_of_wrong_type(self): # We set up a second qvarn.Collection that shares the same # ObjectStore. We add two different resources, one via each # collection. We should only be able to retrieve resources of # the right type from each of the resources. subject = { 'type': 'subject', 'full_name': 'James Bond', } new_subject = self.coll.post(subject) spec2 = { 'type': 'person', 'path': '/persons', 'versions': [ { 'version': 'v0', 'prototype': { 'type': '', 'id': '', 'revision': '', 'full_name': '', }, }, ], } rt2 = qvarn.ResourceType() rt2.from_spec(spec2) coll2 = qvarn.CollectionAPI() coll2.set_object_store(self.store) coll2.set_resource_type(rt2) person = { 'type': 'person', 'full_name': 'James Bond', } new_person = coll2.post(person) self.assertEqual(self.coll.get(new_subject['id']), new_subject) self.assertEqual(coll2.get(new_person['id']), new_person) # Now check we get an error if we retrieve via the wrong # collection. with self.assertRaises(qvarn.NoSuchResource): self.coll.get(new_person['id']) with self.assertRaises(qvarn.NoSuchResource): coll2.get(new_subject['id'])
def setUp(self): self.store = qvarn.MemoryObjectStore() spec = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'type': '', 'id': '', 'revision': '', 'full_name': '', 'names': [ { 'sort_key': '', }, ], 'things': [ { 'things': '', 'other': '', }, ], }, 'subpaths': { 'sub': { 'prototype': { 'subfield': '', }, }, }, }, ], } self.rt = qvarn.ResourceType() self.rt.from_spec(spec) self.coll = qvarn.CollectionAPI() self.coll.set_object_store(self.store) self.coll.set_resource_type(self.rt)
def _get_resource_type_given_type(self, type_name): cond = qvarn.All( qvarn.Equal('id', type_name), qvarn.Equal('type', 'resource_type'), ) results = self._store.get_matches(cond=cond) qvarn.log.log('trace', msg_text='_get_resource_type_given_type', results=results, type_name=type_name) objs = [obj for _, obj in results] if not objs: # pragma: no cover raise qvarn.NoSuchResourceType(type_name) elif len(objs) > 1: # pragma: no cover raise qvarn.TooManyResourceTypes(type_name) rt = qvarn.ResourceType() rt.from_spec(objs[0]['spec']) return rt
def test_returns_routes_for_known_resource_type(self): spec = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'id': '', 'revision': '', 'name': '', }, 'subpaths': { 'sub': { 'prototype': { 'subfoo': '', }, }, }, }, ], } rt = qvarn.ResourceType() rt.from_spec(spec) store = qvarn.MemoryObjectStore() api = qvarn.QvarnAPI() api.set_object_store(store) api.add_resource_type(rt) api.set_base_url('https://qvarn.example.com') dirname = os.path.dirname(qvarn.__file__) dirname = os.path.join(dirname, '../resource_type') resource_types = qvarn.load_resource_types(dirname) for rt in resource_types: api.add_resource_type(rt) self.assertNotEqual(api.find_missing_route('/subjects'), [])
def get_resource_type(self, path): path = self._canonical_path(path) objs = self._get_resource_type_given_path(path) if not objs: qvarn.log.log('error', msg_text='There is no resource type for path', path=path) raise qvarn.NoSuchResourceType(path) # This is disabled until we solve the problem of having multiple # Qvarn instances creating the same resource types at startup # (or, later, via the API). # elif len(objs) > 1: # pragma: no cover # qvarn.log.log( # 'error', # msg_text='There are more than one resource types for path', # path=path, # objs=objs) # raise qvarn.TooManyResourceTypes(path) rt = qvarn.ResourceType() rt.from_spec(objs[0]['spec']) return rt
def setUp(self): self.spec = { 'type': 'foo', 'path': '/foos', 'versions': [ { 'version': 'v0', 'prototype': { 'type': '', 'id': '', 'revision': '', 'foo': '', }, 'subpaths': { 'sub': { 'prototype': { 'subfield': '', }, }, }, }, ] } self.resource_type = qvarn.ResourceType() self.resource_type.from_spec(self.spec) self.validator = qvarn.Validator() self.resource = { 'type': 'foo', 'id': 'resource-1', 'revision': 'revision-1', 'foo': 'this is foo', } self.subresource = { 'subfield': 'bananarama', }
def setUp(self): spec = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v1', 'prototype': { 'foo': '', 'bars': [ { 'foobar': '', 'yo': '', 'names': [''], }, ], }, }, ], } self.rt = qvarn.ResourceType() self.rt.from_spec(spec) self.proto = self.rt.get_latest_prototype()
'user': None, 'min_conn': 1, 'max_conn': 1, 'password': None, }, } config_filename = os.environ.get('QVARN_CONFIG', DEFAULT_CONFIG_FILE) actual_config = read_config(config_filename) config = dict(default_config) config.update(actual_config or {}) check_config(config) qvarn.setup_logging(config) qvarn.log.log('info', msg_text='Qvarn backend starting') subject = qvarn.ResourceType() subject.from_spec({ 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'id': '', 'type': '', 'revision': '',
def test_sets_type(self): rt = qvarn.ResourceType() rt.set_type('subject') self.assertEqual(rt.get_type(), 'subject')
def test_initially_has_no_path(self): rt = qvarn.ResourceType() self.assertEqual(rt.get_path(), None)
def test_load_resource_spec(self): spec = { 'type': 'subject', 'path': '/subjects', 'versions': [ { 'version': 'v0', 'prototype': { 'foo': '', }, }, { 'version': 'v1', 'prototype': { 'foo': '', 'bar': '', 'version': 0, # test for bug }, 'subpaths': { 'subfoo': { 'prototype': { 'subbar': '', }, }, 'blob': { 'prototype': { 'blob': 'blob', 'content-type': '', }, }, }, 'files': [ 'blob', ], }, ], } rt = qvarn.ResourceType() rt.from_spec(spec) self.assertEqual(rt.get_type(), spec['type']) self.assertEqual(rt.get_path(), spec['path']) self.assertEqual(rt.get_all_versions(), ['v0', 'v1']) self.assertEqual(rt.get_version('v0'), spec['versions'][0]) self.assertEqual(rt.get_version('v1'), spec['versions'][1]) with self.assertRaises(KeyError): rt.get_version('v999') self.assertEqual( rt.get_latest_version(), spec['versions'][-1]['version']) self.assertEqual( rt.get_latest_prototype(), spec['versions'][-1]['prototype']) self.assertEqual(rt.as_dict(), spec) subpaths = spec['versions'][-1]['subpaths'] self.assertEqual( rt.get_subpaths(), { 'subfoo': subpaths['subfoo']['prototype'], 'blob': subpaths['blob']['prototype'], }, ) self.assertEqual( rt.get_subprototype('subfoo'), spec['versions'][-1]['subpaths']['subfoo']['prototype'] ) self.assertEqual(rt.get_files(), ['blob']) self.assertEqual( rt.get_subprototype('blob'), { 'blob': 'blob', 'content-type': '', } )
def test_initially_has_no_latest_version(self): rt = qvarn.ResourceType() self.assertEqual(rt.get_latest_version(), None)
def test_sets_path(self): rt = qvarn.ResourceType() rt.set_path('/subjects') self.assertEqual(rt.get_path(), '/subjects')