2011-08-09 19:22:09 +02:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2011-08-10 17:06:32 +02:00
|
|
|
from flask import session
|
2011-08-09 19:22:09 +02:00
|
|
|
|
|
|
|
import web
|
|
|
|
import models
|
|
|
|
|
2011-08-10 17:06:32 +02:00
|
|
|
class TestCase(unittest.TestCase):
|
2011-08-09 19:22:09 +02:00
|
|
|
|
2011-08-09 23:22:57 +02:00
|
|
|
def setUp(self):
|
|
|
|
web.app.config['TESTING'] = True
|
2011-08-09 19:22:09 +02:00
|
|
|
|
2011-08-09 23:22:57 +02:00
|
|
|
self.fd, self.fp = tempfile.mkstemp()
|
|
|
|
web.app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///%s" % self.fp
|
|
|
|
web.app.config['CSRF_ENABLED'] = False # simplify the tests
|
|
|
|
self.app = web.app.test_client()
|
2011-08-09 19:22:09 +02:00
|
|
|
|
2011-08-09 23:22:57 +02:00
|
|
|
models.db.init_app(web.app)
|
|
|
|
web.mail.init_app(web.app)
|
|
|
|
|
|
|
|
models.db.app = web.app
|
|
|
|
models.db.create_all()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# clean after testing
|
|
|
|
os.close(self.fd)
|
|
|
|
os.unlink(self.fp)
|
2011-08-09 19:22:09 +02:00
|
|
|
|
|
|
|
def login(self, project, password=None, test_client=None):
|
|
|
|
password = password or project
|
|
|
|
test_client = test_client or self.app
|
|
|
|
|
|
|
|
return test_client.post('/authenticate', data=dict(
|
|
|
|
id=project, password=password), follow_redirects=True)
|
|
|
|
|
|
|
|
def create_project(self, name):
|
|
|
|
"""Create a fake project"""
|
|
|
|
# create the project
|
2011-08-10 17:06:32 +02:00
|
|
|
self.app.post("/create", data={
|
|
|
|
'name': name,
|
|
|
|
'id': name,
|
|
|
|
'password': name,
|
|
|
|
'contact_email': '%s@notmyidea.org' % name
|
|
|
|
})
|
2011-08-09 19:22:09 +02:00
|
|
|
|
2011-08-10 17:06:32 +02:00
|
|
|
class BudgetTestCase(TestCase):
|
2011-08-09 19:22:09 +02:00
|
|
|
|
2011-08-09 23:22:57 +02:00
|
|
|
def test_notifications(self):
|
|
|
|
"""Test that the notifications are sent, and that email adresses
|
|
|
|
are checked properly.
|
|
|
|
"""
|
|
|
|
# sending a message to one person
|
|
|
|
with web.mail.record_messages() as outbox:
|
2011-08-10 17:06:32 +02:00
|
|
|
|
|
|
|
# create a project
|
|
|
|
self.login("raclette")
|
|
|
|
|
|
|
|
self.create_project("raclette")
|
2011-08-09 23:22:57 +02:00
|
|
|
self.app.post("/raclette/invite", data=
|
|
|
|
{"emails": 'alexis@notmyidea.org'})
|
2011-08-09 19:22:09 +02:00
|
|
|
|
2011-08-09 23:22:57 +02:00
|
|
|
self.assertEqual(len(outbox), 1)
|
|
|
|
self.assertEqual(outbox[0].recipients, ["alexis@notmyidea.org"])
|
2011-08-09 19:22:09 +02:00
|
|
|
|
2011-08-09 23:22:57 +02:00
|
|
|
# sending a message to multiple persons
|
|
|
|
with web.mail.record_messages() as outbox:
|
|
|
|
self.app.post("/raclette/invite", data=
|
|
|
|
{"emails": 'alexis@notmyidea.org, toto@notmyidea.org'})
|
|
|
|
|
|
|
|
# only one message is sent to multiple persons
|
|
|
|
self.assertEqual(len(outbox), 1)
|
|
|
|
self.assertEqual(outbox[0].recipients,
|
|
|
|
["alexis@notmyidea.org", "toto@notmyidea.org"])
|
|
|
|
|
|
|
|
# mail address checking
|
|
|
|
with web.mail.record_messages() as outbox:
|
|
|
|
response = self.app.post("/raclette/invite", data={"emails": "toto"})
|
|
|
|
self.assertEqual(len(outbox), 0) # no message sent
|
|
|
|
self.assertIn("The email toto is not valid", response.data)
|
|
|
|
|
|
|
|
# mixing good and wrong adresses shouldn't send any messages
|
|
|
|
with web.mail.record_messages() as outbox:
|
|
|
|
self.app.post("/raclette/invite", data=
|
|
|
|
{"emails": 'alexis@notmyidea.org, alexis'}) # not valid
|
|
|
|
|
|
|
|
# only one message is sent to multiple persons
|
|
|
|
self.assertEqual(len(outbox), 0)
|
2011-08-09 19:22:09 +02:00
|
|
|
|
|
|
|
|
2011-08-10 17:06:32 +02:00
|
|
|
def test_project_creation(self):
|
|
|
|
with web.app.test_client() as c:
|
|
|
|
|
|
|
|
# add a valid project
|
|
|
|
c.post("/create", data={
|
|
|
|
'name': 'The fabulous raclette party',
|
|
|
|
'id': 'raclette',
|
|
|
|
'password': 'party',
|
|
|
|
'contact_email': 'raclette@notmyidea.org'
|
|
|
|
})
|
|
|
|
|
|
|
|
# session is updated
|
|
|
|
self.assertEqual(session['raclette'], 'party')
|
|
|
|
|
|
|
|
# project is created
|
|
|
|
self.assertEqual(len(models.Project.query.all()), 1)
|
|
|
|
|
|
|
|
# Add a second project with the same id
|
|
|
|
models.Project.query.get('raclette')
|
|
|
|
|
|
|
|
result = c.post("/create", data={
|
|
|
|
'name': 'Another raclette party',
|
|
|
|
'id': 'raclette', #already used !
|
|
|
|
'password': 'party',
|
|
|
|
'contact_email': 'raclette@notmyidea.org'
|
|
|
|
})
|
|
|
|
|
|
|
|
# no new project added
|
|
|
|
self.assertEqual(len(models.Project.query.all()), 1)
|
|
|
|
|
|
|
|
def test_add_member(self):
|
|
|
|
self.create_project("raclette")
|
|
|
|
self.login("raclette")
|
|
|
|
|
|
|
|
# adds a member to this project
|
|
|
|
self.app.post("/raclette/members/add", data={'name': 'alexis' })
|
|
|
|
self.assertEqual(len(models.Project.query.get("raclette").members), 1)
|
|
|
|
|
|
|
|
# adds him twice
|
|
|
|
result = self.app.post("/raclette/members/add", data={'name': 'alexis' })
|
|
|
|
# should not accept him
|
|
|
|
self.assertEqual(len(models.Project.query.get("raclette").members), 1)
|
|
|
|
|
|
|
|
|
2011-08-09 19:22:09 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|