From 683c7ee0b87bde0866a1f1e815fe50a6a87ed22e Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Wed, 10 Aug 2011 17:06:32 +0200 Subject: [PATCH] Add tests about project creation and member add. See #14 --- budget/forms.py | 4 +++ budget/tests.py | 70 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/budget/forms.py b/budget/forms.py index 4699531..0c5394b 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -26,6 +26,10 @@ class ProjectForm(Form): contact_email = TextField("Email", validators=[Required(), Email()]) submit = SubmitField("Create the project") + def validate_id(form, field): + if Project.query.get(field.data): + raise ValidationError("This project id is already used") + def save(self): """Create a new project with the information given by this form. diff --git a/budget/tests.py b/budget/tests.py index b79f16f..2277301 100644 --- a/budget/tests.py +++ b/budget/tests.py @@ -2,12 +2,12 @@ import os import tempfile import unittest -from flask import g +from flask import session import web import models -class BudgetTestCase(unittest.TestCase): +class TestCase(unittest.TestCase): def setUp(self): web.app.config['TESTING'] = True @@ -38,24 +38,26 @@ class BudgetTestCase(unittest.TestCase): def create_project(self, name): """Create a fake project""" # create the project - project = models.Project(id=name, name=unicode(name), password=name, - contact_email="%s@notmyidea.org" % name) - models.db.session.add(project) - models.db.session.commit() + self.app.post("/create", data={ + 'name': name, + 'id': name, + 'password': name, + 'contact_email': '%s@notmyidea.org' % name + }) - return project +class BudgetTestCase(TestCase): def test_notifications(self): """Test that the notifications are sent, and that email adresses are checked properly. """ - # create a project - self.create_project("raclette") - - self.login("raclette") - # sending a message to one person with web.mail.record_messages() as outbox: + + # create a project + self.login("raclette") + + self.create_project("raclette") self.app.post("/raclette/invite", data= {"emails": 'alexis@notmyidea.org'}) @@ -87,5 +89,49 @@ class BudgetTestCase(unittest.TestCase): self.assertEqual(len(outbox), 0) + 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) + + if __name__ == "__main__": unittest.main()