~ubuntu-branches/ubuntu/raring/trac-accountmanager/raring

« back to all changes in this revision

Viewing changes to acct_mgr/tests/functional/testcases.py

  • Committer: Bazaar Package Importer
  • Author(s): Leo Costela
  • Date: 2008-07-15 17:21:11 UTC
  • Revision ID: james.westby@ubuntu.com-20080715172111-ool7wmy573gqolfr
Tags: upstream-0.2.1~vcs20080715
ImportĀ upstreamĀ versionĀ 0.2.1~vcs20080715

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2008 Matthew Good <trac@matt-good.net>
 
4
#
 
5
# "THE BEER-WARE LICENSE" (Revision 42):
 
6
# <trac@matt-good.net> wrote this file.  As long as you retain this notice you
 
7
# can do whatever you want with this stuff. If we meet some day, and you think
 
8
# this stuff is worth it, you can buy me a beer in return.   Matthew Good
 
9
#
 
10
# Author: Pedro Algarvio <ufs@ufsoft.org>
 
11
 
 
12
import base64
 
13
 
 
14
from trac.tests.notification import parse_smtp_message
 
15
 
 
16
from acct_mgr.tests.functional import *
 
17
 
 
18
 
 
19
class TestFormLoginAdmin(FunctionalTwillTestCaseSetup):
 
20
    def runTest(self):
 
21
        """Login with test user 'admin'"""
 
22
        self._tester.login('admin')
 
23
        self._tester.logout()
 
24
 
 
25
class TestFormLoginUser(FunctionalTwillTestCaseSetup):
 
26
    def runTest(self):
 
27
        """Login with test user 'user'"""
 
28
        self._tester.login('user')
 
29
        self._tester.logout()
 
30
 
 
31
class TestRegisterNewUser(FunctionalTestCaseSetup):
 
32
    def runTest(self):
 
33
        """Register 'testuser'"""
 
34
        self._tester.register('testuser')
 
35
 
 
36
class TestLoginNewUser(FunctionalTestCaseSetup):
 
37
    def runTest(self):
 
38
        """Login just registered 'testuser'"""
 
39
        self._tester.login('testuser')
 
40
        self._tester.logout()
 
41
 
 
42
 
 
43
class TestFailRegisterPasswdConfirmNotPassed(FunctionalTestCaseSetup):
 
44
    def runTest(self):
 
45
        """Fail if no password confirmation is passed"""
 
46
        reg_form_name = 'acctmgr_registerform'     
 
47
        username = 'testuser1'   
 
48
        tc.find("Register")
 
49
        tc.follow("Register")        
 
50
        tc.formvalue(reg_form_name, 'user', username)
 
51
        tc.formvalue(reg_form_name, 'password', username)
 
52
        tc.submit()
 
53
        tc.find("The passwords must match.")
 
54
 
 
55
class TestFailRegisterDuplicateUsername(FunctionalTestCaseSetup):
 
56
    def runTest(self):
 
57
        """Fail if username exists"""
 
58
        reg_form_name = 'acctmgr_registerform'
 
59
        username = 'testuser'   
 
60
        tc.find("Register")
 
61
        tc.follow("Register")        
 
62
        tc.formvalue(reg_form_name, 'user', username)
 
63
        tc.formvalue(reg_form_name, 'password', username)
 
64
        tc.formvalue(reg_form_name, 'password_confirm', username)
 
65
        tc.submit()
 
66
        tc.find("Another account with that name already exists.")
 
67
        
 
68
class TestNewAccountNotification(FunctionalTestCaseSetup):
 
69
    def runTest(self):
 
70
        """Send out notification on new account registrations"""
 
71
        tc.notfind('Logout')
 
72
        address_to_notify = 'admin@testenv%s.tld' % self._testenv.port
 
73
        new_username = 'foo'
 
74
        new_username_email = "foo@%s" % address_to_notify.split('@')[1]
 
75
         
 
76
        env = self._testenv.get_trac_environment()
 
77
        env.config.set('account-manager', 'account_changes_notify_addresses',
 
78
                       address_to_notify)
 
79
        env.config.set('account-manager', 'notify_actions', 'new,change,delete')
 
80
        env.config.set('account-manager', 'force_passwd_change', 'true')
 
81
        env.config.save()
 
82
        self._tester.register(new_username, new_username_email)
 
83
        
 
84
        headers, body = parse_smtp_message(self._smtpd.get_message())
 
85
        
 
86
        self.assertEqual(self._smtpd.get_recipients(), [address_to_notify])
 
87
        self.assertEqual(headers['Subject'],
 
88
                         '[%s] New user registration: %s' % (
 
89
                                            'testenv%s' % self._testenv.port,
 
90
                                            new_username))
 
91
        self.assertEqual(headers['X-URL'], self._testenv.url)
 
92
        
 
93
class TestNewAccountEmailVerification(FunctionalTestCaseSetup):
 
94
    def runTest(self):
 
95
        """User is shown info that he needs to verify his address"""
 
96
        user_email = "foo@testenv%s.tld" % self._testenv.port
 
97
        self._tester.login("foo")
 
98
        
 
99
        tc.find('<strong>Notice:</strong> <span>An email has been sent to '
 
100
                '%s with a token to <a href="/verify_email">verify your new '
 
101
                'email address</a></span>' % user_email)
 
102
        self._tester.go_to_front()
 
103
        tc.find('<strong>Warning:</strong> <span>Your permissions have been '
 
104
                'limited until you <a href="/verify_email">verify your email '
 
105
                'address</a></span>')
 
106
        
 
107
class VerifyNewAccountEmailAddress(FunctionalTestCaseSetup):
 
108
    def runTest(self):
 
109
        """User confirms his address with mailed token"""
 
110
        headers, body = parse_smtp_message(self._smtpd.get_message())
 
111
        blines = base64.decodestring(body).splitlines()
 
112
        token = [l.split() for l in blines if 'Verification Token' in l][0][-1]
 
113
        
 
114
        tc.find('Logout') # User is logged in from previous test
 
115
        self._tester.go_to_front()
 
116
        tc.find('<strong>Warning:</strong> <span>Your permissions have been '
 
117
                'limited until you <a href="/verify_email">verify your email '
 
118
                'address</a></span>')
 
119
        tc.go(self._testenv.url + '/verify_email')
 
120
        
 
121
        reg_form_name = 'acctmgr_verify_email'
 
122
        tc.formvalue(reg_form_name, 'token', token)
 
123
        tc.submit('verify')
 
124
        
 
125
        tc.notfind('<strong>Warning:</strong> <span>Your permissions have been '
 
126
                   'limited until you <a href="/verify_email">verify your email'
 
127
                   ' address</a></span>')
 
128
        tc.find('Thank you for verifying your email address')
 
129
        self._tester.go_to_front()
 
130
  
 
131
        
 
132
class PasswdResetsNotifiesAdmin(FunctionalTestCaseSetup):
 
133
    def runTest(self):
 
134
        """User password resets notifies admin by mail"""
 
135
        self._tester.logout()
 
136
        self._smtpd.full_reset() # Clean all previous sent emails
 
137
        tc.notfind('Logout')
 
138
        # Goto Login
 
139
        tc.find("Login")
 
140
        tc.follow("Login")
 
141
        # Do we have the Forgot passwd link
 
142
        tc.find('Forgot your password?')
 
143
        tc.follow('Forgot your password?')
 
144
        
 
145
        username = "foo"
 
146
        email_addr = "foo@testenv%s.tld" % self._testenv.port
 
147
        
 
148
        reset_form_name = 'acctmgr_passwd_reset'
 
149
        tc.formvalue(reset_form_name, 'username', username)
 
150
        tc.formvalue(reset_form_name, 'email', email_addr)
 
151
        tc.submit()
 
152
        
 
153
        headers, body = parse_smtp_message(
 
154
            self._smtpd.get_message('admin@testenv%s.tld' % self._testenv.port))
 
155
        self.assertEqual(headers['Subject'],
 
156
                         '[%s] Password reset for user: %s' % (
 
157
                                            'testenv%s' % self._testenv.port,
 
158
                                            username))
 
159
        self.assertEqual(headers['X-URL'], self._testenv.url)
 
160
        
 
161
        
 
162
class PasswdResetsNotifiesUser(FunctionalTestCaseSetup):
 
163
    def runTest(self):
 
164
        """Password reset sends new password to user by mail"""
 
165
        username = "foo"
 
166
        email_addr = "foo@testenv%s.tld" % self._testenv.port
 
167
        headers, self.body = parse_smtp_message(self._smtpd.get_message(email_addr))
 
168
        self.assertEqual(headers['Subject'],
 
169
                         '[%s] Trac password reset for user: %s' % (
 
170
                                            'testenv%s' % self._testenv.port,
 
171
                                            username))
 
172
        
 
173
class UserLoginWithMailedPassword(PasswdResetsNotifiesUser):
 
174
    def runTest(self):
 
175
        """User is able to login with the new password"""
 
176
        PasswdResetsNotifiesUser.runTest(self)
 
177
        # Does it include a new password
 
178
        body = base64.decodestring(self.body)
 
179
        username = 'foo'
 
180
        self.assertTrue('Username: %s' % username in body)
 
181
        self.assertTrue('Password:' in body)
 
182
        
 
183
        passwd = [l.split(':')[1].strip() for l in
 
184
                  body.splitlines() if 'Password:' in l][0]
 
185
        
 
186
        self._tester.login(username, passwd)
 
187
        
 
188
class UserIsForcedToChangePassword(FunctionalTestCaseSetup):
 
189
    def runTest(self):
 
190
        """User is forced to change password after resets"""
 
191
        tc.find('Logout')
 
192
        tc.find("You are required to change password because of a recent "
 
193
                "password change request.")
 
194
        
 
195
 
 
196
class UserCantBrowseUntilPasswdChange(PasswdResetsNotifiesUser):
 
197
    def runTest(self):
 
198
        """User can't navigate out of '/prefs/account' before password change"""
 
199
        PasswdResetsNotifiesUser.runTest(self)
 
200
        tc.find('Logout')
 
201
        forced_passwd_change_url = '^%s/prefs/account$' % self._tester.url
 
202
        tc.follow('Roadmap')
 
203
        tc.url(forced_passwd_change_url)
 
204
        tc.follow('View Tickets')
 
205
        tc.url(forced_passwd_change_url)
 
206
        tc.follow('New Ticket')
 
207
        tc.url(forced_passwd_change_url)
 
208
        tc.follow('Browse Source')
 
209
        tc.url(forced_passwd_change_url)
 
210
        
 
211
        # Now, let's change his password
 
212
        body = base64.decodestring(self.body)
 
213
        passwd = [l.split(':')[1].strip() for l in
 
214
                  body.splitlines() if 'Password:' in l][0]
 
215
        username = 'foo'
 
216
        change_passwd_form = 'userprefs'
 
217
        tc.formvalue(change_passwd_form, 'old_password', passwd)
 
218
        tc.formvalue(change_passwd_form, 'password', username)
 
219
        tc.formvalue(change_passwd_form, 'password_confirm', username)
 
220
        tc.submit()
 
221
        
 
222
        tc.notfind("You are required to change password because of a recent "
 
223
                   "password change request.")
 
224
        tc.find("Thank you for taking the time to update your password.")
 
225
        
 
226
        # We can now browse away from /prefs/accounts
 
227
        tc.follow('Roadmap')
 
228
        tc.url(self._tester.url + '/roadmap')
 
229
        # Clear the mailstore
 
230
        self._smtpd.full_reset()
 
231
        
 
232
class DeleteAccountNotifiesAdmin(FunctionalTestCaseSetup):
 
233
    def runTest(self):
 
234
        """Delete account notifies admin"""
 
235
        tc.find("Logout") # We're logged-in from previous post
 
236
        tc.follow("Preferences")
 
237
        tc.follow("Account")
 
238
        tc.url(self._testenv.url + '/prefs/account')
 
239
        
 
240
        delete_account_form_name = 'acctmgr_delete_account'
 
241
        tc.formvalue(delete_account_form_name, 'password', 'foo')
 
242
        tc.submit()
 
243
        tc.find("Login") # We're logged out when we delete our account
 
244
        headers, _ = parse_smtp_message(self._smtpd.get_message())
 
245
        self.assertEqual(headers['Subject'],
 
246
                         '[%s] Deleted User: %s' % (
 
247
                                'testenv%s' % self._testenv.port, 'foo'))
 
248
        
 
249
class UserNoLongerLogins(FunctionalTestCaseSetup):
 
250
    def runTest(self):
 
251
        """Deleted user can't login"""
 
252
        tc.follow('Login')
 
253
        login_form_name = 'acctmgr_loginform'
 
254
        tc.formvalue(login_form_name, 'user', 'foo')
 
255
        tc.formvalue(login_form_name, 'password', 'foo')
 
256
        tc.submit()
 
257
        tc.find("Invalid username or password")
 
258
        tc.notfind('Logout')
 
259
        
 
260
class UserIsAbleToRegisterWithSameUserName(FunctionalTestCaseSetup):
 
261
    def runTest(self):
 
262
        """Register with deleted username (session and session_attributes clean)"""
 
263
        self._tester.register('foo')
 
264
        self._tester.login('foo')
 
265
        self._tester.logout()
 
266
        self._smtpd.full_reset()
 
267
 
 
268
class NoEmailVerificationForAnonymousUsers(FunctionalTestCaseSetup):
 
269
    def runTest(self):
 
270
        """Anonymous users don't get their email address verified"""
 
271
        tc.find("Login")
 
272
        tc.follow("Preferences")
 
273
        form_name = 'userprefs'
 
274
        email_address = 'anonyous.user@fakedomain.tld'
 
275
        tc.formvalue(form_name, 'email', email_address)
 
276
        tc.submit()
 
277
        tc.notfind('<strong>Notice:</strong> <span>An email has been sent to '
 
278
                   '%s with a token to <a href="/verify_email">verify your new '
 
279
                   'email address</a></span>' % email_address)
 
280
        self._tester.go_to_front()
 
281
        tc.notfind('<strong>Warning:</strong> <span>Your permissions have been '
 
282
                   'limited until you <a href="/verify_email">verify your email '
 
283
                   'address</a></span>')
 
284
        
 
285
        
 
286
def suite():
 
287
    suite = FunctionalTestSuite()
 
288
    suite.addTest(TestFormLoginAdmin())
 
289
    suite.addTest(TestFormLoginUser())
 
290
    suite.addTest(TestRegisterNewUser())
 
291
    suite.addTest(TestLoginNewUser())
 
292
    suite.addTest(TestFailRegisterPasswdConfirmNotPassed())
 
293
    suite.addTest(TestFailRegisterDuplicateUsername())
 
294
    suite.addTest(TestNewAccountNotification())
 
295
    suite.addTest(TestNewAccountEmailVerification())
 
296
    suite.addTest(VerifyNewAccountEmailAddress())
 
297
    suite.addTest(PasswdResetsNotifiesAdmin())
 
298
    suite.addTest(PasswdResetsNotifiesUser())
 
299
    suite.addTest(UserLoginWithMailedPassword())
 
300
    suite.addTest(UserIsForcedToChangePassword())
 
301
    suite.addTest(UserCantBrowseUntilPasswdChange())
 
302
    suite.addTest(DeleteAccountNotifiesAdmin())
 
303
    suite.addTest(UserNoLongerLogins())
 
304
    suite.addTest(UserIsAbleToRegisterWithSameUserName())
 
305
    suite.addTest(NoEmailVerificationForAnonymousUsers())
 
306
    return suite
 
307
 
 
308
 
 
309
if __name__ == '__main__':
 
310
    unittest.main(defaultTest='suite')
 
311