Fixes in tests and html escaping

This commit is contained in:
Antonio de la Rosa 2016-07-25 05:34:13 +02:00
parent 9e1e48e0e9
commit 61a2d53308
10 changed files with 77 additions and 22 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
from bottle import request import json, re
from bottle import request, response
from paramecio.citoplasma.sessions import get_session from paramecio.citoplasma.sessions import get_session
from paramecio.citoplasma.keyutils import create_key_encrypt from paramecio.citoplasma.keyutils import create_key_encrypt
@ -19,7 +20,27 @@ except:
no_csrf=False no_csrf=False
def filter_ajax(data, filter_tags=True):
response.set_header('Content-type', 'application/json')
#arr_data=map(
json_encoded=json.dumps(data)
#if filter_tags:
# json_encoded=json_encoded.replace('<', '&lt;').replace('>', '&gt;')
#json_encoded=re.sub(r'\\"', '&quot;', json_encoded)
#json_encoded=re.sub('\\"', "", json_encoded)
#json_encoded=re.sub('\"', "&quot;", json_encoded)
#replace('\\"', '&quot;')
#replace('\\\\', '${slashes}').
return json_encoded
class GetPostFiles: class GetPostFiles:
# Need this for obtain utf8 valid values # Need this for obtain utf8 valid values

View file

@ -27,7 +27,7 @@ class SendMail:
self.smtp=smtplib.SMTP(host=self.host, port=self.port) self.smtp=smtplib.SMTP(host=self.host, port=self.port)
self.txt_error='' self.txt_error=''
def send(self, from_address, to_address, subject, message, content_type='plain', attachments=[]): def send(self, from_address, to_address: list, subject, message, content_type='plain', attachments=[]):
if self.ssl==True: if self.ssl==True:
@ -106,6 +106,8 @@ class SendMail:
self.smtp.send_message(msg) self.smtp.send_message(msg)
#self.quit()
return True return True
else: else:
@ -161,12 +163,15 @@ class SendMail:
self.smtp.send_message(outer) self.smtp.send_message(outer)
#self.quit()
return True return True
def quit(self): def quit(self):
self.smtp.quit() self.smtp.quit()
def __del__(self): def __del__(self):
self.quit() self.smtp.quit()

View file

@ -42,7 +42,7 @@
% if simplelist.model.fields[field].escape==True: % if simplelist.model.fields[field].escape==True:
<td class="${simplelist.model.fields[field].name}_td">${simplelist.model.fields[field].show_formatted(row[field])}</td> <td class="${simplelist.model.fields[field].name}_td">${simplelist.model.fields[field].show_formatted(row[field])}</td>
% else: % else:
<td class="${simplelist.model.fields[field].name}_td">${simplelist.model.fields[field].show_formatted(row[field])|n}</td> <td class="${simplelist.model.fields[field].name}_td">${str(simplelist.model.fields[field].show_formatted(row[field]))|n}</td>
% endif % endif
% endfor % endfor

View file

@ -1,4 +1,4 @@
from paramecio.cromosoma.webmodel import PhangoField from paramecio.cromosoma.webmodel import PhangoField,WebModel
import json import json
class ArrayField(PhangoField): class ArrayField(PhangoField):
@ -33,7 +33,7 @@ class ArrayField(PhangoField):
final_value=json.dumps(value) final_value=json.dumps(value)
final_value=super().check(final_value) final_value=WebModel.escape_sql(final_value)
return final_value return final_value

View file

@ -1,4 +1,4 @@
from paramecio.cromosoma.webmodel import PhangoField from paramecio.cromosoma.webmodel import WebModel, PhangoField
import json import json
class DictField(PhangoField): class DictField(PhangoField):
@ -18,7 +18,7 @@ class DictField(PhangoField):
value={} value={}
self.error=True self.error=True
self.txt_error='Sorry, the json array is invalid' self.txt_error='Sorry, the json dict is invalid'
elif type(value).__name__!='dict': elif type(value).__name__!='dict':
@ -26,13 +26,13 @@ class DictField(PhangoField):
self.error=True self.error=True
self.txt_error='Sorry, the json array is invalid' self.txt_error='Sorry, the json array is invalid'
for k,v in enumerate(value): for k,v in value.items():
value[k]=self.field_type.check(v) value[k]=self.field_type.check(v)
final_value=json.dumps(value) final_value=json.dumps(value)
final_value=super().check(final_value) #final_value=WebModel.escape_sql(final_value)
return final_value return final_value

View file

@ -7,6 +7,8 @@ class EmailField(CharField):
def check(self, value): def check(self, value):
value=super().check(value)
self.error=False self.error=False
self.txt_error='' self.txt_error=''
@ -16,4 +18,4 @@ class EmailField(CharField):
value="" value=""
self.txt_error='No valid format' self.txt_error='No valid format'
return value return value

View file

@ -989,7 +989,7 @@ class PhangoField:
# Property that define if make escape in show_formatted # Property that define if make escape in show_formatted
self.escape=True self.escape=False
# File related: if the field have a file related, delete the file # File related: if the field have a file related, delete the file
@ -1024,7 +1024,13 @@ class PhangoField:
value=str(value) value=str(value)
value=WebModel.escape_sql(value) value=value.replace('<', '&lt;')
value=value.replace('>', '&gt;')
value=value.replace('"', '&quot;')
#value=WebModel.escape_sql(value)
if value=="": if value=="":
self.txt_error="The field is empty" self.txt_error="The field is empty"

View file

@ -1,6 +1,7 @@
from settings import config from settings import config
from paramecio.cromosoma.webmodel import WebModel from paramecio.cromosoma.webmodel import WebModel
from paramecio.cromosoma import corefields from paramecio.cromosoma import corefields
from paramecio.cromosoma.extrafields.emailfield import EmailField
import unittest import unittest
class TestFieldMethods(unittest.TestCase): class TestFieldMethods(unittest.TestCase):
@ -21,7 +22,7 @@ class TestFieldMethods(unittest.TestCase):
value=field.check("injection_'") value=field.check("injection_'")
self.assertEqual(value, "injection_\\'") self.assertEqual(value, "injection_'")
def test_integerfield(self): def test_integerfield(self):
@ -40,4 +41,19 @@ class TestFieldMethods(unittest.TestCase):
value=integerfield.check("25'") value=integerfield.check("25'")
self.assertEqual(value, "0") self.assertEqual(value, "0")
def test_emailfield(self):
emailfield=EmailField('email')
emailfield.required=True
emailfield.check('exampleweb-t-sys.com')
self.assertTrue(emailfield.error)
emailfield.check('example@web-t-sys.com')
self.assertFalse(emailfield.error)

View file

@ -1,5 +1,6 @@
from settings import config from settings import config
from paramecio.citoplasma import sendmail from paramecio.citoplasma import sendmail
import time
import unittest import unittest
class TestFieldMethods(unittest.TestCase): class TestFieldMethods(unittest.TestCase):
@ -8,10 +9,14 @@ class TestFieldMethods(unittest.TestCase):
s=sendmail.SendMail() s=sendmail.SendMail()
self.assertTrue( s.send(config.portal_email, config.email_test, 'This is a test', 'A message for test a simple email method', content_type='plain', attachments=[]) ) self.assertTrue( s.send(config.portal_email, [config.email_test], 'This is a test', 'A message for test a simple email method', content_type='plain', attachments=[]) )
self.assertTrue( s.send(config.portal_email, config.email_test, 'This is a test', 'A message for test a simple email method in <b>html</b>', content_type='html', attachments=[]) ) time.sleep(70)
self.assertTrue( s.send(config.portal_email, config.email_test, 'This is a test', 'A message for test a simple email method in <b>html</b> and attachments', content_type='html', attachments=['tests/images/image.jpg']) ) self.assertTrue( s.send(config.portal_email, [config.email_test], 'This is a test', 'A message for test a simple email method in <b>html</b>', content_type='html', attachments=[]) )
s.quit() time.sleep(70)
self.assertTrue( s.send(config.portal_email, [config.email_test], 'This is a test', 'A message for test a simple email method in <b>html</b> and attachments', content_type='html', attachments=['tests/images/image.jpg']) )
s.quit()

View file

@ -79,7 +79,7 @@ class TestWebModelMethods(unittest.TestCase):
print('Select and save in an array') print('Select and save in an array')
self.assertEqual(model.select_to_array(['title', 'content']), {1: {'title': 'Example title Updated', 'content': 'New content Updated'}}) self.assertEqual(model.select_to_array(['title', 'content']), [{'id': 1, 'title': 'Example title Updated', 'content': 'New content Updated'}])
model.yes_reset_conditions=True model.yes_reset_conditions=True