Mercurial > p > roundup > code
comparison test/test_cgi.py @ 1380:4ce6820c18fa
fixes to CGI form handling (NEEDS BACKPORTING TO 0.5)
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Tue, 14 Jan 2003 22:21:35 +0000 |
| parents | 9ddb3ab23a3f |
| children | 944bd3c6d365 |
comparison
equal
deleted
inserted
replaced
| 1379:98ec0c4ce7ab | 1380:4ce6820c18fa |
|---|---|
| 6 # | 6 # |
| 7 # This module is distributed in the hope that it will be useful, | 7 # This module is distributed in the hope that it will be useful, |
| 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 # | 10 # |
| 11 # $Id: test_cgi.py,v 1.1 2003-01-14 06:15:58 richard Exp $ | 11 # $Id: test_cgi.py,v 1.2 2003-01-14 22:21:35 richard Exp $ |
| 12 | 12 |
| 13 import unittest, os, shutil, errno, sys, difflib, cgi | 13 import unittest, os, shutil, errno, sys, difflib, cgi |
| 14 | 14 |
| 15 from roundup.cgi import client | 15 from roundup.cgi import client |
| 16 from roundup import init, instance | 16 from roundup import init, instance, password |
| 17 | 17 |
| 18 def makeForm(args): | 18 def makeForm(args): |
| 19 form = cgi.FieldStorage() | 19 form = cgi.FieldStorage() |
| 20 for k,v in args.items(): | 20 for k,v in args.items(): |
| 21 if type(v) is type([]): | 21 if type(v) is type([]): |
| 22 form.list.append([cgi.MiniFieldStorage(k, x) for x in v]) | 22 [form.list.append(cgi.MiniFieldStorage(k, x)) for x in v] |
| 23 else: | 23 else: |
| 24 form.list.append(cgi.MiniFieldStorage(k, v)) | 24 form.list.append(cgi.MiniFieldStorage(k, v)) |
| 25 return form | 25 return form |
| 26 | 26 |
| 27 class FormTestCase(unittest.TestCase): | 27 class FormTestCase(unittest.TestCase): |
| 48 try: | 48 try: |
| 49 shutil.rmtree(self.dirname) | 49 shutil.rmtree(self.dirname) |
| 50 except OSError, error: | 50 except OSError, error: |
| 51 if error.errno not in (errno.ENOENT, errno.ESRCH): raise | 51 if error.errno not in (errno.ENOENT, errno.ESRCH): raise |
| 52 | 52 |
| 53 def testParseNothing(self): | 53 # |
| 54 client.parsePropsFromForm(self.db, self.db.issue, makeForm({})) | 54 # Empty form |
| 55 # | |
| 56 def testNothing(self): | |
| 57 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 58 makeForm({})), {}) | |
| 55 | 59 |
| 56 def testParseNothingWithRequired(self): | 60 def testNothingWithRequired(self): |
| 57 form = makeForm({':required': 'title'}) | 61 form = makeForm({':required': 'title'}) |
| 58 self.assertRaises(ValueError, client.parsePropsFromForm, self.db, | 62 self.assertRaises(ValueError, client.parsePropsFromForm, self.db, |
| 59 self.db.issue, form) | 63 self.db.issue, form) |
| 64 | |
| 65 # | |
| 66 # String | |
| 67 # | |
| 68 def testEmptyString(self): | |
| 69 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 70 makeForm({'title': ''})), {}) | |
| 71 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 72 makeForm({'title': ' '})), {}) | |
| 73 | |
| 74 def testSetString(self): | |
| 75 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 76 makeForm({'title': 'foo'})), {'title': 'foo'}) | |
| 77 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 78 makeForm({'title': 'a\r\nb\r\n'})), {'title': 'a\nb'}) | |
| 79 | |
| 80 def testEmptyStringSet(self): | |
| 81 nodeid = self.db.issue.create(title='foo') | |
| 82 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 83 makeForm({'title': ''}), nodeid), {'title': ''}) | |
| 84 nodeid = self.db.issue.create(title='foo') | |
| 85 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 86 makeForm({'title': ' '}), nodeid), {'title': ''}) | |
| 87 | |
| 88 # | |
| 89 # Multilink | |
| 90 # | |
| 91 def testEmptyMultilink(self): | |
| 92 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 93 makeForm({'nosy': ''})), {}) | |
| 94 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 95 makeForm({'nosy': ' '})), {}) | |
| 96 | |
| 97 def testSetMultilink(self): | |
| 98 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 99 makeForm({'nosy': '1'})), {'nosy': ['1']}) | |
| 100 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 101 makeForm({'nosy': ['1','2']})), {'nosy': ['1','2']}) | |
| 102 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 103 makeForm({'nosy': '1,2'})), {'nosy': ['1','2']}) | |
| 104 | |
| 105 def testEmptyMultilinkSet(self): | |
| 106 nodeid = self.db.issue.create(nosy=['1','2']) | |
| 107 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 108 makeForm({'nosy': ''}), nodeid), {'nosy': []}) | |
| 109 nodeid = self.db.issue.create(nosy=['1','2']) | |
| 110 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue, | |
| 111 makeForm({'nosy': ' '}), nodeid), {'nosy': []}) | |
| 112 | |
| 113 # | |
| 114 # Password | |
| 115 # | |
| 116 def testEmptyPassword(self): | |
| 117 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user, | |
| 118 makeForm({'password': ''})), {}) | |
| 119 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user, | |
| 120 makeForm({'password': ''})), {}) | |
| 121 | |
| 122 def testSetPassword(self): | |
| 123 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user, | |
| 124 makeForm({'password': 'foo', 'password:confirm': 'foo'})), | |
| 125 {'password': 'foo'}) | |
| 126 | |
| 127 def testSetPasswordConfirmBad(self): | |
| 128 self.assertRaises(ValueError, client.parsePropsFromForm, self.db, | |
| 129 self.db.user, makeForm({'password': 'foo'})) | |
| 130 self.assertRaises(ValueError, client.parsePropsFromForm, self.db, | |
| 131 self.db.user, makeForm({'password': 'foo', | |
| 132 'password:confirm': 'bar'})) | |
| 133 | |
| 134 def testEmptyPasswordNOTSet(self): | |
| 135 nodeid = self.db.user.create(username='1', password=password.Password('foo')) | |
| 136 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user, | |
| 137 makeForm({'password': ''}), nodeid), {}) | |
| 138 nodeid = self.db.user.create(username='2', password=password.Password('foo')) | |
| 139 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user, | |
| 140 makeForm({'password': '', 'password:confirm': ''}), nodeid), {}) | |
| 60 | 141 |
| 61 | 142 |
| 62 def suite(): | 143 def suite(): |
| 63 l = [unittest.makeSuite(FormTestCase), | 144 l = [unittest.makeSuite(FormTestCase), |
| 64 ] | 145 ] |
