def test_form(self): # test empty form with self.captured_templates(self.app) as templates: resp = self.client.post(self.url) self.assertStatus(resp, 200) template, context = templates[0] form = context['form'] self.assertTrue(u'This field is required.' in form.errors['email']) # test validate email with self.captured_templates(self.app) as templates: resp = self.client.post(self.url, data=dict(email='*****@*****.**')) self.assertStatus(resp, 200) template, context = templates[0] form = context['form'] self.assertTrue(u'E-mail in use.' in form.errors['email']) # test valid form with mail.record_messages() as outbox: self.assertEquals(len(outbox), 0) resp = self.client.post(self.url, data=dict(email='*****@*****.**')) self.assertRedirects(resp, self.redirect_to) self.assertEquals(len(outbox), 1) self.assertEquals(outbox[0].subject, u'Confirm your account - Flask Example.') # test valid form after redirect resp = self.client.post(self.url, data=dict(email='*****@*****.**'), follow_redirects=True) self.assertStatus(resp, 200) self.assertTrue( 'Check your email to confirm registration.' in resp.data)
def test_form(self): # test empty form with self.captured_templates(self.app) as templates: resp = self.client.post(self.url) self.assertStatus(resp, 200) template, context = templates[0] form = context['form'] self.assertTrue( u'This field is required.' in form.errors['email'] ) # test validate email with self.captured_templates(self.app) as templates: resp = self.client.post( self.url, data=dict(email='*****@*****.**') ) self.assertStatus(resp, 200) template, context = templates[0] form = context['form'] self.assertTrue( u'E-mail not found.' in form.errors['email'] ) # test valid form with mail.record_messages() as outbox: self.assertEquals(len(outbox), 0) resp = self.client.post( self.url, data=dict(email='*****@*****.**') ) self.assertRedirects(resp, self.redirect_to) self.assertEquals(len(outbox), 1) self.assertEquals( outbox[0].subject, u'Recover your password - Flask Example.' ) # test valid form after redirect resp = self.client.post( self.url, data=dict(email='*****@*****.**'), follow_redirects=True ) self.assertStatus(resp, 200) self.assertTrue( 'Check it out at your email instructions for setting a new password.' in resp.data )
def contact(): if request.method == 'POST': extra_headers = {} if 'return-addr' in request.form: extra_headers['Reply-To'] = request.form['return-addr']; with mail.record_messages() as outbox: msg = Message(recipients=[app.config['MAIL_DEFAULT_RECIPIENT']], subject="Contact form message from %s" % (app.config['SERVER_NAME'] if app.config['SERVER_NAME'] is not None else 'the website'), body=request.form['content'], extra_headers=extra_headers) mail.send(msg) sent = outbox[0] flash("Message has been sent.") return redirect(url_for('article')) article = models.Article.query.get('contact') return render_template('contact_form.html', article=article)
def test_confirmation(self): """ test /customer/<customer_id>/send_confirmation and /customer/confirm/<token> """ with mail.record_messages() as outbox: customer_id = "180422867908286360754098232165804040712" rv = self.app.put('/customer/' + customer_id + '/send_confirmation', headers=self.headers, content_type='application/json') customer = Customer.objects.filter(customer_id=customer_id).first() token = customer.confirmation_token assert rv.status_code == 200 assert customer.confirmation_token_expires_at > datetime.now() assert customer.confirmation_token is not None assert len(outbox) == 1 rv = self.app.get('/customer/confirm/' + token, headers=self.headers, content_type='application/json') customer = Customer.objects.filter(customer_id=customer_id).first() assert rv.status_code == 200 assert customer.confirmed_on is not None