def test_contact_page_bad_body_field(self): form = ContactForm(name='Test Name', email='*****@*****.**', subject='Hello World', body=None) response = self.client.post('/contact', data=form.data) self.assertIsNot(form.validate(), True) # body is required to validate self.assertEqual(response.status_code, 200) form = ContactForm(name='Test Name', email='*****@*****.**', subject='Hello World', body='a') response = self.client.post('/contact', data=form.data) self.assertIsNot(form.validate(), True) # body should be too short to validate self.assertEqual(response.status_code, 200)
def test_contact_page_bad_name_field(self): form = ContactForm(name=None, email='*****@*****.**', subject='Hello World', body='This should not validate') response = self.client.post('/contact', data=form.data) self.assertIsNot(form.validate(), True) # name is required to validate self.assertEqual(response.status_code, 200)
def test_contact_page_bad_email_field(self): form = ContactForm(name='Test Name', email='test@mail', subject='Hello World', body='This should not validate') response = self.client.post('/contact', data=form.data) self.assertIsNot(form.validate(), True) # email is not valid self.assertEqual(response.status_code, 200)
def test_contact_page_post(self): form = ContactForm(name='Test Name', email='*****@*****.**', subject='Hello World', body='Is there anybody out there?') response = self.client.post('/contact', data=form.data) self.assertEqual( response.status_code, 302) # successful validation should result in redirect response = self.client.post('/contact', data=form.data, follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertTrue(form.validate())
def contact(): form = ContactForm() if request.method == 'POST': if form.validate() is False: flash('All fields are required', 'danger') return render_template('contact.html', form=form) else: msg = Message(form.subject.data, sender="*****@*****.**", recipients=['*****@*****.**']) msg.body = """ From: %s %s <%s> %s """ % (form.firstname.data, form.lastname.data, form.email.data, form.message.data) log.info( "Sending message from {first} {last} to [email protected]" .format(first=form.firstname.data, last=form.lastname.data)) mail.send(msg) flash('Message Sent!', 'success') # TODO: Handle error in case of unsuccessful sending return render_template('contact.html', form=form) elif request.method == 'GET': return render_template("contact.html", form=form)