Mercurial > p > roundup > code
comparison test/test_cgi.py @ 5700:f90a534cb112
Change output comparison from strings to comparison on python
dicts. String representation of the json data had fields in different
order causing failure for python3 compared to python2.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 08 Apr 2019 22:45:47 -0400 |
| parents | b1ab8bd18e79 |
| children | 92c1864d5dd2 |
comparison
equal
deleted
inserted
replaced
| 5699:b1ab8bd18e79 | 5700:f90a534cb112 |
|---|---|
| 16 from roundup.exceptions import UsageError | 16 from roundup.exceptions import UsageError |
| 17 from roundup.cgi.templating import HTMLItem, HTMLRequest, NoTemplate | 17 from roundup.cgi.templating import HTMLItem, HTMLRequest, NoTemplate |
| 18 from roundup.cgi.templating import HTMLProperty, _HTMLItem, anti_csrf_nonce | 18 from roundup.cgi.templating import HTMLProperty, _HTMLItem, anti_csrf_nonce |
| 19 from roundup.cgi.form_parser import FormParser | 19 from roundup.cgi.form_parser import FormParser |
| 20 from roundup import init, instance, password, hyperdb, date | 20 from roundup import init, instance, password, hyperdb, date |
| 21 from roundup.anypy.strings import StringIO, u2s | 21 from roundup.anypy.strings import StringIO, u2s, b2s |
| 22 | 22 |
| 23 # For testing very simple rendering | 23 # For testing very simple rendering |
| 24 from roundup.cgi.engine_zopetal import RoundupPageTemplate | 24 from roundup.cgi.engine_zopetal import RoundupPageTemplate |
| 25 | 25 |
| 26 from .mocknull import MockNull | 26 from .mocknull import MockNull |
| 1029 if os.path.exists(SENDMAILDEBUG): | 1029 if os.path.exists(SENDMAILDEBUG): |
| 1030 os.remove(SENDMAILDEBUG) | 1030 os.remove(SENDMAILDEBUG) |
| 1031 #raise ValueError | 1031 #raise ValueError |
| 1032 | 1032 |
| 1033 def testRestCsrfProtection(self): | 1033 def testRestCsrfProtection(self): |
| 1034 import json | |
| 1034 # set the password for admin so we can log in. | 1035 # set the password for admin so we can log in. |
| 1035 passwd=password.Password('admin') | 1036 passwd=password.Password('admin') |
| 1036 self.db.user.set('1', password=passwd) | 1037 self.db.user.set('1', password=passwd) |
| 1037 | 1038 |
| 1038 out = [] | 1039 out = [] |
| 1042 # rest has no form content | 1043 # rest has no form content |
| 1043 form = cgi.FieldStorage() | 1044 form = cgi.FieldStorage() |
| 1044 form.list = [ | 1045 form.list = [ |
| 1045 cgi.MiniFieldStorage('title', 'A new issue'), | 1046 cgi.MiniFieldStorage('title', 'A new issue'), |
| 1046 cgi.MiniFieldStorage('status', '1'), | 1047 cgi.MiniFieldStorage('status', '1'), |
| 1047 cgi.MiniFieldStorage('pretty', 'false'), | 1048 cgi.MiniFieldStorage('@pretty', 'false'), |
| 1048 cgi.MiniFieldStorage('@apiver', '1'), | 1049 cgi.MiniFieldStorage('@apiver', '1'), |
| 1049 ] | 1050 ] |
| 1050 cl = client.Client(self.instance, None, | 1051 cl = client.Client(self.instance, None, |
| 1051 {'REQUEST_METHOD':'POST', | 1052 {'REQUEST_METHOD':'POST', |
| 1052 'PATH_INFO':'rest/data/issue', | 1053 'PATH_INFO':'rest/data/issue', |
| 1067 cl.write = wh # capture output | 1068 cl.write = wh # capture output |
| 1068 | 1069 |
| 1069 # Should return explanation because content type is text/plain | 1070 # Should return explanation because content type is text/plain |
| 1070 # and not text/xml | 1071 # and not text/xml |
| 1071 cl.handle_rest() | 1072 cl.handle_rest() |
| 1072 self.assertEqual(out[0], "<class 'roundup.exceptions.UsageError'>: Required Header Missing\n") | 1073 self.assertEqual(b2s(out[0]), "<class 'roundup.exceptions.UsageError'>: Required Header Missing\n") |
| 1073 del(out[0]) | 1074 del(out[0]) |
| 1074 | 1075 |
| 1075 cl = client.Client(self.instance, None, | 1076 cl = client.Client(self.instance, None, |
| 1076 {'REQUEST_METHOD':'POST', | 1077 {'REQUEST_METHOD':'POST', |
| 1077 'PATH_INFO':'rest/data/issue', | 1078 'PATH_INFO':'rest/data/issue', |
| 1092 | 1093 |
| 1093 cl.write = wh # capture output | 1094 cl.write = wh # capture output |
| 1094 | 1095 |
| 1095 # Should work as all required headers are present. | 1096 # Should work as all required headers are present. |
| 1096 cl.handle_rest() | 1097 cl.handle_rest() |
| 1097 self.assertEqual(out[0], '{"data": {"link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1", "id": "1"}}\n') | 1098 answer='{"data": {"link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1", "id": "1"}}\n' |
| 1099 response=json.loads(b2s(out[0])) | |
| 1100 expected=json.loads(answer) | |
| 1101 self.assertEqual(response,expected) | |
| 1098 del(out[0]) | 1102 del(out[0]) |
| 1099 | 1103 |
| 1100 def testXmlrpcCsrfProtection(self): | 1104 def testXmlrpcCsrfProtection(self): |
| 1101 # set the password for admin so we can log in. | 1105 # set the password for admin so we can log in. |
| 1102 passwd=password.Password('admin') | 1106 passwd=password.Password('admin') |
