From 5550e18cfd628fcb7431f560323f681d06d3edc7 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Mon, 22 Aug 2011 23:19:00 +0200 Subject: [PATCH] Add missing tests. Fixes #14 and #22. --- budget/forms.py | 3 +- budget/tests.py | 116 +++++++++++++++++++++++++++++++++++++++++++++--- budget/web.py | 8 ++++ 3 files changed, 120 insertions(+), 7 deletions(-) diff --git a/budget/forms.py b/budget/forms.py index 51da01f..6272b7f 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -85,7 +85,8 @@ class MemberForm(Form): def validate_name(form, field): if Person.query.filter(Person.name == field.data)\ - .filter(Person.project == form.project).all(): + .filter(Person.project == form.project)\ + .filter(Person.activated == True).all(): raise ValidationError("This project already have this member") diff --git a/budget/tests.py b/budget/tests.py index f16a1e1..9efb78a 100644 --- a/budget/tests.py +++ b/budget/tests.py @@ -35,7 +35,7 @@ class TestCase(unittest.TestCase): return test_client.post('/authenticate', data=dict( id=project, password=password), follow_redirects=True) - def create_project(self, name): + def post_project(self, name): """Create a fake project""" # create the project self.app.post("/create", data={ @@ -45,6 +45,11 @@ class TestCase(unittest.TestCase): 'contact_email': '%s@notmyidea.org' % name }) + def create_project(self, name): + models.db.session.add(models.Project(id=name, name=unicode(name), + password=name, contact_email="%s@notmyidea.org" % name)) + models.db.session.commit() + class BudgetTestCase(TestCase): def test_notifications(self): @@ -57,7 +62,7 @@ class BudgetTestCase(TestCase): # create a project self.login("raclette") - self.create_project("raclette") + self.post_project("raclette") self.app.post("/raclette/invite", data= {"emails": 'alexis@notmyidea.org'}) @@ -120,7 +125,7 @@ class BudgetTestCase(TestCase): self.assertEqual(len(models.Project.query.all()), 1) def test_membership(self): - self.create_project("raclette") + self.post_project("raclette") self.login("raclette") # adds a member to this project @@ -176,6 +181,11 @@ class BudgetTestCase(TestCase): result = self.app.get("/raclette/add") self.assertNotIn("fred", result.data) + # adding him again should reactivate him + self.app.post("/raclette/members/add", data={'name': 'fred' }) + self.assertEqual( + len(models.Project.query.get("raclette").active_members), 2) + def test_demo(self): # Test that it is possible to connect automatically by going onto /demo with web.app.test_client() as c: @@ -189,10 +199,104 @@ class BudgetTestCase(TestCase): def test_demo(self): # test that a demo project is created if none is defined + self.assertEqual([], models.Project.query.all()) + self.app.get("/demo") + self.assertTrue(models.Project.query.get("demo") is not None) + + def test_authentication(self): + # raclette that the login / logout process works + self.create_project("raclette") + + # try to see the project while not being authenticated should redirect + # to the authentication page + resp = self.app.post("/raclette", follow_redirects=True) + self.assertIn("Authentication", resp.data) + + # try to connect with wrong credentials should not work with web.app.test_client() as c: - self.assertEqual([], models.Project.query.all()) - c.get("/demo") - self.assertTrue(models.Project.query.get("demo") is not None) + resp = c.post("/authenticate", + data={'id': 'raclette', 'password': 'nope'}) + + self.assertIn("Authentication", resp.data) + self.assertNotIn('raclette', session) + + # try to connect with the right credentials should work + with web.app.test_client() as c: + resp = c.post("/authenticate", + data={'id': 'raclette', 'password': 'raclette'}) + + self.assertNotIn("Authentication", resp.data) + self.assertIn('raclette', session) + self.assertEqual(session['raclette'], 'raclette') + + # logout should wipe the session out + c.get("/exit") + self.assertNotIn('raclette', session) + + def test_manage_bills(self): + self.post_project("raclette") + + # add two persons + self.app.post("/raclette/members/add", data={'name': 'alexis' }) + self.app.post("/raclette/members/add", data={'name': 'fred' }) + + members_ids = [m.id for m in models.Project.query.get("raclette").members] + + # create a bill + self.app.post("/raclette/add", data={ + 'date': '2011-08-10', + 'what': u'fromage à raclette', + 'payer': members_ids[0], + 'payed_for': members_ids, + 'amount': '25', + }) + raclette = models.Project.query.get("raclette") + bill = models.Bill.query.one() + self.assertEqual(bill.amount, 25) + + # edit the bill + resp = self.app.post("/raclette/edit/%s" % bill.id, data={ + 'date': '2011-08-10', + 'what': u'fromage à raclette', + 'payer': members_ids[0], + 'payed_for': members_ids, + 'amount': '10', + }) + + bill = models.Bill.query.one() + self.assertEqual(bill.amount, 10, "bill edition") + + # delete the bill + self.app.get("/raclette/delete/%s" % bill.id) + self.assertEqual(0, len(models.Bill.query.all()), "bill deletion") + + # test balance + self.app.post("/raclette/add", data={ + 'date': '2011-08-10', + 'what': u'fromage à raclette', + 'payer': members_ids[0], + 'payed_for': members_ids, + 'amount': '19', + }) + + self.app.post("/raclette/add", data={ + 'date': '2011-08-10', + 'what': u'fromage à raclette', + 'payer': members_ids[1], + 'payed_for': members_ids[0], + 'amount': '20', + }) + + self.app.post("/raclette/add", data={ + 'date': '2011-08-10', + 'what': u'fromage à raclette', + 'payer': members_ids[1], + 'payed_for': members_ids, + 'amount': '17', + }) + + balance = models.Project.query.get("raclette").get_balance() + self.assertEqual(set(balance.values()), set([19.0, -19.0])) if __name__ == "__main__": diff --git a/budget/web.py b/budget/web.py index 78886d8..c69189e 100644 --- a/budget/web.py +++ b/budget/web.py @@ -173,6 +173,14 @@ def add_member(): form = MemberForm(g.project) if request.method == "POST": if form.validate(): + # if the user is already bound to the project, just reactivate him + person = Person.query.filter(Person.name == form.name.data)\ + .filter(Project.id == g.project.id).all() + if person: + person[0].activated = True + db.session.commit() + return redirect(url_for("list_bills")) + db.session.add(Person(name=form.name.data, project=g.project)) db.session.commit() return redirect(url_for("list_bills"))