Skip to content

Commit 54f1975

Browse files
author
Richard Jones
committed
more cgi form parsing tests, and a fix for an outstanding couple of bugs
1 parent d3549c4 commit 54f1975

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

doc/upgrading.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Migrating from 0.5 to 0.6
1313
- Introduced EMAIL_FROM_TAG config variable.
1414

1515

16-
1716
Migrating from 0.4.x to 0.5.0
1817
=============================
1918

roundup/cgi/client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: client.py,v 1.68 2003-01-14 22:21:35 richard Exp $
1+
# $Id: client.py,v 1.69 2003-01-15 11:07:45 richard Exp $
22

33
__doc__ = """
44
WWW request handler (also used in the stand-alone server).
@@ -1242,6 +1242,9 @@ def parsePropsFromForm(db, cl, form, nodeid=0, num_re=re.compile('^\d+$')):
12421242
# it's a MiniFieldStorage, but may be a comma-separated list
12431243
# of values
12441244
value = [i.strip() for i in value.value.split(',')]
1245+
1246+
# filter out the empty bits
1247+
value = filter(None, value)
12451248
else:
12461249
# multiple values are not OK
12471250
if isinstance(value, type([])):
@@ -1302,12 +1305,13 @@ def parsePropsFromForm(db, cl, form, nodeid=0, num_re=re.compile('^\d+$')):
13021305
elif isinstance(proptype, hyperdb.Multilink):
13031306
# perform link class key value lookup if necessary
13041307
link = proptype.classname
1308+
link_cl = db.classes[link]
13051309
l = []
13061310
for entry in value:
13071311
if not entry: continue
13081312
if not num_re.match(entry):
13091313
try:
1310-
entry = db.classes[link].lookup(entry)
1314+
entry = link_cl.lookup(entry)
13111315
except KeyError:
13121316
raise ValueError, _('property "%(propname)s": '
13131317
'"%(value)s" not an entry of %(classname)s')%{
@@ -1371,6 +1375,10 @@ def parsePropsFromForm(db, cl, form, nodeid=0, num_re=re.compile('^\d+$')):
13711375
if not existing and not value:
13721376
continue
13731377

1378+
# existing will come out unsorted in some cases
1379+
if isinstance(proptype, hyperdb.Multilink):
1380+
existing.sort()
1381+
13741382
# if changed, set it
13751383
if value != existing:
13761384
props[propname] = value

test/test_cgi.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# but WITHOUT ANY WARRANTY; without even the implied warranty of
99
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1010
#
11-
# $Id: test_cgi.py,v 1.2 2003-01-14 22:21:35 richard Exp $
11+
# $Id: test_cgi.py,v 1.3 2003-01-15 11:07:45 richard Exp $
1212

1313
import unittest, os, shutil, errno, sys, difflib, cgi
1414

@@ -70,6 +70,8 @@ def testEmptyString(self):
7070
makeForm({'title': ''})), {})
7171
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
7272
makeForm({'title': ' '})), {})
73+
self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
74+
self.db.issue, makeForm({'title': ['', '']}))
7375

7476
def testSetString(self):
7577
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
@@ -97,10 +99,14 @@ def testEmptyMultilink(self):
9799
def testSetMultilink(self):
98100
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
99101
makeForm({'nosy': '1'})), {'nosy': ['1']})
102+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
103+
makeForm({'nosy': 'admin'})), {'nosy': ['1']})
100104
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
101105
makeForm({'nosy': ['1','2']})), {'nosy': ['1','2']})
102106
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
103107
makeForm({'nosy': '1,2'})), {'nosy': ['1','2']})
108+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
109+
makeForm({'nosy': 'admin,2'})), {'nosy': ['1','2']})
104110

105111
def testEmptyMultilinkSet(self):
106112
nodeid = self.db.issue.create(nosy=['1','2'])
@@ -110,6 +116,48 @@ def testEmptyMultilinkSet(self):
110116
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
111117
makeForm({'nosy': ' '}), nodeid), {'nosy': []})
112118

119+
def testInvalidMultilinkValue(self):
120+
# XXX This is not the current behaviour - should we enforce this?
121+
# self.assertRaises(IndexError, client.parsePropsFromForm, self.db,
122+
# self.db.issue, makeForm({'nosy': '4'}))
123+
self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
124+
self.db.issue, makeForm({'nosy': 'frozzle'}))
125+
self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
126+
self.db.issue, makeForm({'nosy': '1,frozzle'}))
127+
# XXX need a test for the TypeError (where the ML class doesn't define a key
128+
129+
def testMultilinkAdd(self):
130+
nodeid = self.db.issue.create(nosy=['1'])
131+
# do nothing
132+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
133+
makeForm({':add:nosy': ''}), nodeid), {})
134+
135+
# do something ;)
136+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
137+
makeForm({':add:nosy': '2'}), nodeid), {'nosy': ['1','2']})
138+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
139+
makeForm({':add:nosy': '2,mary'}), nodeid), {'nosy': ['1','2','4']})
140+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
141+
makeForm({':add:nosy': ['2','3']}), nodeid), {'nosy': ['1','2','3']})
142+
143+
def testMultilinkRemove(self):
144+
nodeid = self.db.issue.create(nosy=['1','2'])
145+
# do nothing
146+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
147+
makeForm({':remove:nosy': ''}), nodeid), {})
148+
149+
# do something ;)
150+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
151+
makeForm({':remove:nosy': '1'}), nodeid), {'nosy': ['2']})
152+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
153+
makeForm({':remove:nosy': 'admin,2'}), nodeid), {'nosy': []})
154+
self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
155+
makeForm({':remove:nosy': ['1','2']}), nodeid), {'nosy': []})
156+
157+
# remove one that doesn't exist?
158+
self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
159+
self.db.issue, makeForm({':remove:nosy': '4'}), nodeid)
160+
113161
#
114162
# Password
115163
#
@@ -118,6 +166,11 @@ def testEmptyPassword(self):
118166
makeForm({'password': ''})), {})
119167
self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
120168
makeForm({'password': ''})), {})
169+
self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
170+
self.db.user, makeForm({'password': ['', '']}))
171+
self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
172+
self.db.user, makeForm({'password': 'foo',
173+
'password:confirm': ['', '']}))
121174

122175
def testSetPassword(self):
123176
self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,

0 commit comments

Comments
 (0)