def test_redirect(self): import frappe.hooks frappe.hooks.website_redirects = [ dict(source=r'/testfrom', target=r'://testto1'), dict(source=r'/testfromregex.*', target=r'://testto2'), dict(source=r'/testsub/(.*)', target=r'://testto3/\1') ] frappe.cache().delete_key('app_hooks') frappe.cache().delete_key('website_redirects') set_request(method='GET', path='/testfrom') response = render.render() self.assertEquals(response.status_code, 301) self.assertEquals(response.headers.get('Location'), r'://testto1') set_request(method='GET', path='/testfromregex/test') response = render.render() self.assertEquals(response.status_code, 301) self.assertEquals(response.headers.get('Location'), r'://testto2') set_request(method='GET', path='/testsub/me') response = render.render() self.assertEquals(response.status_code, 301) self.assertEquals(response.headers.get('Location'), r'://testto3/me') set_request(method='GET', path='/test404') response = render.render() self.assertEquals(response.status_code, 404) delattr(frappe.hooks, 'website_redirects') frappe.cache().delete_key('app_hooks')
def test_category_link(self): # Make a temporary Blog Post (and a Blog Category) blog = make_test_blog() # Visit the blog post page set_request(path=blog.route) blog_page_response = render() blog_page_html = frappe.safe_decode(blog_page_response.get_data()) # On blog post page find link to the category page soup = BeautifulSoup(blog_page_html, "lxml") category_page_link = list( soup.find_all('a', href=re.compile(blog.blog_category)))[0] category_page_url = category_page_link["href"] # Visit the category page (by following the link found in above stage) set_request(path=category_page_url) category_page_response = render() category_page_html = frappe.safe_decode( category_page_response.get_data()) # Category page should contain the blog post title self.assertIn(blog.title, category_page_html) # Cleanup afterwords frappe.delete_doc("Blog Post", blog.name) frappe.delete_doc("Blog Category", blog.blog_category)
def test_homepage_section_custom_html(self): frappe.get_doc({ "doctype": "Homepage Section", "name": "Custom HTML Section", "section_based_on": "Custom HTML", "section_html": '<div class="custom-section">My custom html</div>', }).insert() set_request(method="GET", path="home") response = render() self.assertEqual(response.status_code, 200) html = frappe.safe_decode(response.get_data()) soup = BeautifulSoup(html, "html.parser") sections = soup.find("main").find_all(class_="custom-section") self.assertEqual(len(sections), 1) homepage_section = sections[0] self.assertEqual(homepage_section.text, "My custom html") # cleanup frappe.db.rollback()
def get_html_for_route(route): from frappe.website import render set_request(method="GET", path=route) response = render.render() html = frappe.safe_decode(response.get_data()) return html
def test_meta_tag_generation(self): blogs = frappe.get_all('Blog Post', fields=['name', 'route'], filters={'published': 1, 'route': ('!=', '')}, limit=1) blog = blogs[0] # create meta tags for this route doc = frappe.new_doc('Website Route Meta') doc.append('meta_tags', { 'key': 'type', 'value': 'blog_post' }) doc.append('meta_tags', { 'key': 'og:title', 'value': 'My Blog' }) doc.name = blog.route doc.insert() # set request on this route set_request(path=blog.route) response = render() self.assertTrue(response.status_code, 200) html = response.get_data().decode() self.assertTrue('''<meta name="type" content="blog_post">''' in html) self.assertTrue('''<meta property="og:title" content="My Blog">''' in html)
def test_homepage_section_custom_html(self): frappe.get_doc({ 'doctype': 'Homepage Section', 'name': 'Custom HTML Section', 'section_based_on': 'Custom HTML', 'section_html': '<div class="custom-section">My custom html</div>', }).insert() set_request(method='GET', path='home') response = render() self.assertEquals(response.status_code, 200) html = frappe.safe_decode(response.get_data()) soup = BeautifulSoup(html, 'html.parser') sections = soup.find('main').find_all(class_='custom-section') self.assertEqual(len(sections), 1) homepage_section = sections[0] self.assertEqual(homepage_section.text, 'My custom html') # cleanup frappe.db.rollback()
def test_meta_tag_generation(self): blogs = frappe.get_all("Blog Post", fields=["name", "route"], filters={ "published": 1, "route": ("!=", "") }, limit=1) blog = blogs[0] # create meta tags for this route doc = frappe.new_doc("Website Route Meta") doc.append("meta_tags", {"key": "type", "value": "blog_post"}) doc.append("meta_tags", {"key": "og:title", "value": "My Blog"}) doc.name = blog.route doc.insert() # set request on this route set_request(path=blog.route) response = render() self.assertTrue(response.status_code, 200) html = response.get_data().decode() self.assertTrue("""<meta name="type" content="blog_post">""" in html) self.assertTrue( """<meta property="og:title" content="My Blog">""" in html)
def test_homepage_load(self): set_request(method='GET', path='home') response = render() self.assertEquals(response.status_code, 200) html = frappe.safe_decode(response.get_data()) self.assertTrue('<section class="hero-section' in html)
def test_redirect(self): import frappe.hooks frappe.hooks.website_redirects = [ dict(source=r"/testfrom", target=r"://testto1"), dict(source=r"/testfromregex.*", target=r"://testto2"), dict(source=r"/testsub/(.*)", target=r"://testto3/\1"), ] website_settings = frappe.get_doc("Website Settings") website_settings.append("route_redirects", { "source": "/testsource", "target": "/testtarget" }) website_settings.save() frappe.cache().delete_key("app_hooks") frappe.cache().delete_key("website_redirects") set_request(method="GET", path="/testfrom") response = render.render() self.assertEquals(response.status_code, 301) self.assertEquals(response.headers.get("Location"), r"://testto1") set_request(method="GET", path="/testfromregex/test") response = render.render() self.assertEquals(response.status_code, 301) self.assertEquals(response.headers.get("Location"), r"://testto2") set_request(method="GET", path="/testsub/me") response = render.render() self.assertEquals(response.status_code, 301) self.assertEquals(response.headers.get("Location"), r"://testto3/me") set_request(method="GET", path="/test404") response = render.render() self.assertEquals(response.status_code, 404) set_request(method="GET", path="/testsource") response = render.render() self.assertEquals(response.status_code, 301) self.assertEquals(response.headers.get("Location"), "/testtarget") delattr(frappe.hooks, "website_redirects") frappe.cache().delete_key("app_hooks")
def test_generator_not_found(self): pages = frappe.get_all("Blog Post", fields=["name", "route"], filters={"published": 0}, limit=1) frappe.db.set_value("Blog Post", pages[0].name, "route", "test-route-000") set_request(path="test-route-000") response = render() self.assertTrue(response.status_code, 404)
def test_generator_not_found(self): pages = frappe.get_all('Blog Post', fields=['name', 'route'], filters={'published': 0}, limit =1) frappe.db.set_value('Blog Post', pages[0].name, 'route', 'test-route-000') set_request(path='test-route-000') response = render() self.assertTrue(response.status_code, 404)
def test_generator_view(self): pages = frappe.get_all('Blog Post', fields=['name', 'route'], filters={'published': 1, 'route': ('!=', '')}, limit =1) set_request(path=pages[0].route) response = render() self.assertTrue(response.status_code, 200) html = response.get_data().decode() self.assertTrue('<article class="blog-content" itemscope itemtype="http://schema.org/BlogPosting">' in html)
def test_page_load(self): set_request(method='POST', path='login') response = render.render() self.assertTrue(response.status_code, 200) html = response.get_data() self.assertTrue('/* login-css */' in html) self.assertTrue('// login.js' in html) self.assertTrue('<!-- login.html -->' in html)
def test_homepage_section_card(self): try: frappe.get_doc({ "doctype": "Homepage Section", "name": "Card Section", "section_based_on": "Cards", "section_cards": [ { "title": "Card 1", "subtitle": "Subtitle 1", "content": "This is test card 1", "route": "/card-1", }, { "title": "Card 2", "subtitle": "Subtitle 2", "content": "This is test card 2", "image": "test.jpg", }, ], "no_of_columns": 3, }).insert() except frappe.DuplicateEntryError: pass set_request(method="GET", path="home") response = render() self.assertEqual(response.status_code, 200) html = frappe.safe_decode(response.get_data()) soup = BeautifulSoup(html, "html.parser") sections = soup.find("main").find_all("section") self.assertEqual(len(sections), 3) homepage_section = sections[2] self.assertEqual(homepage_section.h3.text, "Card Section") cards = homepage_section.find_all(class_="card") self.assertEqual(len(cards), 2) self.assertEqual(cards[0].h5.text, "Card 1") self.assertEqual(cards[0].a["href"], "/card-1") self.assertEqual(cards[1].p.text, "Subtitle 2") self.assertEqual( cards[1].find(class_="website-image-lazy")["data-src"], "test.jpg") # cleanup frappe.db.rollback()
def test_homepage_section_card(self): try: frappe.get_doc({ 'doctype': 'Homepage Section', 'name': 'Card Section', 'section_based_on': 'Cards', 'section_cards': [ { 'title': 'Card 1', 'subtitle': 'Subtitle 1', 'content': 'This is test card 1', 'route': '/card-1' }, { 'title': 'Card 2', 'subtitle': 'Subtitle 2', 'content': 'This is test card 2', 'image': 'test.jpg' }, ], 'no_of_columns': 3 }).insert() except frappe.DuplicateEntryError: pass set_request(method='GET', path='home') response = render() self.assertEquals(response.status_code, 200) html = frappe.safe_decode(response.get_data()) soup = BeautifulSoup(html, 'html.parser') sections = soup.find('main').find_all('section') self.assertEqual(len(sections), 3) homepage_section = sections[2] self.assertEqual(homepage_section.h3.text, 'Card Section') cards = homepage_section.find_all(class_="card") self.assertEqual(len(cards), 2) self.assertEqual(cards[0].h5.text, 'Card 1') self.assertEqual(cards[0].a['href'], '/card-1') self.assertEqual(cards[1].p.text, 'Subtitle 2') self.assertEqual( cards[1].find(class_='website-image-lazy')['data-src'], 'test.jpg') # cleanup frappe.db.rollback()
def test_generator_view(self): pages = frappe.get_all( "Blog Post", fields=["name", "route"], filters={"published": 1, "route": ("!=", "")}, limit=1 ) set_request(path=pages[0].route) response = render() self.assertTrue(response.status_code, 200) html = response.get_data() self.assertTrue('<article class="blog-content" itemscope itemtype="http://schema.org/BlogPosting">' in html)
def test_page_load(self): frappe.set_user("Guest") set_request(method="POST", path="login") response = render.render() self.assertEquals(response.status_code, 200) html = frappe.safe_decode(response.get_data()) self.assertTrue("// login.js" in html) self.assertTrue("<!-- login.html -->" in html) frappe.set_user("Administrator")
def test_page_load(self): frappe.set_user('Guest') set_request(method='POST', path='login') response = render.render() self.assertEquals(response.status_code, 200) html = frappe.safe_decode(response.get_data()) self.assertTrue('// login.js' in html) self.assertTrue('<!-- login.html -->' in html) frappe.set_user('Administrator')
def test_page_load(self): frappe.set_user('Guest') set_request(method='POST', path='login') response = render.render() self.assertEquals(response.status_code, 200) html = frappe.safe_decode(response.get_data()) self.assertTrue('/* login-css */' in html) self.assertTrue('// login.js' in html) self.assertTrue('<!-- login.html -->' in html) frappe.set_user('Administrator')
def test_web_page_with_page_builder(self): self.create_web_page() set_request(method="GET", path="test-web-template") response = render() self.assertEqual(response.status_code, 200) html = frappe.safe_decode(response.get_data()) soup = BeautifulSoup(html, "html.parser") sections = soup.find("main").find_all("section") self.assertEqual(len(sections), 2) self.assertEqual(sections[0].find("h2").text, "Test Title") self.assertEqual(sections[0].find("p").text, "test lorem ipsum") self.assertEqual(len(sections[1].find_all("a")), 3)
def test_generator_view(self): pages = frappe.get_all("Blog Post", fields=["name", "route"], filters={ "published": 1, "route": ("!=", "") }, limit=1) set_request(path=pages[0].route) response = render() self.assertTrue(response.status_code, 200) html = response.get_data().decode() self.assertTrue( '<article class="blog-content" itemscope itemtype="http://schema.org/BlogPosting">' in html)
def test_custom_stylesheet(self): self.create_web_page() theme = self.create_website_theme() theme.set_as_default() frappe.conf.developer_mode = 1 set_request(method="GET", path="test-web-template") response = render() self.assertEqual(response.status_code, 200) html = frappe.safe_decode(response.get_data()) soup = BeautifulSoup(html, "html.parser") stylesheet = soup.select_one('link[rel="stylesheet"]') self.assertEqual(stylesheet.attrs["href"], theme.theme_url) frappe.get_doc("Website Theme", "Standard").set_as_default()
def as_page(): """print web page""" from frappe.website.render import render return render(frappe.response["route"], http_status_code=frappe.response.get("http_status_code"))
def get_html_for_route(route): set_request(method='GET', path=route) response = render.render() html = frappe.safe_decode(response.get_data()) return html
def as_page(): """print web page""" return render(frappe.response['route'], http_status_code=frappe.response.get("http_status_code"))
def print_page(): """print web page""" from frappe.website.render import render render(frappe.response['page_name'])
def as_page(): """print web page""" return render(frappe.response['page_name'], http_status_code=frappe.response.get("http_status_code"))
def as_page(): """print web page""" from frappe.website.render import render return render(frappe.response['page_name'], http_status_code=frappe.response.get("http_status_code"))