From 5be65f7167df0d0256fbe8124dcdcad475b906d8 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 20:52:22 -0300 Subject: [PATCH 1/8] cannot vote in the same day --- tests/vote_tests.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/vote_tests.py b/tests/vote_tests.py index 89a73d8..a569767 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -70,5 +70,28 @@ class TestVote(LunchoTests): self.assertJsonError(rv, 406, 'The vote must register 2 places') return + def test_already_voted(self): + """Try to vote when the user already voted.""" + group = self._group() + place = self._place() + group.places.append(place) + self.user.groups.append(group) + server.db.session.commit() + + group_id = group.id + token = self.user.token + + request = {'choices': [place.id]} + rv = self.post('/vote/{group_id}/'.format(group_id=group_id), + request, + token=token) + self.assertJsonOk(rv) # vote in the day + + rv = self.post('/vote/{group_id}/'.format(group_id=group_id), + request, + token=token) + self.assertJsonError(rv, 406, 'User already voted today') + return + if __name__ == '__main__': unittest.main() From 3d377cd8ea61035658d81e9b99901c9ff4daa9cc Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 20:55:23 -0300 Subject: [PATCH 2/8] cannot vote in the same day even in different groups --- tests/vote_tests.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/vote_tests.py b/tests/vote_tests.py index a569767..dda91b2 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -93,5 +93,36 @@ class TestVote(LunchoTests): self.assertJsonError(rv, 406, 'User already voted today') return + def test_already_vote_other_group(self): + """Try to vote in two different groups in the same day.""" + group1 = self._group() + place1 = self._place() + group1.places.append(place1) + self.user.groups.append(group1) + + group2 = self._group() + place2 = self._place() + group2.places.append(place2) + self.user.groups.append(group2) + server.db.session.commit() + + group1_id = group1.id + group2_id = group2.id + place1_id = place1.id + place2_id = place2.id + token = self.user.token + + request = {'choices': [place1_id]} + rv = self.post('/vote/{group_id}/'.format(group_id=group1_id), + request, + token=token) + self.assertJsonOk(rv) # first vote for the day + + request = {'choices': [place2_id]} + rv = self.post('/vote/{group_id}/'.format(group_id=group2_id), + request, + token=token) + self.assertJsonError(rv, 406, 'User already voted today') + if __name__ == '__main__': unittest.main() From b7c8274f31c458081e9740260b608f6cc53e2f1e Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 21:05:37 -0300 Subject: [PATCH 3/8] cannot vote for places not part of the group --- tests/vote_tests.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/vote_tests.py b/tests/vote_tests.py index dda91b2..b8857c5 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -123,6 +123,23 @@ class TestVote(LunchoTests): request, token=token) self.assertJsonError(rv, 406, 'User already voted today') + return + + def test_vote_place_not_in_group(self): + """Vote for a place that doesn't belong to the group.""" + group = self._group() + place1 = self._place() + place2 = self._place() + group.places.append(place2) + self.user.groups.append(group) + server.db.session.commit() + + request = {'choices': [place1.id]} + rv = self.post('/vote/{group_id}/'.format(group_id=group.id), + request, + token=self.user.token) + self.assertJsonError(rv, 404, 'Places are not part of this group') + return if __name__ == '__main__': unittest.main() From 9a4e6269c0b9820988d1fa739ee179b03cac937b Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 21:11:59 -0300 Subject: [PATCH 4/8] cannot vote for the same place twice --- luncho/blueprints/voting.py | 2 +- tests/vote_tests.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/luncho/blueprints/voting.py b/luncho/blueprints/voting.py index 3894ad6..f372800 100644 --- a/luncho/blueprints/voting.py +++ b/luncho/blueprints/voting.py @@ -108,7 +108,7 @@ class PlacesVotedMoreThanOnceException(LunchoException): def _json(self): super(PlacesVotedMoreThanOnceException, self)._json() - self.json['places'] = self.places + self.json['places'] = list(self.places) # ---------------------------------------------------------------------- diff --git a/tests/vote_tests.py b/tests/vote_tests.py index b8857c5..188f996 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -141,5 +141,20 @@ class TestVote(LunchoTests): self.assertJsonError(rv, 404, 'Places are not part of this group') return + def test_vote_for_same_place_twice(self): + """Vote for a place more than once.""" + group = self._group() + place = self._place() + group.places.append(place) + self.user.groups.append(group) + server.db.session.commit() + + request = {'choices': [place.id, place.id]} + rv = self.post('/vote/{group_id}/'.format(group_id=group.id), + request, + token=self.user.token) + self.assertJsonError(rv, 409, 'Places voted more than once') + return + if __name__ == '__main__': unittest.main() From c6c039ce7b328f387f3935ae3196e0e972028153 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 21:43:10 -0300 Subject: [PATCH 5/8] cannot vote for groups that doesnt exist --- tests/vote_tests.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/vote_tests.py b/tests/vote_tests.py index 188f996..4e6c25b 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -156,5 +156,14 @@ class TestVote(LunchoTests): self.assertJsonError(rv, 409, 'Places voted more than once') return + def test_unknown_group(self): + """Try to vote in a group that doesn't exist.""" + request = {'choices': [100]} + rv = self.post('/vote/{group_id}/'.format(group_id=100), + request, + token=self.user.token) + self.assertJsonError(rv, 404, 'Group not found') + return + if __name__ == '__main__': unittest.main() From 9bf2c8167722dfc6357c184b0aa08a3adf02f653 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 21:44:59 -0300 Subject: [PATCH 6/8] cannot vote for groups that the user is not a member --- tests/vote_tests.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/vote_tests.py b/tests/vote_tests.py index 4e6c25b..0a39037 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -165,5 +165,24 @@ class TestVote(LunchoTests): self.assertJsonError(rv, 404, 'Group not found') return + def test_vote_in_group_not_member(self): + """Try to vote for a group that the user is not a member.""" + group = self._group() + place = self._place() + group.places.append(place) + server.db.session.commit() + + user2 = self.create_user(name='newUser', + fullname='new user', + verified=True, + create_token=True) + + request = {'choices': [place.id]} + rv = self.post('/vote/{group_id}/'.format(group_id=group.id), + request, + token=user2.token) + self.assertJsonError(rv, 403, 'User is not member of this group') + return + if __name__ == '__main__': unittest.main() From 7348c7a7bb33ed9f65f0f47fc76a081e4e51d970 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 21:48:57 -0300 Subject: [PATCH 7/8] cannot vote for places that doesnt exist --- tests/vote_tests.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/vote_tests.py b/tests/vote_tests.py index 0a39037..f24b8ba 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -184,5 +184,19 @@ class TestVote(LunchoTests): self.assertJsonError(rv, 403, 'User is not member of this group') return + def test_vote_place_doesnt_exist(self): + """Vote for a place that doesnt exist.""" + group = self._group() + place = self._place() + group.places.append(place) + server.db.session.commit() + + request = {'choices': [place.id + 10]} + rv = self.post('/vote/{group_id}/'.format(group_id=group.id), + request, + token=self.user.token) + self.assertJsonError(rv, 404, 'Place not found') + return + if __name__ == '__main__': unittest.main() From 3f0a7b1dcd1d224dbd23b05567d63b0bc7c9e006 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 14 Apr 2014 21:53:58 -0300 Subject: [PATCH 8/8] added logging to run the repr() calls --- luncho/blueprints/voting.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/luncho/blueprints/voting.py b/luncho/blueprints/voting.py index f372800..7528d91 100644 --- a/luncho/blueprints/voting.py +++ b/luncho/blueprints/voting.py @@ -150,10 +150,14 @@ def cast_vote(group_id): # finally, cast the vote vote = Vote(request.user, group_id) + LOG.debug('User {user} casted vote {vote}'.format(user=request.user, + vote=vote)) db.session.add(vote) db.session.commit() # so vote gets an id for (pos, place_id) in enumerate(request.as_json.get('choices')): place = CastedVote(vote, pos, place_id) + LOG.debug('\tVoted {place} in {pos} position'.format(place=place, + pos=pos)) db.session.add(place) db.session.commit()