1 #
2 # Copyright (c) 2003 Richard Jones, rjones@ekit-inc.com
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 #
11 # $Id: test_cgi.py,v 1.3 2003-01-15 11:07:45 richard Exp $
13 import unittest, os, shutil, errno, sys, difflib, cgi
15 from roundup.cgi import client
16 from roundup import init, instance, password
18 def makeForm(args):
19 form = cgi.FieldStorage()
20 for k,v in args.items():
21 if type(v) is type([]):
22 [form.list.append(cgi.MiniFieldStorage(k, x)) for x in v]
23 else:
24 form.list.append(cgi.MiniFieldStorage(k, v))
25 return form
27 class FormTestCase(unittest.TestCase):
28 def setUp(self):
29 self.dirname = '_test_cgi_form'
30 try:
31 shutil.rmtree(self.dirname)
32 except OSError, error:
33 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
34 # create the instance
35 init.install(self.dirname, 'classic', 'anydbm')
36 init.initialise(self.dirname, 'sekrit')
37 # check we can load the package
38 self.instance = instance.open(self.dirname)
39 # and open the database
40 self.db = self.instance.open('admin')
41 self.db.user.create(username='Chef', address='chef@bork.bork.bork',
42 realname='Bork, Chef', roles='User')
43 self.db.user.create(username='mary', address='mary@test',
44 roles='User', realname='Contrary, Mary')
46 def tearDown(self):
47 self.db.close()
48 try:
49 shutil.rmtree(self.dirname)
50 except OSError, error:
51 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
53 #
54 # Empty form
55 #
56 def testNothing(self):
57 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
58 makeForm({})), {})
60 def testNothingWithRequired(self):
61 form = makeForm({':required': 'title'})
62 self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
63 self.db.issue, form)
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 self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
74 self.db.issue, makeForm({'title': ['', '']}))
76 def testSetString(self):
77 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
78 makeForm({'title': 'foo'})), {'title': 'foo'})
79 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
80 makeForm({'title': 'a\r\nb\r\n'})), {'title': 'a\nb'})
82 def testEmptyStringSet(self):
83 nodeid = self.db.issue.create(title='foo')
84 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
85 makeForm({'title': ''}), nodeid), {'title': ''})
86 nodeid = self.db.issue.create(title='foo')
87 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
88 makeForm({'title': ' '}), nodeid), {'title': ''})
90 #
91 # Multilink
92 #
93 def testEmptyMultilink(self):
94 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
95 makeForm({'nosy': ''})), {})
96 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
97 makeForm({'nosy': ' '})), {})
99 def testSetMultilink(self):
100 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
101 makeForm({'nosy': '1'})), {'nosy': ['1']})
102 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
103 makeForm({'nosy': 'admin'})), {'nosy': ['1']})
104 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
105 makeForm({'nosy': ['1','2']})), {'nosy': ['1','2']})
106 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
107 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']})
111 def testEmptyMultilinkSet(self):
112 nodeid = self.db.issue.create(nosy=['1','2'])
113 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
114 makeForm({'nosy': ''}), nodeid), {'nosy': []})
115 nodeid = self.db.issue.create(nosy=['1','2'])
116 self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
117 makeForm({'nosy': ' '}), nodeid), {'nosy': []})
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
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), {})
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']})
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), {})
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': []})
157 # remove one that doesn't exist?
158 self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
159 self.db.issue, makeForm({':remove:nosy': '4'}), nodeid)
161 #
162 # Password
163 #
164 def testEmptyPassword(self):
165 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
166 makeForm({'password': ''})), {})
167 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
168 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': ['', '']}))
175 def testSetPassword(self):
176 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
177 makeForm({'password': 'foo', 'password:confirm': 'foo'})),
178 {'password': 'foo'})
180 def testSetPasswordConfirmBad(self):
181 self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
182 self.db.user, makeForm({'password': 'foo'}))
183 self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
184 self.db.user, makeForm({'password': 'foo',
185 'password:confirm': 'bar'}))
187 def testEmptyPasswordNOTSet(self):
188 nodeid = self.db.user.create(username='1', password=password.Password('foo'))
189 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
190 makeForm({'password': ''}), nodeid), {})
191 nodeid = self.db.user.create(username='2', password=password.Password('foo'))
192 self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
193 makeForm({'password': '', 'password:confirm': ''}), nodeid), {})
196 def suite():
197 l = [unittest.makeSuite(FormTestCase),
198 ]
199 return unittest.TestSuite(l)
202 # vim: set filetype=python ts=4 sw=4 et si