Package trac :: Package web :: Package tests :: Module auth

Source Code for Module trac.web.tests.auth

  1  from trac.core import TracError 
  2  from trac.test import EnvironmentStub, Mock 
  3  from trac.web.auth import LoginModule 
  4  from trac.web.href import Href 
  5   
  6  from Cookie import SimpleCookie as Cookie 
  7  import unittest 
  8   
  9   
10 -class LoginModuleTestCase(unittest.TestCase):
11
12 - def setUp(self):
13 self.env = EnvironmentStub() 14 self.db = self.env.get_db_cnx() 15 self.module = LoginModule(self.env)
16
17 - def tearDown(self):
18 self.env.reset_db()
19
20 - def test_anonymous_access(self):
21 req = Mock(incookie=Cookie(), href=Href('/trac.cgi'), 22 remote_addr='127.0.0.1', remote_user=None, 23 base_path='/trac.cgi') 24 self.assertEqual(None, self.module.authenticate(req))
25 34 47 62 76
77 - def test_login(self):
78 outcookie = Cookie() 79 # remote_user must be upper case to test that by default, case is 80 # preserved. 81 req = Mock(cgi_location='/trac', href=Href('/trac.cgi'), 82 incookie=Cookie(), outcookie=outcookie, 83 remote_addr='127.0.0.1', remote_user='john', 84 authname='john', base_path='/trac.cgi') 85 self.module._do_login(req) 86 87 assert outcookie.has_key('trac_auth'), '"trac_auth" Cookie not set' 88 auth_cookie = outcookie['trac_auth'].value 89 cursor = self.db.cursor() 90 cursor.execute("SELECT name,ipnr FROM auth_cookie WHERE cookie=%s", 91 (auth_cookie,)) 92 row = cursor.fetchone() 93 self.assertEquals('john', row[0]) 94 self.assertEquals('127.0.0.1', row[1])
95
96 - def test_login_ignore_case(self):
97 """ 98 Test that login is succesful when the usernames differ in case, but case 99 is ignored. 100 """ 101 self.env.config.set('trac', 'ignore_auth_case', 'yes') 102 103 outcookie = Cookie() 104 req = Mock(cgi_location='/trac', href=Href('/trac.cgi'), 105 incookie=Cookie(), outcookie=outcookie, 106 remote_addr='127.0.0.1', remote_user='John', 107 authname='anonymous', base_path='/trac.cgi') 108 self.module._do_login(req) 109 110 assert outcookie.has_key('trac_auth'), '"trac_auth" Cookie not set' 111 auth_cookie = outcookie['trac_auth'].value 112 cursor = self.db.cursor() 113 cursor.execute("SELECT name,ipnr FROM auth_cookie WHERE cookie=%s", 114 (auth_cookie,)) 115 row = cursor.fetchone() 116 self.assertEquals('john', row[0]) 117 self.assertEquals('127.0.0.1', row[1])
118
119 - def test_login_no_username(self):
120 req = Mock(incookie=Cookie(), href=Href('/trac.cgi'), 121 remote_addr='127.0.0.1', remote_user=None, 122 base_path='/trac.cgi') 123 self.assertRaises(TracError, self.module._do_login, req)
124
126 cursor = self.db.cursor() 127 cursor.execute("INSERT INTO auth_cookie (cookie, name, ipnr) " 128 "VALUES ('123', 'john', '127.0.0.1')") 129 incookie = Cookie() 130 incookie['trac_auth'] = '123' 131 req = Mock(incookie=incookie, outcookie=Cookie(), 132 href=Href('/trac.cgi'), base_path='/trac.cgi', 133 remote_addr='127.0.0.1', remote_user='john', authname='john') 134 self.module._do_login(req) # this shouldn't raise an error
135
137 cursor = self.db.cursor() 138 cursor.execute("INSERT INTO auth_cookie (cookie, name, ipnr) " 139 "VALUES ('123', 'john', '127.0.0.1')") 140 incookie = Cookie() 141 incookie['trac_auth'] = '123' 142 req = Mock(incookie=incookie, authname='john', 143 href=Href('/trac.cgi'), base_path='/trac.cgi', 144 remote_addr='127.0.0.1', remote_user='tom') 145 self.assertRaises(AssertionError, self.module._do_login, req)
146
147 - def test_logout(self):
148 cursor = self.db.cursor() 149 cursor.execute("INSERT INTO auth_cookie (cookie, name, ipnr) " 150 "VALUES ('123', 'john', '127.0.0.1')") 151 incookie = Cookie() 152 incookie['trac_auth'] = '123' 153 outcookie = Cookie() 154 req = Mock(cgi_location='/trac', href=Href('/trac.cgi'), 155 incookie=incookie, outcookie=outcookie, 156 remote_addr='127.0.0.1', remote_user=None, authname='john', 157 base_path='/trac.cgi') 158 self.module._do_logout(req) 159 self.failIf('trac_auth' not in outcookie) 160 cursor.execute("SELECT name,ipnr FROM auth_cookie WHERE name='john'") 161 self.failIf(cursor.fetchone())
162
164 req = Mock(cgi_location='/trac', href=Href('/trac.cgi'), 165 incookie=Cookie(), outcookie=Cookie(), 166 remote_addr='127.0.0.1', remote_user=None, 167 authname='anonymous', base_path='/trac.cgi') 168 self.module._do_logout(req) # this shouldn't raise an error
169 170
171 -def suite():
172 return unittest.makeSuite(LoginModuleTestCase, 'test')
173 174 if __name__ == '__main__': 175 unittest.main() 176