def test_phone_vote_error(self, vc, choice, sms): """ Testcase 1: given invalid choice number (String) """ request = mock.Mock() request.POST = { 'Body': 'aaa', 'From': '1234567890', } request.REQUEST = request.POST views.phone_vote(request) message = 'Please text a vote number correctly to vote.' sms.assert_called_with(message) """ Testcase 2: given invalid choice number (Not in Choice) """ request = mock.Mock() request.POST = { 'Body': '999', 'From': '1234567890', } request.REQUEST = request.POST choice.return_value = [] views.phone_vote(request) message = 'Please text a vote number correctly to vote.' sms.assert_called_with(message)
def test_phone_vote_success(self, VoterChoice, vc, choice, sms): """ Testcase 1: vote success """ request = mock.Mock() request.POST = { 'Body': '1', 'From': '1234567890', } request.REQUEST = request.POST c = mock.Mock(spec=models.Choice) poll = mock.Mock() def status(): return 'ONGOING' poll.status = status c.poll = poll c.votes = 0 c.save = mock.Mock() choice.return_value = [c] vc.return_value = [] views.phone_vote(request) message = 'Thanks for your vote!' sms.assert_called_with(message) self.assertEqual(c.votes, 1)
def test_phone_vote_sorry(self, vc, choice, sms): """ Testcase 1: try to vote twice in the same poll """ request = mock.Mock() request.POST = { 'Body': '1', 'From': '1234567890', } request.REQUEST = request.POST c = mock.Mock() choice.return_value = [c] vc.return_value = [mock.Mock()] views.phone_vote(request) message = 'Sorry, you have already voted in this poll.' \ + ' You cannot vote twice.' sms.assert_called_with(message) """ Testcase 2: try to vote before it starts """ request = mock.Mock() request.POST = { 'Body': '1', 'From': '1234567890', } request.REQUEST = request.POST c = mock.Mock() poll = mock.Mock() def status(): return 'PENDING' poll.status = status c.poll = poll c.votes = 0 choice.return_value = [c] vc.return_value = [] views.phone_vote(request) message = 'Sorry, your vote is invalid.' \ + ' This poll has not yet begun.' sms.assert_called_with(message) self.assertEqual(c.votes, 0) """ Testcase 3: try to vote after it finishes """ request = mock.Mock() request.POST = { 'Body': '1', 'From': '1234567890', } request.REQUEST = request.POST c = mock.Mock() poll = mock.Mock() def status(): return 'FINISHED' poll.status = status c.poll = poll c.votes = 0 choice.return_value = [c] vc.return_value = [] views.phone_vote(request) message = 'Sorry, your vote is invalid.' \ + ' This poll has already finished.'