def test_bookmarks(web_fixture): """Bookmarks normally refer to the current locale. You can override that to be a specified locale instead. """ bookmark = Bookmark('/base_path', '/relative_path', 'description') af_bookmark = Bookmark('/base_path', '/relative_path', 'description', locale='af') assert af_bookmark.locale == 'af' assert af_bookmark.href.path == '/af/base_path/relative_path' assert bookmark.locale is None assert bookmark.href.path == '/base_path/relative_path'
def test_populating(web_fixture): """Navs can be populated with a list of A's or Bookmarks.""" # Case: a normal menu from bookmarks item_specs = [Bookmark('/', '/href1', 'description1'), Bookmark('/', '/go_to_href', 'description2')] menu = Nav(web_fixture.view).with_bookmarks(item_specs) tester = WidgetTester(menu) [item1, item2] = menu.menu_items assert item1.a.href.path == '/href1' assert item1.a.children[0].value == 'description1' assert item2.a.href.path == '/go_to_href' assert item2.a.children[0].value == 'description2' #case: using A's a_list = [A.from_bookmark(web_fixture.view, i) for i in item_specs] menu = Nav(web_fixture.view).with_a_list(a_list) [item1, item2] = menu.menu_items assert item1.a is a_list[0] assert item2.a is a_list[1]
def navs(fixture): """A Nav is a menu with css classes for styling by Bootstrap.""" bookmarks = [Bookmark('', '/one', 'One'), Bookmark('', '/two', 'Two')] menu = Nav(fixture.view).with_bookmarks(bookmarks) # A nav is an ul.nav vassert(menu.html_representation.tag_name == 'ul') vassert('nav' in menu.html_representation.get_attribute('class')) # Containing a li for each menu item [one, two] = menu.html_representation.children for item, expected_href, expected_description in [(one, '/one', 'One'), (two, '/two', 'Two')]: vassert(item.tag_name == 'li') vassert(item.get_attribute('class') == 'nav-item') [a] = item.children vassert(a.get_attribute('href') == expected_href) vassert(a.children[0].value == expected_description) vassert(a.get_attribute('class') == 'nav-link')
def bookmarks_support_such_fragments(self, fixture): """Page-internal bookmarks support such bookmarkable widgets. These Bookmarks usually do not affect an URL - they just set widget states in different ways, depending on whether the client has javascript support or not. However, if a page was opened using the widget arguments on the querystring, a bookmark that would normally only have changed that argument on the hash will point to a new url on which the argument has been removed from the querystring and changed on the hash. """ internal_bookmark = Bookmark.for_widget( 'an ajax bookmark', query_arguments={'fancy_state': 2}) normal_bookmark = Bookmark('/', '', 'a normal bookmark') # You can query whether a bookmark is page_internal or not vassert(internal_bookmark.is_page_internal) vassert(not normal_bookmark.is_page_internal) # page-internal bookmarks must be added to normal ones to be useful usable_bookmark = normal_bookmark + internal_bookmark wsgi_app = fixture.new_wsgi_app( widget_factory=A.factory_from_bookmark(usable_bookmark)) # Case: when rendered without javascript browser = Browser(wsgi_app) browser.open('/') a = browser.lxml_html.xpath('//a')[0] vassert(a.attrib['href'] == '/?fancy_state=2') vassert(a.text == 'an ajax bookmark') # Case: when rendered in a browser with javascript support fixture.reahl_server.set_app(wsgi_app) fixture.driver_browser.open('/') vassert( fixture.driver_browser.is_element_present( "//a[@href='/#fancy_state=2']")) vassert(not fixture.driver_browser.is_element_present( "//a[@href='/?fancy_state=2']")) # Case: when the argument was given on the query string of the current page fixture.driver_browser.open('/?fancy_state=4') vassert( fixture.driver_browser.is_element_present( "//a[@href='/#fancy_state=2']"))
def test_bookmark_rights_when_adding(rights_scenarios): """When adding two Bookmarks, access rights of both are taken into account. """ fixture = rights_scenarios internal_bookmark = Bookmark.for_widget('an ajax bookmark', query_arguments={'fancy_state':2}, read_check=fixture.internal_readable, write_check=fixture.internal_writable) normal_bookmark = Bookmark('/', '', 'a normal bookmark', read_check=fixture.normal_readable, write_check=fixture.normal_writable) usable_bookmark = normal_bookmark+internal_bookmark assert usable_bookmark.read_check() is fixture.expected_readable assert usable_bookmark.write_check() is fixture.expected_writable
def new_bookmarks(self): return [Bookmark('', '/one', 'One')]