Code

really fix bug 663235, and test it
[roundup.git] / test / test_cgi.py
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.4 2003-01-15 11:14:01 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 testMultilinkAddNew(self):
144         self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
145             makeForm({':add:nosy': ['2','3']})), {'nosy': ['2','3']})
147     def testMultilinkRemove(self):
148         nodeid = self.db.issue.create(nosy=['1','2'])
149         # do nothing
150         self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
151             makeForm({':remove:nosy': ''}), nodeid), {})
153         # do something ;)
154         self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
155             makeForm({':remove:nosy': '1'}), nodeid), {'nosy': ['2']})
156         self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
157             makeForm({':remove:nosy': 'admin,2'}), nodeid), {'nosy': []})
158         self.assertEqual(client.parsePropsFromForm(self.db, self.db.issue,
159             makeForm({':remove:nosy': ['1','2']}), nodeid), {'nosy': []})
161         # remove one that doesn't exist?
162         self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
163             self.db.issue, makeForm({':remove:nosy': '4'}), nodeid)
165     #
166     # Password
167     #
168     def testEmptyPassword(self):
169         self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
170             makeForm({'password': ''})), {})
171         self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
172             makeForm({'password': ''})), {})
173         self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
174             self.db.user, makeForm({'password': ['', '']}))
175         self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
176             self.db.user, makeForm({'password': 'foo',
177             'password:confirm': ['', '']}))
179     def testSetPassword(self):
180         self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
181             makeForm({'password': 'foo', 'password:confirm': 'foo'})),
182             {'password': 'foo'})
184     def testSetPasswordConfirmBad(self):
185         self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
186             self.db.user, makeForm({'password': 'foo'}))
187         self.assertRaises(ValueError, client.parsePropsFromForm, self.db,
188             self.db.user, makeForm({'password': 'foo',
189             'password:confirm': 'bar'}))
191     def testEmptyPasswordNOTSet(self):
192         nodeid = self.db.user.create(username='1', password=password.Password('foo'))
193         self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
194             makeForm({'password': ''}), nodeid), {})
195         nodeid = self.db.user.create(username='2', password=password.Password('foo'))
196         self.assertEqual(client.parsePropsFromForm(self.db, self.db.user,
197             makeForm({'password': '', 'password:confirm': ''}), nodeid), {})
200 def suite():
201     l = [unittest.makeSuite(FormTestCase),
202     ]
203     return unittest.TestSuite(l)
206 # vim: set filetype=python ts=4 sw=4 et si