def registerFoodseller(): if current_user.is_authenticated: return redirect(url_for('about')) form = RegistrationFormFoodseller() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data.decode('utf-8')) register = Foodseller(foodsellerName=form.foodsellerName.data, email=form.email.data, city=form.city.data.upper(), address=form.address.data, phone_number=form.phone_number.data, opening_hours=form.opening_hours.data, password=hashed_password) db.session.add(register) db.session.commit() flash('Account created for %s, you can login now' % form.foodsellerName.data) send_email(form.email.data, 'Registration completed successfully', 'email', username=form.foodsellerName.data) return redirect(url_for('login')) return render_template('registerFoodseller.html', title='Register', form=form)
def test_send_email(self, mock_tasks, mock_form_values, mock_request): class FakeTask(): def get(self, timeout=0): return "Fake Service" form = { "to": "*****@*****.**", "from_email": "*****@*****.**", "subject": "thisi s a test", "text": "text" } mock_form_values.return_value = True mock_request.form.to_dict.return_value = form mock_tasks.send_mail.delay.return_value = FakeTask() app.send_email() self.assertTrue(mock_tasks.send_mail.delay.called) self.assertTrue(mock_form_values.called)
def test_send_email(self, mock_tasks, mock_form_values, mock_request): class FakeTask(): def get(self, timeout=0): return "Fake Service" form = { "to": "*****@*****.**", "from_email": "*****@*****.**", "subject": "thisi s a test", "text": "text" } mock_form_values.return_value = True mock_request.form.to_dict.return_value = form mock_tasks.send_mail.delay.return_value = FakeTask() app.send_email() self.assertTrue(mock_tasks.send_mail.delay.called) self.assertTrue(mock_form_values.called)
def forgot_password(): if not current_user.is_anonymous: logout_user() form = ForgotPasswordForm() if form.validate_on_submit(): email = form.email.data user = User.from_email(email) if user.exists(): token = current_app.ts.dumps(user.email, salt='recover-key') recover_url = url_for('pages.reset_password', token=token, _external=True) app.send_email('Password Reset', email, recover_url) print(recover_url) flash( 'Please check your inbox for your password reset link. (It may take a couple minutes.)' ) return render_template('forgot_password.html', form=form)
def reset_password(): form = ResetPasswordForm() if request.method == 'POST': if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if not user: flash('ไม่พบอีเมลที่ระบุในฐานข้อมูล', 'warning') else: s = Serializer(current_app.config.get('SECRET_KEY'), 3600) token = s.dumps({'email': form.email.data}) body = 'Click the link to reset your password. {}'\ .format(url_for('main.set_password', token=token, _external=True)) send_email(to=user.email, subject='Password Reset', body=body) flash('Link to reset a password has been sent to {}.'\ .format(form.email.data), 'warning') return redirect(url_for('main.index')) return render_template('main/reset_password.html', form=form)
def registerCustomer(): if current_user.is_authenticated: return redirect(url_for('customerPage')) form = RegistrationFormCustomer() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data.decode('utf-8')) register = Customer(username=form.username.data, name=form.name.data, surname=form.surname.data, email=form.email.data, city=form.city.data.upper(), password=hashed_password) db.session.add(register) db.session.commit() flash('Account created for %s, you can login now' % form.username.data) send_email(form.email.data, 'Registration completed successfully', 'email', username=form.username.data) return redirect(url_for('login')) return render_template('registerCustomer.html', title='Register', form=form)
def test_send_email(self, mock_method, mock_client): # Test send_email invokes send_email_ses. args = ['value1', 'value2'] kwargs = dict(key='value') val = send_email(*args, **kwargs) assert not mock_client.send_email.called assert mock_method.called mock_method.assert_called_with(*args, **kwargs, ses_client=mock_client) assert val is True # Test that send_email prints out error when an exception is raised. mock_method.reset_mock() # Raise an exception when this method is called. mock_method.side_effect = Exception('I am a super exception!') with captured_output() as (out, err): val = send_email(*args, **kwargs) assert val is False assert 'I am a super exception!' == out.getvalue().strip() assert not mock_client.send_email.called
def register(): if current_user.is_active: return redirect(url_for('pages.home')) form = RegistrationForm() if form.validate_on_submit(): email = form.email.data password = form.password.data if User.from_email(email).exists(): # Username isn't taken flash("That email is already in use.") else: user = User.create(email, password) token = current_app.ts.dumps(user.email, salt='email-confirm-key') confirm_url = url_for('pages.confirm_email', token=token, _external=True) app.send_email('Please confirm your email address.', email, confirm_url) flash( "Please check your inbox for a confirmation email. (It may take a couple minutes.)" ) return redirect(url_for('pages.login')) print('registration errors', form.errors) return render_template('register.html', form=form)
def main(args): apikey = args.get("SENDGRID_API_KEY") return app.send_email(apikey)