You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.8 KiB
97 lines
2.8 KiB
#!/usr/bin/env python |
|
# -*- encoding: utf-8 -*- |
|
|
|
import unittest |
|
|
|
from luncho import server |
|
|
|
from base import LunchoTests |
|
from luncho.server import Group |
|
from luncho.server import Place |
|
|
|
|
|
class TestVote(LunchoTests): |
|
def setUp(self): |
|
super(TestVote, self).setUp() |
|
self.default_user() |
|
|
|
def tearDown(self): |
|
super(TestVote, self).tearDown() |
|
|
|
def _group(self): |
|
"""Add a default group.""" |
|
group = Group(name='Test group', |
|
owner=self.user) |
|
server.db.session.add(group) |
|
self.user.groups.append(group) |
|
server.db.session.commit() |
|
return group |
|
|
|
def _place(self, user=None): |
|
"""Add a default place, linked to the user.""" |
|
if not user: |
|
user = self.user |
|
|
|
place = Place(name='Place', |
|
owner=user) |
|
server.db.session.add(place) |
|
server.db.session.commit() |
|
return place |
|
|
|
def test_cast_vote(self): |
|
"""Try to cast a vote.""" |
|
group = self._group() |
|
place = self._place() |
|
group.places.append(place) |
|
self.user.groups.append(group) |
|
server.db.session.commit() |
|
|
|
request = {'choices': [place.id]} |
|
rv = self.post('/vote/{group_id}/'.format(group_id=group.id), |
|
request, |
|
token=self.user.token) |
|
self.assertJsonOk(rv) |
|
return |
|
|
|
def test_cast_less_votes(self): |
|
"""Try to cast a vote with not enough places.""" |
|
group = self._group() |
|
place1 = self._place() |
|
place2 = self._place() |
|
group.places.append(place1) |
|
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, 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()
|
|
|