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
11
16
19
25
34
36 cursor = self.db.cursor()
37 cursor.execute("INSERT INTO auth_cookie (cookie, name, ipnr) "
38 "VALUES ('123', 'john', '127.0.0.1')")
39 incookie = Cookie()
40 incookie['trac_auth'] = '123'
41 outcookie = Cookie()
42 req = Mock(incookie=incookie, outcookie=outcookie,
43 href=Href('/trac.cgi'), base_path='/trac.cgi',
44 remote_addr='127.0.0.1', remote_user=None)
45 self.assertEqual('john', self.module.authenticate(req))
46 self.failIf('auth_cookie' in req.outcookie)
47
49 self.env.config.set('trac', 'check_auth_ip', 'yes')
50 cursor = self.db.cursor()
51 cursor.execute("INSERT INTO auth_cookie (cookie, name, ipnr) "
52 "VALUES ('123', 'john', '127.0.0.1')")
53 incookie = Cookie()
54 incookie['trac_auth'] = '123'
55 outcookie = Cookie()
56 req = Mock(cgi_location='/trac', href=Href('/trac.cgi'),
57 incookie=incookie, outcookie=outcookie,
58 remote_addr='192.168.0.100', remote_user=None,
59 base_path='/trac.cgi')
60 self.assertEqual(None, self.module.authenticate(req))
61 self.failIf('trac_auth' not in req.outcookie)
62
64 self.env.config.set('trac', 'check_auth_ip', 'no')
65 cursor = self.db.cursor()
66 cursor.execute("INSERT INTO auth_cookie (cookie, name, ipnr) "
67 "VALUES ('123', 'john', '127.0.0.1')")
68 incookie = Cookie()
69 incookie['trac_auth'] = '123'
70 outcookie = Cookie()
71 req = Mock(incookie=incookie, outcookie=outcookie,
72 href=Href('/trac.cgi'), base_path='/trac.cgi',
73 remote_addr='192.168.0.100', remote_user=None)
74 self.assertEqual('john', self.module.authenticate(req))
75 self.failIf('auth_cookie' in req.outcookie)
76
78 outcookie = Cookie()
79
80
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
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
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)
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
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
169
170
173
174 if __name__ == '__main__':
175 unittest.main()
176