def test_update(self):
     """
     Make sure that updating credentials works.
     Make sure the update page only appears when the user is logged in.
     Make sure the update form is filled with the user's credentials.
     Make sure that update_ only works when the user is logged in.
     Make sure that update_ works and sends a confirmation email.
     Make sure that update confirmation works.
     """
     # Assert that we get a blank page if the person is not logged in
     self.assertEqual('', self.app.post(url('person_update')).body)
     self.assertEqual(simplejson.dumps({'isOk': 0}), self.app.post(url('person_update_')).body)
     # Add person
     meta.Session.add(model.Person(username, model.hashString(password), nickname, email, email_sms))
     meta.Session.commit()
     # Log in
     self.app.post(url('person_login_'), dict(username=username, password=password, offset_in_minutes=0))
     # Assert that the update form is filled with the user's credentials
     responseBody = self.app.get(url('person_update')).body
     self.assert_(username in responseBody)
     self.assert_(nickname in responseBody)
     self.assert_(email in responseBody)
     self.assert_(email_sms in responseBody)
     # Update credentials
     newUsername = store.makeRandomString(16)
     newPassword = store.makeRandomString(16)
     newNickname = unicode(store.makeRandomString(16))
     newEmail = re.sub(r'.*@', store.makeRandomString(16) + '@', email)
     newEmailSMS = re.sub(r'.*@', store.makeRandomString(16) + '@', email)
     self.assertEqual(simplejson.dumps({'isOk': 1}), self.app.post(url('person_update_'), dict(username=newUsername, password=newPassword, nickname=newNickname, email=newEmail, email_sms=newEmailSMS)).body)
     # Make sure the credentials have not changed yet
     self.assertEqual(meta.Session.query(model.Person).filter_by(username=newUsername, password_hash=model.hashString(newPassword), nickname=newNickname, email=newEmail, email_sms=newEmailSMS).count(), 0)
     # Activate confirmation
     self.app.get(url('person_confirm', ticket=meta.Session.query(model.PersonConfirmation.ticket).filter_by(username=newUsername).first()[0]))
     # Make sure the credentials have changed
     self.assertEqual(meta.Session.query(model.Person).filter_by(username=newUsername, password_hash=model.hashString(newPassword), nickname=newNickname, email=newEmail, email_sms=newEmailSMS).count(), 1)
 def test_sendMessage(self):
     """
     Ensure that we can send messages successfully.
     """
     # Prepare
     fromPack = config['extra']['mail_support']
     toPack = config['extra']['mail_test']
     subject = store.makeRandomString(16)
     # Send message
     mail.sendMessage(fromPack, toPack, subject, '')
     # Check that the message exists on the server
     server = imaplib.IMAP4(toPack['imap'])
     server.login(toPack['username'], toPack['password'])
     server.select()
     self.assertNotEqual(server.search(None, '(SUBJECT "%s")' % subject)[1], [''])
 def reset(self):
     """
     Reset password
     """
     # Get email
     email = request.POST.get('email')
     # Try to load the person
     person = meta.Session.query(model.Person).filter(model.Person.email==email).first()
     # If the email is not in our database,
     if not person: 
         return {'isOk': 0}
     # Reset account
     c.password = store.makeRandomString(parameter.PASSWORD_LENGTH_AVERAGE)
     return changeAccount(dict(
         username=person.username,
         password=c.password,
         nickname=person.nickname,
         email=person.email,
         email_sms=person.email_sms,
     ), 'reset', '/people/confirm_email.mako', person)