def test_allows_access(self): authority = Authority('secure catalog', 'any col') authority.get_roles = lambda: [] self.assertFalse(authority.allows_access()) authority.get_roles = lambda: [role_b] self.assertTrue(authority.allows_access()) authority._catalog = "secure catalog collection" authority._collection = "secure collection" authority.get_roles = lambda: [] self.assertFalse(authority.allows_access()) authority.get_roles = lambda: [role_b] self.assertTrue(authority.allows_access()) authority._collection = "any collection" authority.get_roles = lambda: [] self.assertTrue(authority.allows_access())
def test_filter_row(self): authority = Authority('cat', 'col') authority.get_suppressed_columns = lambda: ['b', 'd'] row = {'a': 1, 'b': 2, 'c': 3} authority.filter_row(row) self.assertEqual(row, {'a': 1, 'b': None, 'c': 3}) authority.allows_access = lambda: False row = {'a': 1, 'b': 2, 'c': 3} authority.filter_row(row) self.assertEqual(row, {'a': None, 'b': None, 'c': None})
def sql(self): self._reset() # Relation without parent is main relation base_collection = [ k for k, v in self.relation_parents.items() if v is None ][0] self._collect_relation_info(base_collection, base_collection) base_info = self._get_relation_info(base_collection) select_fields = [ self._select_expression(base_info, field) for field in [FIELD.GOBID] + self.selects[base_collection]['fields'] ] self.select_expressions.extend(select_fields) # Add catalog and collection to allow for value resolution self.select_expressions.extend([ f"'{base_info['catalog_name']}' AS {CATALOG_NAME}", f"'{base_info['collection_name']}' AS {COLLECTION_NAME}", ]) authority = Authority(base_info['catalog_name'], base_info['collection_name']) if not authority.allows_access(): raise NoAccessException arguments = self._get_arguments_with_defaults( self.selects[base_collection]['arguments']) self.joins.append( self._build_from_table(arguments, base_info['tablename'], base_info['alias'])) del self.selects[base_collection] self._join_relations(self.selects) select = ',\n'.join(self.select_expressions) table_select = '\n'.join(self.joins) order_by = f"ORDER BY {base_info['alias']}.{FIELD.GOBID}" query = f"SELECT\n{select}\n{table_select}\n{order_by}" return query