def test_dynamic_basic(template_render_tag, template_context, template_strip_tags): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees, tree, item, get_dynamic_trees from sitetree.sitetreeapp import _IDX_ORPHAN_TREES trees = [ compose_dynamic_tree([tree('dynamic1', items=[ item('dynamic1_1', '/dynamic1_1_url', url_as_pattern=False, sort_order=2), item('dynamic1_2', '/dynamic1_2_url', url_as_pattern=False, sort_order=1), ])]), compose_dynamic_tree([tree('dynamic2', items=[ item('dynamic2_1', '/dynamic2_1_url', url_as_pattern=False), item('dynamic2_2', '/dynamic2_2_url', url_as_pattern=False), ])]), ] register_dynamic_trees(*trees, reset_cache=True) # new less-brackets style result = template_strip_tags(template_render_tag('sitetree', 'sitetree_tree from "dynamic1"', template_context())) assert 'dynamic1_1|dynamic1_2' in result assert 'dynamic2_1' not in result register_dynamic_trees(trees) result = template_strip_tags(template_render_tag('sitetree', 'sitetree_tree from "dynamic1"', template_context())) assert 'dynamic1_1|dynamic1_2' in result assert 'dynamic2_1' not in result trees = get_dynamic_trees() assert len(trees[_IDX_ORPHAN_TREES]) == 2 from sitetree.sitetreeapp import _DYNAMIC_TREES _DYNAMIC_TREES.clear()
def test_dynamic_basic(render_template_tag, mock_template_context): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees, tree, item, get_dynamic_trees from sitetree.sitetreeapp import _IDX_ORPHAN_TREES trees = [ compose_dynamic_tree([tree('dynamic1', items=[ item('dynamic1_1', '/dynamic1_1_url', url_as_pattern=False, sort_order=2), item('dynamic1_2', '/dynamic1_2_url', url_as_pattern=False, sort_order=1), ])]), compose_dynamic_tree([tree('dynamic2', items=[ item('dynamic2_1', '/dynamic2_1_url', url_as_pattern=False), item('dynamic2_2', '/dynamic2_2_url', url_as_pattern=False), ])]), ] register_dynamic_trees(*trees, reset_cache=True) # new less-brackets style result = strip_tags(render_template_tag('sitetree', 'sitetree_tree from "dynamic1"', mock_template_context())) assert 'dynamic1_1|dynamic1_2' in result assert 'dynamic2_1' not in result register_dynamic_trees(trees) result = strip_tags(render_template_tag('sitetree', 'sitetree_tree from "dynamic1"', mock_template_context())) assert 'dynamic1_1|dynamic1_2' in result assert 'dynamic2_1' not in result trees = get_dynamic_trees() assert len(trees[_IDX_ORPHAN_TREES]) == 2 from sitetree.sitetreeapp import _DYNAMIC_TREES _DYNAMIC_TREES.clear()
def test_dynamic_attach_from_module(template_render_tag, template_context, template_strip_tags, settings): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees register_dynamic_trees(compose_dynamic_tree('sitetree.tests.testapp', include_trees=['dynamic4'])) result = template_strip_tags(template_render_tag('sitetree', 'sitetree_tree from "dynamic4"', template_context())) assert 'dynamic4_1' in result settings.DEBUG = True with pytest.warns(UserWarning): compose_dynamic_tree('nonexistent') from sitetree.sitetreeapp import _DYNAMIC_TREES _DYNAMIC_TREES.clear()
def test_dynamic_attach_from_module(render_template_tag, mock_template_context, settings): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees register_dynamic_trees(compose_dynamic_tree('sitetree.tests', include_trees=['dynamic4'])) result = strip_tags(render_template_tag('sitetree', 'sitetree_tree from "dynamic4"', mock_template_context())) assert 'dynamic4_1' in result settings.DEBUG = True with pytest.warns(UserWarning): compose_dynamic_tree('nonexistent') from sitetree.sitetreeapp import _DYNAMIC_TREES _DYNAMIC_TREES.clear()
def test_dynamic_attach(template_render_tag, template_context, template_strip_tags, common_tree): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees, tree, item children = [ item('dynamic2_2_child', '/dynamic2_2_url_child', url_as_pattern=False), ] register_dynamic_trees([ compose_dynamic_tree([ tree( 'dynamic1', items=[ item('dynamic1_1', '/dynamic1_1_url', url_as_pattern=False), item('dynamic1_2', '/dynamic1_2_url', url_as_pattern=False), ]) ], target_tree_alias='mytree'), compose_dynamic_tree([ tree('dynamic2', items=[ item('dynamic2_1', '/dynamic2_1_url', url_as_pattern=False), item('dynamic2_2', '/dynamic2_2_url', url_as_pattern=False, children=children), ], title='some_title') ], target_tree_alias='mytree', parent_tree_item_alias='ruweb'), ]) result = template_strip_tags( template_render_tag('sitetree', 'sitetree_tree from "mytree"', template_context())) assert 'Web|dynamic2_1|dynamic2_2' in result assert 'China|dynamic1_1|dynamic1_2' in result from sitetree.sitetreeapp import _DYNAMIC_TREES _DYNAMIC_TREES.clear()
def test_dynamic_only(template_render_tag, template_context, template_strip_tags, monkeypatch): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees, tree, item # If DYNAMIC_ONLY is not set, pytest-django will tell: "Database access not allowed" on any DB access attempt. monkeypatch.setattr('sitetree.sitetreeapp.DYNAMIC_ONLY', 'UNKNOWN') register_dynamic_trees(compose_dynamic_tree([tree('dynamic1', items=[ item('dynamic1_1', '/dynamic1_1_url', url_as_pattern=False, sort_order=2), ])]), reset_cache=True) result = template_strip_tags(template_render_tag('sitetree', 'sitetree_tree from "dynamic1"', template_context())) assert 'dynamic1_1' in result
def test_dynamic_only(render_template_tag, mock_template_context, monkeypatch): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees, tree, item # If DYNAMIC_ONLY is not set, pytest-django will tell: "Database access not allowed" on any DB access attempt. monkeypatch.setattr('sitetree.sitetreeapp.DYNAMIC_ONLY', 'UNKNOWN') register_dynamic_trees(compose_dynamic_tree([tree('dynamic1', items=[ item('dynamic1_1', '/dynamic1_1_url', url_as_pattern=False, sort_order=2), ])]), reset_cache=True) result = strip_tags(render_template_tag('sitetree', 'sitetree_tree from "dynamic1"', mock_template_context())) assert 'dynamic1_1' in result
def test_dynamic_attach(template_render_tag, template_context, template_strip_tags, common_tree): from sitetree.toolbox import compose_dynamic_tree, register_dynamic_trees, tree, item register_dynamic_trees([ compose_dynamic_tree([tree('dynamic1', items=[ item('dynamic1_1', '/dynamic1_1_url', url_as_pattern=False), item('dynamic1_2', '/dynamic1_2_url', url_as_pattern=False), ])], target_tree_alias='mytree'), compose_dynamic_tree([tree('dynamic2', items=[ item('dynamic2_1', '/dynamic2_1_url', url_as_pattern=False), item('dynamic2_2', '/dynamic2_2_url', url_as_pattern=False), ], title='some_title')], target_tree_alias='mytree', parent_tree_item_alias='ruweb'), ]) result = template_strip_tags(template_render_tag('sitetree', 'sitetree_tree from "mytree"', template_context())) assert 'Web|dynamic2_1|dynamic2_2' in result assert 'China|dynamic1_1|dynamic1_2' in result from sitetree.sitetreeapp import _DYNAMIC_TREES _DYNAMIC_TREES.clear()
def basic_test(self, new_style=False, reset_cache=False): trees = ( compose_dynamic_tree(( tree('dynamic_main_root', items=( item('dynamic_main_root_1', 'dynamic_main_root_1_url', url_as_pattern=False, sort_order=2), item('dynamic_main_root_2', 'dynamic_main_root_2_url', url_as_pattern=False, sort_order=1), )), ), target_tree_alias='main'), compose_dynamic_tree(( tree('dynamic_main_sub', items=( item('dynamic_main_sub_1', 'dynamic_main_sub_1_url', url_as_pattern=False, access_by_perms=['auth.add_group', 'auth.change_group']), item('dynamic_main_sub_2', 'dynamic_main_sub_2_url', url_as_pattern=False, access_by_perms=['auth.add_group'], perms_mode_all=False), )), ), target_tree_alias='main', parent_tree_item_alias='for_dynamic'), compose_dynamic_tree(( tree('dynamic', items=( item('dynamic_1', 'dynamic_1_url', children=( item('dynamic_1_sub_1', 'dynamic_1_sub_1_url', url_as_pattern=False), ), url_as_pattern=False), item('dynamic_2', 'dynamic_2_url', url_as_pattern=False), )), )), ) kwargs = { 'reset_cache': reset_cache } if new_style: register_dynamic_trees(*trees, **kwargs) else: register_dynamic_trees(trees, **kwargs) mock_context = get_mock_context(path='/the_same_url/') self.sitetree.cache.init() self.sitetree.set_global_context(mock_context) tree_alias, sitetree_items = self.sitetree.get_sitetree('main') if reset_cache: self.assertEqual(len(sitetree_items), 13) children = self.sitetree.get_children('main', self.tree_main_root) self.assertEqual(len(children), 6) tree_alias, sitetree_items = self.sitetree.get_sitetree('dynamic') self.assertEqual(len(sitetree_items), 9) children = self.sitetree.get_children('dynamic', sitetree_items[0]) self.assertEqual(len(children), 1) else: mock_user = MockUser(True) self.assertFalse(self.sitetree.check_access(sitetree_items[1], {'user': mock_user})) self.assertTrue(self.sitetree.check_access(sitetree_items[2], {'user': mock_user})) self.assertFalse(self.sitetree.check_access(sitetree_items[2], { 'user': MockUser(True, permissions=['dummy.dummy'])})) self.assertEqual(len(sitetree_items), 5) self.assertEqual(sitetree_items[1].perms, set(['auth.add_group', 'auth.change_group'])) self.assertEqual(sitetree_items[3].title, 'dynamic_main_root_1') self.assertEqual(sitetree_items[4].title, 'dynamic_main_root_2') self.assertEqual(sitetree_items[3].sort_order, 2) self.assertEqual(sitetree_items[4].sort_order, 1) self.assertIsNone(getattr(sitetree_items[3], 'perms', None)) children = self.sitetree.get_children('main', self.tree_main_root) self.assertEqual(len(children), 2) tree_alias, sitetree_items = self.sitetree.get_sitetree('dynamic') self.assertEqual(len(sitetree_items), 3) children = self.sitetree.get_children('dynamic', sitetree_items[0]) self.assertEqual(len(children), 1)