Skip to content

Commit

Permalink
WIP: Setting up tests
Browse files Browse the repository at this point in the history
test index page
test post to form submission
test bad submission
  • Loading branch information
speg committed Jan 14, 2015
1 parent 502d067 commit e22a10d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ sta.env
pro.env

celerybeat-schedule.db
.Python
include
4 changes: 4 additions & 0 deletions formspree/forms/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def send_email(to=None, subject=None, text=None, html=None, sender=None, cc=None

log.info('Queuing message to %s' % str(to))

if settings.TESTING:
# replace this with a real mock email function?
return True, ""

result = requests.post(
'https://api.sendgrid.com/api/mail.send.json',
data=data
Expand Down
7 changes: 6 additions & 1 deletion formspree/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys

# load a bunch of environment

Expand All @@ -7,9 +8,13 @@
if DEBUG:
SQLALCHEMY_ECHO = True


TESTING = True if 'test' in sys.argv else False
TEST_DATABASE = os.getenv('TEST_DATABASE_URL')
SQLALCHEMY_DATABASE_URI = TEST_DATABASE if TESTING else os.getenv('DATABASE_URL')

LOG_LEVEL = os.getenv('LOG_LEVEL') or 'debug'
NONCE_SECRET = os.getenv('NONCE_SECRET')
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL')

SERVICE_NAME = os.getenv('SERVICE_NAME') or 'Forms'
SERVICE_URL = os.getenv('SERVICE_URL') or 'http://example.com'
Expand Down
53 changes: 53 additions & 0 deletions formspree/tests/FormPosts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
import unittest

from formspree import create_app, app
from formspree.forms.models import Form

ajax_headers = {
'Referer': 'example.com',
'X_REQUESTED_WITH': 'xmlhttprequest'
}

class FormPostsTestCase(unittest.TestCase):

def setUp(self):
#create database here?
self.app = create_app() # new app everytime?
self.client = self.app.test_client()


def tearDown(self):
#destroy database here?
Form.query.delete()
c = Form.query.count()
print 'Torn down. %s left.' % c #TODO: this always says 0, but there is still a row left in the database

def test_index_page(self):
r = self.client.get('/')
self.assertEqual(200, r.status_code)

def test_submit_form(self):
r = self.client.post('/alice@example.com',
headers = ajax_headers,
data={'name': 'alice'}
)
self.assertEqual(1, Form.query.count())

def test_second_form(self):
r = self.client.post('/bob@example.com',
headers = ajax_headers,
data={'name': 'bob'}
)
self.assertEqual(1, Form.query.count())

def test_fail_form_submission(self):
no_referer = ajax_headers.copy()
del no_referer['Referer']
r = self.client.post('/bob@example.com',
headers = no_referer,
data={'name': 'bob'}
)
self.assertNotEqual(200, r.status_code)


Empty file added formspree/tests/__init__.py
Empty file.
14 changes: 13 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import unittest

from flask.ext.script import Manager, prompt_bool
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.migrate import Migrate, MigrateCommand, upgrade

from formspree import create_app, app
from formspree.tests import FormPosts

forms_app = create_app()
manager = Manager(forms_app)
Expand All @@ -15,6 +18,15 @@
def run_debug(port=5000):
forms_app.run(host='0.0.0.0', debug=True, port=int(port))

@manager.command
def test():
from formspree.settings import TEST_DATABASE
assert TEST_DATABASE, "Please configure environment variable: TEST_DATABASE_URL with test database."
print "Launching tests:"
upgrade()
suite = unittest.TestLoader().loadTestsFromTestCase(FormPosts.FormPostsTestCase)
unittest.TextTestRunner(verbosity=2).run(suite)


@manager.command
def unsubscribe(email=None, host=None):
Expand Down

0 comments on commit e22a10d

Please sign in to comment.