comparison MoinMoin/_tests/test_user.py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children 1314fdb74689
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 # -*- coding: utf-8 -*-
2 """
3 MoinMoin - MoinMoin.user Tests
4
5 @copyright: 2003-2004 by Jürgen Hermann <jh@web.de>
6 @license: GNU GPL, see COPYING for details.
7 """
8
9 import unittest, os, dircache
10
11 from MoinMoin._tests import TestConfig, TestSkiped
12 from MoinMoin import user, caching
13
14
15
16 class EncodePasswordTestCase(unittest.TestCase):
17 """user: encode passwords tests"""
18
19 def testAscii(self):
20 """user: encode ascii password"""
21 # u'MoinMoin' and 'MoinMoin' should be encoded to same result
22 expected = "{SHA}X+lk6KR7JuJEH43YnmettCwICdU="
23
24 result = user.encodePassword("MoinMoin")
25 self.assertEqual(result, expected,
26 'Expected "%(expected)s" but got "%(result)s"' % locals())
27 result = user.encodePassword(u"MoinMoin")
28 self.assertEqual(result, expected,
29 'Expected "%(expected)s" but got "%(result)s"' % locals())
30
31 def testUnicode(self):
32 """ user: encode unicode password """
33 result = user.encodePassword(u'סיסמה סודית בהחלט') # Hebrew
34 expected = "{SHA}GvvkgYzv5MoF9Ljivv2oc81FmkE="
35 self.assertEqual(result, expected,
36 'Expected "%(expected)s" but got "%(result)s"' % locals())
37
38
39 class LoginWithPasswordTestCase(unittest.TestCase):
40 """user: login tests"""
41
42 def setUp(self):
43 # Save original user and cookie
44 self.saved_cookie = self.request.saved_cookie
45 self.saved_user = self.request.user
46
47 # Create anon user for the tests
48 self.request.saved_cookie = ''
49 self.request.user = user.User(self.request)
50
51 # Prevent user list caching - we create and delete users too
52 # fast for that.
53 try:
54 del dircache.cache[self.request.cfg.user_dir]
55 except KeyError:
56 pass
57
58 def tearDown(self):
59 """ Run after each test
60
61 Remove user and reset user listing cache.
62 """
63 # Remove user file and user
64 if self.user is not None:
65 try:
66 path = self.user._User__filename()
67 os.remove(path)
68 except OSError:
69 pass
70 del self.user
71
72 # Restore original user
73 self.request.saved_cookie = self.saved_cookie
74 self.request.user = self.saved_user
75
76 # Remove user name to id cache, or next test will fail
77 caching.CacheEntry(self.request, 'user', 'name2id').remove()
78 del self.request.cfg._name2id
79
80 # Prevent user list caching - we create and delete users too
81 # fast for that.
82 try:
83 del dircache.cache[self.request.cfg.user_dir]
84 except KeyError:
85 pass
86
87 def testAsciiPassword(self):
88 """ user: login with ascii password """
89 # Create test user
90 name = u'__Non Existent User Name__'
91 password = name
92 self.createUser(name, password)
93
94 # Try to "login"
95 theUser = user.User(self.request, name=name, password=password)
96 self.failUnless(theUser.valid, "Can't login with ascii password")
97
98 def testUnicodePassword(self):
99 """ user: login with non-ascii password """
100 # Create test user
101 name = u'__שם משתמש לא קיים__' # Hebrew
102 password = name
103 self.createUser(name, password)
104
105 # Try to "login"
106 theUser = user.User(self.request, name=name, password=password)
107 self.failUnless(theUser.valid, "Can't login with unicode password")
108
109 def testOldNonAsciiPassword(self):
110 """ user: login with non-ascii password in pre 1.3 user file
111
112 When trying to login with an old non-ascii password in the user
113 file, utf-8 encoded password will not match. In this case, try
114 all other encoding available on pre 1.3 before failing.
115 """
116 # Create test user
117 # Use iso charset to create user with old enc_password, as if
118 # the user file was migrated from pre 1.3 wiki.
119 name = u'__Jürgen Herman__'
120 password = name
121 self.createUser(name, password, charset='iso-8859-1')
122
123 # Try to "login"
124 theUser = user.User(self.request, name=name, password=password)
125 self.failUnless(theUser.valid, "Can't login with old unicode password")
126
127 def testReplaceOldNonAsciiPassword(self):
128 """ user: login replace old non-ascii password in pre 1.3 user file
129
130 When trying to login with an old non-ascii password in the user
131 file, the password hash should be replaced with new utf-8 hash.
132 """
133 # Create test user
134 # Use iso charset to create user with old enc_password, as if
135 # the user file was migrated from pre 1.3 wiki.
136 name = u'__Jürgen Herman__'
137 password = name
138 self.createUser(name, password, charset='iso-8859-1')
139
140 # Login - this should replace the old password in the user file
141 theUser = user.User(self.request, name=name, password=password)
142 # Login again - the password should be new unicode password
143 expected = user.encodePassword(password)
144 theUser = user.User(self.request, name=name, password=password)
145 self.assertEqual(theUser.enc_password, expected,
146 "User password was not replaced with new")
147
148 # Helpers ---------------------------------------------------------
149
150 def createUser(self, name, password, charset='utf-8'):
151 """ helper to create test user
152
153 charset is used to create user with pre 1.3 password hash
154 """
155 # Hack self.request form to contain the password
156 self.request.form['password'] = [password]
157
158 # Create user
159 self.user = user.User(self.request)
160 self.user.name = name
161 self.user.enc_password = user.encodePassword(password, charset=charset)
162
163 # Validate that we are not modifying existing user data file!
164 if self.user.exists():
165 self.user = None
166 raise TestsSkiped("Test user exists, will not override existing"
167 " user data file!")
168
169 # Save test user
170 self.user.save()
171
172 # Validate user creation
173 if not self.user.exists():
174 self.user = None
175 raise TestsSkiped("Can't create test user")
176
177
178 class GroupNameTestCase(unittest.TestCase):
179
180 def setUp(self):
181 self.config = TestConfig(self.request,
182 page_group_regex = r'.+Group')
183
184 def tearDown(self):
185 del self.config
186
187 import re
188 group = re.compile(r'.+Group', re.UNICODE)
189
190 def testGroupNames(self):
191 """ user: isValidName: reject group names """
192 test = u'AdminGroup'
193 assert self.group.search(test)
194 result = user.isValidName(self.request, test)
195 expected = False
196 self.assertEqual(result, expected,
197 'Expected "%(expected)s" but got "%(result)s"' % locals())
198
199
200 class IsValidNameTestCase(unittest.TestCase):
201
202 def testNonAlnumCharacters(self):
203 """ user: isValidName: reject unicode non alpha numeric characters
204
205 : and , used in acl rules, we might add more characters to the syntax.
206 """
207 invalid = u'! @ # $ % ^ & * ( ) - = + , : ; " | ~ / \\ \u0000 \u202a'.split()
208 base = u'User%sName'
209 expected = False
210 for c in invalid:
211 name = base % c
212 result = user.isValidName(self.request, name)
213 self.assertEqual(result, expected,
214 'Expected "%(expected)s" but got "%(result)s"' % locals())
215
216 def testWhitespace(self):
217 """ user: isValidName: reject leading, trailing or multiple whitespace """
218 cases = (
219 u' User Name',
220 u'User Name ',
221 u'User Name',
222 )
223 expected = False
224 for test in cases:
225 result = user.isValidName(self.request, test)
226 self.assertEqual(result, expected,
227 'Expected "%(expected)s" but got "%(result)s"' % locals())
228
229 def testValid(self):
230 """ user: isValidName: accept names in any language, with spaces """
231 cases = (
232 u'Jürgen Hermann', # German
233 u'ניר סופר', # Hebrew
234 u'CamelCase', # Good old camel case
235 u'가각간갇갈 갉갊감 갬갯걀갼' # Hangul (gibberish)
236 )
237 expected = True
238 for test in cases:
239 result = user.isValidName(self.request, test)
240 self.assertEqual(result, expected,
241 'Expected "%(expected)s" but got "%(result)s"' % locals())
242
243