fix(minor): fix some tests and merge old and new website tests

This commit is contained in:
Rushabh Mehta 2021-02-15 17:43:17 +05:30
parent 75ba0ca6a9
commit e79e142061
6 changed files with 113 additions and 76 deletions

View file

@ -148,7 +148,7 @@
"label": "Print Style"
},
{
"default": "Modern",
"default": "Redesign",
"fieldname": "print_style",
"fieldtype": "Link",
"in_list_view": 1,
@ -183,7 +183,7 @@
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2020-10-22 23:42:09.471022",
"modified": "2021-02-15 14:16:18.474254",
"modified_by": "Administrator",
"module": "Printing",
"name": "Print Settings",

View file

@ -4,13 +4,19 @@ import unittest
import frappe
from frappe.website import render
from frappe.website import serve
from frappe.website.utils import get_home_page
from frappe.utils import set_request
class TestWebsite(unittest.TestCase):
def setUp(self):
frappe.set_user('Guest')
def tearDown(self):
frappe.set_user('Administrator')
def test_home_page_for_role(self):
frappe.set_user('Administrator')
frappe.delete_doc_if_exists('User', 'test-user-for-home-page@example.com')
frappe.delete_doc_if_exists('Role', 'home-page-test')
frappe.delete_doc_if_exists('Web Page', 'home-page-test')
@ -44,7 +50,6 @@ class TestWebsite(unittest.TestCase):
self.assertEqual(get_home_page(), 'test-portal-home')
def test_page_load(self):
frappe.set_user('Guest')
set_request(method='POST', path='login')
response = render.render()
@ -54,10 +59,47 @@ class TestWebsite(unittest.TestCase):
self.assertTrue('// login.js' in html)
self.assertTrue('<!-- login.html -->' in html)
def test_static_page(self):
set_request(method='GET', path='/_test/static-file-test.png')
response = serve.get_response()
self.assertEquals(response.status_code, 200)
def test_error_page(self):
set_request(method='GET', path='/error')
response = serve.get_response()
self.assertEquals(response.status_code, 500)
def test_login(self):
set_request(method='GET', path='/login')
response = serve.get_response()
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)
def test_app(self):
frappe.set_user('Administrator')
set_request(method='GET', path='/app')
response = serve.get_response()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
self.assertTrue('window.app = true;' in html)
frappe.local.session_obj = None
def test_not_found(self):
set_request(method='GET', path='/_test/missing')
response = serve.get_response()
self.assertEquals(response.status_code, 404)
def test_redirect(self):
import frappe.hooks
frappe.set_user('Administrator')
frappe.hooks.website_redirects = [
dict(source=r'/testfrom', target=r'://testto1'),
dict(source=r'/testfromregex.*', target=r'://testto2'),

View file

@ -1,49 +0,0 @@
from __future__ import unicode_literals
import unittest
import frappe
from frappe.website import serve
from frappe.website.utils import get_home_page
from frappe.utils import set_request
class TestWebsite(unittest.TestCase):
def setUp(self):
frappe.set_user('Guest')
def tearDown(self):
frappe.set_user('Administrator')
def test_static_page(self):
set_request(method='GET', path='/_test/static-file-test.png')
response = serve.StaticPage().get()
self.assertEquals(response.status_code, 200)
def test_error_page(self):
set_request(method='GET', path='/error')
response = serve.get_response()
self.assertEquals(response.status_code, 500)
def test_login(self):
set_request(method='GET', path='/login')
response = serve.get_response()
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)
def test_app(self):
frappe.set_user('Administrator')
set_request(method='GET', path='/app')
response = serve.get_response()
self.assertEquals(response.status_code, 200)
html = frappe.safe_decode(response.get_data())
self.assertTrue('window.app = true;' in html)
frappe.local.session_obj = None
def test_not_found(self):
set_request(method='GET', path='/_test/missing')
response = serve.get_response()
self.assertEquals(response.status_code, 404)

View file

@ -29,7 +29,7 @@ def render(path=None, http_status_code=None):
# temp feature flag
if True or frappe.conf.flag_new_website:
from frappe.website.serve import get_response
return get_response()
return get_response(path, http_status_code)
else:
return _render(path, http_status_code)

View file

@ -25,26 +25,34 @@ def get_response(path=None, http_status_code=None):
# there is no way to determine the type of the page based on the route
# so evaluate each type of page sequentially
response = StaticPage(path).get()
response = StaticPage(path, http_status_code).get()
if not response:
response = TemplatePage(path).get()
response = TemplatePage(path, http_status_code).get()
if not response:
response = ListPage(path).get()
response = ListPage(path, http_status_code).get()
if not response:
response = DocumentPage(path).get()
response = DocumentPage(path, http_status_code).get()
if not response:
response = TemplatePage('404').get()
response = PrintPage(path, http_status_code).get()
if not response:
response = TemplatePage('404', 404).get()
except frappe.Redirect:
return build_response(path, "", 301, {
"Location": frappe.flags.redirect_location or (frappe.local.response or {}).get('location'),
"Cache-Control": "no-store, no-cache, must-revalidate"
})
except frappe.PermissionError as e:
response = TemplatePage('403').get()
except:
response = TemplatePage('error').get()
frappe.local.message = cstr(e)
response = NotPermittedPage(path, http_status_code).get()
except Exception as e:
response = TemplatePage('error', getattr(e, 'http_status_code', 500) or http_status_code).get()
return response
class WebPage(object):
def __init__(self, path=None):
def __init__(self, path=None, http_status_code=200):
self.headers = None
self.status_code = 200
self.http_status_code = http_status_code
if not path:
path = frappe.local.request.path
self.path = path.strip('/ ')
@ -240,7 +248,7 @@ class TemplatePage(BaseTemplatePage):
search_path + '/index.md')
def render(self):
return build_response(self.path, self.get_html(), self.status_code, self.headers)
return build_response(self.path, self.get_html(), self.http_status_code, self.headers)
def get_html(self):
# context object should be separate from self for security
@ -304,7 +312,8 @@ class TemplatePage(BaseTemplatePage):
# TODO: self.context.children = self.run_pymodule_method('get_children')
self.context.developer_mode = frappe.conf.developer_mode
self.status_code = self.context.http_status_code or 200
if self.context.http_status_code:
self.http_status_code = self.context.http_status_code
def set_pymodule_properties(self):
for prop in ("base_template_path", "template", "no_cache", "sitemap",
@ -385,18 +394,50 @@ class TemplatePage(BaseTemplatePage):
return html
def set_standard_path(self, path):
self.app = 'frappe'
self.app_path = frappe.get_app_path('frappe')
self.path = path
self.template_path = 'www/{path}.html'.format(path=path)
class ListPage(TemplatePage):
def validate(self):
if frappe.db.get_value('DocType', self.path):
self.app = 'frappe'
self.app_path = frappe.get_app_path('frappe')
self.doctype = self.path
self.path = 'list'
self.template_path = 'www/list.html'
frappe.local.form_dict.doctype = self.doctype
frappe.local.form_dict.doctype = self.path
self.set_standard_path('list')
return True
return False
class PrintPage(TemplatePage):
'''
default path returns a printable object (based on permission)
/Quotation/Q-0001
'''
def validate(self):
parts = self.path.split('/', 1)
if len(parts)==2:
if (frappe.db.get_value('DocType', parts[0])
and frappe.db.get_value(parts[0], parts[1])):
frappe.form_dict.doctype = parts[0]
frappe.form_dict.name = parts[1]
self.set_standard_path('printview')
return True
return False
class NotPermittedPage(TemplatePage):
def validate(self):
frappe.local.message_title = _("Not Permitted")
frappe.local.response['context'] = dict(
indicator_color = 'red',
primary_action = '/login',
primary_label = _('Login'),
fullpage=True
)
self.set_standard_path('message')
return True
class DocumentPage(BaseTemplatePage):
def validate(self):
'''
@ -477,8 +518,5 @@ class DocumentPage(BaseTemplatePage):
return condition_field
class PrintPage(TemplatePage):
pass
class WebFormPage(WebPage):
pass

View file

@ -9,7 +9,7 @@ from frappe.utils import strip_html_tags
no_cache = 1
def get_context(context):
message_context = {}
message_context = frappe._dict()
if hasattr(frappe.local, "message"):
message_context["header"] = frappe.local.message_title
message_context["title"] = strip_html_tags(frappe.local.message_title)
@ -26,4 +26,10 @@ def get_context(context):
if message.get('http_status_code'):
frappe.local.response['http_status_code'] = message['http_status_code']
if not message_context.title:
message_context.title = frappe.form_dict.title
if not message_context.message:
message_context.message = frappe.form_dict.message
return message_context