def test_signup_sends_welcome_email(self): # Sign up successfully response = self.app.post(self.uri_for('signup'), self.SIGNUP_DATA) self.assertRedirects(response, self.uri_for('dashboard', tour='')) # Check that a profile was created profile = Profile.get_by_email(self.SIGNUP_DATA['email']) self.assertIsNotNone(profile) # Check that a mail-sending task is in the queue tasks = self.taskqueue_stub.GetTasks('mail') self.assertLength(1, tasks) # Run the task (it should be a deferred call) and check that an e-mail # is sent task, = tasks deferred.run(base64.b64decode(task['body'])) messages = self.mail_stub.get_sent_messages() self.assertLength(1, messages) message, = messages self.assertEqual('"%s" <%s>' % (profile.name, profile.email), message.to) self.assertEqual('Welcome to Daily Meeting!', message.subject) self.assertEqual('"Daily Meeting" <*****@*****.**>', message.sender) self.assertEqual( '"Daily Meeting Support" <*****@*****.**>', message.reply_to) activation_key = Profile.all().get().activation_key activation_url = self.uri_for('profile.activate', k=activation_key) self.assertIn(activation_url, message.body.decode()) self.assertIn(activation_url, message.html.decode())
def test_forgot_password_post_only_has_homepage_login_form(self): params = {'email': '*****@*****.**'} self.assertIsNone(Profile.get_by_email(params['email'])) response = self.app.post(self.uri_for('forgot-password'), params) self.assertOk(response) self.assertTemplateUsed('forgot_password.haml') self.assertLength(1, response.pyquery('form#login-form'))
def login(self): error = None if self.request.method == 'POST': email = self.request.POST.get('email', '').strip() password = self.request.POST.get('password', '').strip() user = Profile.get_by_email(email) if not user: error = 'User not found' else: try: self.auth.get_user_by_password(email, password) except auth.InvalidPasswordError: error = 'Invalid password!' except auth.InvalidAuthIdError: error = 'Unknown e-mail address!' if not user.beta_tester: self.session.add_flash( value='Please ensure you are cleared for beta testing.', level='error') return self.redirect_to('login') if self.get_current_profile(): redirect = self.request.get('redirect') return self.redirect(redirect or self.uri_for('home')) else: return self.render_to_response('login.haml', {'error': error})
def test_signup_sends_welcome_email(self): # Sign up successfully response = self.app.post(self.uri_for('signup'), self.SIGNUP_DATA) self.assertRedirects(response, self.uri_for('dashboard', tour='')) # Check that a profile was created profile = Profile.get_by_email(self.SIGNUP_DATA['email']) self.assertIsNotNone(profile) # Check that a mail-sending task is in the queue tasks = self.taskqueue_stub.GetTasks('mail') self.assertLength(1, tasks) # Run the task (it should be a deferred call) and check that an e-mail # is sent task, = tasks deferred.run(base64.b64decode(task['body'])) messages = self.mail_stub.get_sent_messages() self.assertLength(1, messages) message, = messages self.assertEqual('"%s" <%s>' % (profile.name, profile.email), message.to) self.assertEqual('Welcome to Daily Meeting!', message.subject) self.assertEqual('"Daily Meeting" <*****@*****.**>', message.sender) self.assertEqual('"Daily Meeting Support" <*****@*****.**>', message.reply_to) activation_key = Profile.all().get().activation_key activation_url = self.uri_for('profile.activate', k=activation_key) self.assertIn(activation_url, message.body.decode()) self.assertIn(activation_url, message.html.decode())
def login(self): error = None if self.request.method == "POST": email = self.request.POST.get("email", "").strip() password = self.request.POST.get("password", "").strip() user = Profile.get_by_email(email) if not user: error = "User not found" else: try: self.auth.get_user_by_password(email, password) except auth.InvalidPasswordError: error = "Invalid password!" except auth.InvalidAuthIdError: error = "Unknown e-mail address!" if not user.beta_tester: self.session.add_flash(value="Please ensure you are cleared for beta testing.", level="error") return self.redirect_to("login") if self.get_current_profile(): redirect = self.request.get("redirect") return self.redirect(redirect or self.uri_for("home")) else: return self.render_to_response("login.haml", {"error": error})
def test_signup_schedules_payment_create_call(self): # Sign up successfully. response = self.app.post(self.uri_for('signup'), self.SIGNUP_DATA) self.assertRedirects(response, self.uri_for('dashboard', tour='')) # Check that a profile was created. profile = Profile.get_by_email(self.SIGNUP_DATA['email']) self.assertIsNotNone(profile) # There should be one task in the scheduler queue. tasks = self.taskqueue_stub.get_filtered_tasks(queue_names=['payment']) self.assertLength(1, tasks) # Verify the details about the task are correct. task, = tasks self.assertEqual(self.uri_for('payment.create'), task.url) self.assertEqual(str(profile.key()), task.extract_params()['profile_key'])
def login(self): error = None if self.request.method == 'POST': email = self.request.POST.get('email', '').strip() password = self.request.POST.get('password', '').strip() user = Profile.get_by_email(email) if not user: error = 'User not found' else: try: self.auth.get_user_by_password(email, password) except auth.InvalidPasswordError: error = 'Invalid password!' except auth.InvalidAuthIdError: error = 'Unknown e-mail address!' if self.get_current_profile(): redirect = self.request.get('redirect') return self.redirect(redirect or self.uri_for('home')) else: return self.render_to_response('login.haml', {'error': error})
def test_forgot_password_post_with_email_not_member(self): params = {'email': '*****@*****.**'} self.assertIsNone(Profile.get_by_email(params['email'])) response = self.app.post(self.uri_for('forgot-password'), params) self.assertOk(response) self.assertTemplateUsed('forgot_password.haml')
def forgot_password(self): if self.get_current_profile(): return self.redirect_to('home') key = self.request.get('k') if key: profile = Profile.get_by_activation_key(key) else: profile = None # GET request (either with or without an activation key and profile); # We should show either the form to send the recovery e-mail, or the # form to change your password. if self.request.method == 'GET': return self.render_to_response('forgot_password.haml', {'profile': profile}) if self.request.method == 'POST': email = self.request.POST.get('email', '').strip() password = self.request.POST.get('password', '').strip() # POST request that had an activation key and a matching profile; # We should update their password, log them in, and redirect. if key and profile: # If we didn't submit a password, then start the process over. if not password: return self.redirect_to('forgot-password', k=key) # Set as activated (since they've confirmed their e-mail). profile.activated = True profile.put() # Change the password for the auth_user. user = self.auth.store.user_model.get_by_id(profile.auth_user_id) user.password = security.generate_password_hash(password, length=12) user.put() # Log the user in. user_id = user.key.id() self.auth._user = None self.auth.get_user_by_token(user_id, user.create_auth_token(user_id)) # Redirect to the dashboard. return self.redirect_to('home') # POST request that didn't find a profile, but POST'ed an e-mail address; # We should send them a recovery e-mail. elif email and not profile: profile = Profile.get_by_email(email) if profile: profile.activation_key = None profile.put() context = {'profile': profile} self.send_mail( profile=profile, defer=True, context=context, subject='{0.PRODUCT_NAME} Password Recovery'.format(constants), template='emails/forgot_password.haml') return self.render_to_response('forgot_password.haml') # POST request that was missing something... # We should redirect back to start the process over. else: return self.redirect_to('forgot-password')
def forgot_password(self): if self.get_current_profile(): return self.redirect_to('home') key = self.request.get('k') if key: profile = Profile.get_by_activation_key(key) else: profile = None # GET request (either with or without an activation key and profile); # We should show either the form to send the recovery e-mail, or the # form to change your password. if self.request.method == 'GET': return self.render_to_response('forgot_password.haml', {'profile': profile}) if self.request.method == 'POST': email = self.request.POST.get('email', '').strip() password = self.request.POST.get('password', '').strip() # POST request that had an activation key and a matching profile; # We should update their password, log them in, and redirect. if key and profile: # If we didn't submit a password, then start the process over. if not password: return self.redirect_to('forgot-password', k=key) # Set as activated (since they've confirmed their e-mail). profile.activated = True profile.put() # Change the password for the auth_user. user = self.auth.store.user_model.get_by_id( profile.auth_user_id) user.password = security.generate_password_hash(password, length=12) user.put() # Log the user in. user_id = user.key.id() self.auth._user = None self.auth.get_user_by_token(user_id, user.create_auth_token(user_id)) # Redirect to the dashboard. return self.redirect_to('home') # POST request that didn't find a profile, but POST'ed an e-mail address; # We should send them a recovery e-mail. elif email and not profile: profile = Profile.get_by_email(email) if profile: profile.activation_key = None profile.put() context = {'profile': profile} self.send_mail( profile=profile, defer=True, context=context, subject='{0.PRODUCT_NAME} Password Recovery'.format( constants), template='emails/forgot_password.haml') return self.render_to_response('forgot_password.haml') # POST request that was missing something... # We should redirect back to start the process over. else: return self.redirect_to('forgot-password')
def validate_email(self, field): profile = Profile.get_by_email(field.data) if profile and profile.key().id() != self.profile_id: raise ValidationError("Email already in use!")