Back to index

moin  1.9.0~rc2
test_user.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     MoinMoin - MoinMoin.user Tests
00004 
00005     @copyright: 2003-2004 by Juergen Hermann <jh@web.de>
00006                 2009 by ReimarBauer
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import os
00011 import py
00012 
00013 from MoinMoin import user, caching
00014 from MoinMoin.util import filesys
00015 
00016 
00017 class TestEncodePassword(object):
00018     """user: encode passwords tests"""
00019 
00020     def testAscii(self):
00021         """user: encode ascii password"""
00022         # u'MoinMoin' and 'MoinMoin' should be encoded to same result
00023         expected = "{SSHA}xkDIIx1I7A4gC98Vt/+UelIkTDYxMjM0NQ=="
00024 
00025         result = user.encodePassword("MoinMoin", salt='12345')
00026         assert result == expected
00027         result = user.encodePassword(u"MoinMoin", salt='12345')
00028         assert result == expected
00029 
00030     def testUnicode(self):
00031         """ user: encode unicode password """
00032         result = user.encodePassword(u'סיסמה סודית בהחלט', salt='12345') # Hebrew
00033         expected = "{SSHA}YiwfeVWdVW9luqyVn8t2JivlzmUxMjM0NQ=="
00034         assert result == expected
00035 
00036 
00037 class TestLoginWithPassword(object):
00038     """user: login tests"""
00039 
00040     def setup_method(self, method):
00041         # Save original user and cookie
00042         self.saved_cookie = self.request.cookies
00043         self.saved_user = self.request.user
00044 
00045         # Create anon user for the tests
00046         self.request.cookies = {}
00047         self.request.user = user.User(self.request)
00048 
00049         # Prevent user list caching - we create and delete users too fast for that.
00050         filesys.dcdisable()
00051         self.user = None
00052 
00053     def teardown_method(self, method):
00054         """ Run after each test
00055 
00056         Remove user and reset user listing cache.
00057         """
00058         # Remove user file and user
00059         if self.user is not None:
00060             try:
00061                 path = self.user._User__filename()
00062                 os.remove(path)
00063             except OSError:
00064                 pass
00065             del self.user
00066 
00067         # Restore original user
00068         self.request.cookies = self.saved_cookie
00069         self.request.user = self.saved_user
00070 
00071         # Remove user name to id cache, or next test will fail
00072         caching.CacheEntry(self.request, 'user', 'name2id', scope='wiki').remove()
00073         try:
00074             del self.request.cfg.cache.name2id
00075         except:
00076             pass
00077 
00078         # Prevent user list caching - we create and delete users too fast for that.
00079         filesys.dcdisable()
00080 
00081     def testAsciiPassword(self):
00082         """ user: login with ascii password """
00083         # Create test user
00084         name = u'__Non Existent User Name__'
00085         password = name
00086         self.createUser(name, password)
00087 
00088         # Try to "login"
00089         theUser = user.User(self.request, name=name, password=password)
00090         assert theUser.valid
00091 
00092     def testUnicodePassword(self):
00093         """ user: login with non-ascii password """
00094         # Create test user
00095         name = u'__שם משתמש לא קיים__' # Hebrew
00096         password = name
00097         self.createUser(name, password)
00098 
00099         # Try to "login"
00100         theUser = user.User(self.request, name=name, password=password)
00101         assert theUser.valid
00102 
00103     def testSubscriptionSubscribedPage(self):
00104         """ user: tests isSubscribedTo  """
00105         pagename = u'HelpMiscellaneous'
00106         name = u'__Jürgen Herman__'
00107         password = name
00108         self.createUser(name, password)
00109         # Login - this should replace the old password in the user file
00110         theUser = user.User(self.request, name=name, password=password)
00111         theUser.subscribe(pagename)
00112         assert theUser.isSubscribedTo([pagename]) # list(!) of pages to check
00113 
00114     def testSubscriptionSubPage(self):
00115         """ user: tests isSubscribedTo on a subpage """
00116         pagename = u'HelpMiscellaneous'
00117         testPagename = u'HelpMiscellaneous/FrequentlyAskedQuestions'
00118         name = u'__Jürgen Herman__'
00119         password = name
00120         self.createUser(name, password)
00121         # Login - this should replace the old password in the user file
00122         theUser = user.User(self.request, name=name, password=password)
00123         theUser.subscribe(pagename)
00124         assert not theUser.isSubscribedTo([testPagename]) # list(!) of pages to check
00125 
00126     def testRenameUser(self):
00127         """ create user and then rename user and check
00128         if the old username is removed from the cache name2id
00129         """
00130         # Create test user
00131         name = u'__Some Name__'
00132         password = name
00133         self.createUser(name, password)
00134         # Login - this should replace the old password in the user file
00135         theUser = user.User(self.request, name=name)
00136         # Rename user
00137         theUser.name = u'__SomeName__'
00138         theUser.save()
00139         theUser = user.User(self.request, name=name, password=password)
00140 
00141         assert not theUser.exists()
00142 
00143     def test_upgrade_password_to_salted(self):
00144         """
00145         Create user with {SHA} password and check that logging in
00146         upgrades to {SSHA}.
00147         """
00148         name = u'/no such user/'
00149         password = '{SHA}jLIjfQZ5yojbZGTqxg2pY0VROWQ=' # 12345
00150         self.createUser(name, password, True)
00151         theuser = user.User(self.request, name=name, password='12345')
00152         assert theuser.enc_password[:6] == '{SSHA}'
00153 
00154     def test_for_email_attribute_by_name(self):
00155         """
00156         checks for no access to the email attribute by getting the user object from name
00157         """
00158         name = u"__TestUser__"
00159         password = u"ekfdweurwerh"
00160         email = "__TestUser__@moinhost"
00161         self.createUser(name, password, email=email)
00162         theuser = user.User(self.request, name=name)
00163         assert theuser.email == ""
00164 
00165     def test_for_email_attribut_by_uid(self):
00166         """
00167         checks access to the email attribute by getting the user object from the uid
00168         """
00169         name = u"__TestUser2__"
00170         password = u"ekERErwerwerh"
00171         email = "__TestUser2__@moinhost"
00172         self.createUser(name, password, email=email)
00173         uid = user.getUserId(self.request, name)
00174         theuser = user.User(self.request, uid)
00175         assert theuser.email == email
00176 
00177     # Helpers ---------------------------------------------------------
00178 
00179     def createUser(self, name, password, pwencoded=False, email=None):
00180         """ helper to create test user
00181         """
00182         # Create user
00183         self.user = user.User(self.request)
00184         self.user.name = name
00185         self.user.email = email
00186         if not pwencoded:
00187             password = user.encodePassword(password)
00188         self.user.enc_password = password
00189 
00190         # Validate that we are not modifying existing user data file!
00191         if self.user.exists():
00192             self.user = None
00193             py.test.skip("Test user exists, will not override existing user data file!")
00194 
00195         # Save test user
00196         self.user.save()
00197 
00198         # Validate user creation
00199         if not self.user.exists():
00200             self.user = None
00201             py.test.skip("Can't create test user")
00202 
00203 
00204 class TestGroupName(object):
00205 
00206     def testGroupNames(self):
00207         """ user: isValidName: reject group names """
00208         test = u'AdminGroup'
00209         assert not user.isValidName(self.request, test)
00210 
00211 
00212 class TestIsValidName(object):
00213 
00214     def testNonAlnumCharacters(self):
00215         """ user: isValidName: reject unicode non alpha numeric characters
00216 
00217         : and , used in acl rules, we might add more characters to the syntax.
00218         """
00219         invalid = u'! # $ % ^ & * ( ) = + , : ; " | ~ / \\ \u0000 \u202a'.split()
00220         base = u'User%sName'
00221         for c in invalid:
00222             name = base % c
00223             assert not user.isValidName(self.request, name)
00224 
00225     def testWhitespace(self):
00226         """ user: isValidName: reject leading, trailing or multiple whitespace """
00227         cases = (
00228             u' User Name',
00229             u'User Name ',
00230             u'User   Name',
00231             )
00232         for test in cases:
00233             assert not user.isValidName(self.request, test)
00234 
00235     def testValid(self):
00236         """ user: isValidName: accept names in any language, with spaces """
00237         cases = (
00238             u'Jürgen Hermann', # German
00239             u'ניר סופר', # Hebrew
00240             u'CamelCase', # Good old camel case
00241             u'가각간갇갈 갉갊감 갬갯걀갼' # Hangul (gibberish)
00242             )
00243         for test in cases:
00244             assert user.isValidName(self.request, test)
00245 
00246 
00247 coverage_modules = ['MoinMoin.user']
00248