diff options
author | Code Raguet <ignacio.code@gmail.com> | 2013-10-03 20:27:34 (GMT) |
---|---|---|
committer | Code Raguet <ignacio.code@gmail.com> | 2013-10-03 20:27:34 (GMT) |
commit | 8ec82cdadab563925225f3a115258ad9a2accda9 (patch) | |
tree | 0975c12134097266ec929dd4febcbb064383a081 | |
parent | 6f50e7bee51537f23eb73e7e9966662575b34a0f (diff) |
add method is_authored_by to PollResultFile class
-rw-r--r-- | webapp/polls/models.py | 6 | ||||
-rw-r--r-- | webapp/polls/tests/poll_result_file_tests.py | 24 |
2 files changed, 30 insertions, 0 deletions
diff --git a/webapp/polls/models.py b/webapp/polls/models.py index 103feee..58bd863 100644 --- a/webapp/polls/models.py +++ b/webapp/polls/models.py @@ -1572,3 +1572,9 @@ class PollResultFile(object): exists = self.hash in hashes return exists + + def is_authored_by(self, user): + """Check if result is authored by user.""" + result_author = self.get_pollster_username() + username = user.username + return username == result_author diff --git a/webapp/polls/tests/poll_result_file_tests.py b/webapp/polls/tests/poll_result_file_tests.py index 281e44c..dfcd644 100644 --- a/webapp/polls/tests/poll_result_file_tests.py +++ b/webapp/polls/tests/poll_result_file_tests.py @@ -6,6 +6,7 @@ import hashlib from django.test import TestCase from django.conf import settings +from mock import Mock, patch from polls.models import PollResultFile, Poll from utils.test import MongoTestCase, create_results_dir @@ -248,3 +249,26 @@ class RemovePollResultFileTest(TestCase): result_file.delete() saved_file_path = result_file.file_path self.assertFalse(os.path.exists(saved_file_path)) + + +class AuthoredByTest(TestCase): + + def test_it_should_be_True_if_user_is_in_result_file(self): + username = "Is my result" + user = Mock() + user.username = username + file_path = "a_path" + with patch('__builtin__.open'), patch('polls.models.json'): + prf = PollResultFile(file_path) + prf.get_pollster_username = Mock(return_value=username) + self.assertTrue(prf.is_authored_by(user)) + + def test_it_should_be_False_if_user_is_not_in_result_file(self): + username = "Is not my result" + user = Mock() + user.username = username + file_path = "a_path" + with patch('__builtin__.open'), patch('polls.models.json'): + prf = PollResultFile(file_path) + prf.get_pollster_username = Mock(return_value="Other pollster") + self.assertFalse(prf.is_authored_by(user)) |