def test_likes(self): """api returns list of likes""" like = testutils.like_post(self.post, self.user) other_like = testutils.like_post(self.post, self.user) response = self.client.get(self.api_link) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), [ { 'id': other_like.id, 'liked_on': serialize_datetime(other_like.liked_on), 'liker_id': self.user.id, 'username': self.user.username, 'avatars': self.user.avatars, 'url': self.user.get_absolute_url(), }, { 'id': like.id, 'liked_on': serialize_datetime(like.liked_on), 'liker_id': self.user.id, 'username': self.user.username, 'avatars': self.user.avatars, 'url': self.user.get_absolute_url(), }, ] ) # api has no showstoppers for likes by deleted users like.liker = None like.save() other_like.liker = None other_like.save() response = self.client.get(self.api_link) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), [ { 'id': other_like.id, 'liked_on': serialize_datetime(other_like.liked_on), 'liker_id': None, 'username': self.user.username, 'avatars': None, 'url': None, }, { 'id': like.id, 'liked_on': serialize_datetime(like.liked_on), 'liker_id': None, 'username': self.user.username, 'avatars': None, 'url': None, }, ] )
def test_list_handles_search(self): """list returns found username changes""" self.user.set_username('NewUsername', self.user) self.user.save() override_acl(self.user, {'can_see_users_name_history': False}) response = self.client.get('%s?user=%s&search=new' % (self.link, self.user.pk)) self.assertEqual(response.status_code, 200) username_change = self.user.namechanges.all()[0] self.assertEqual(response.json(), { 'page': 1, 'pages': 1, 'count': 1, 'first': None, 'previous': None, 'next': None, 'last': None, 'before': 0, 'more': 0, 'results': [ { 'id': username_change.id, 'user': { 'id': self.user.id, 'username': '******', 'slug': 'newusername', 'avatars': self.user.avatars, }, 'changed_by': { 'id': self.user.id, 'username': '******', 'slug': 'newusername', 'avatars': self.user.avatars, }, 'changed_by_username': '******', 'changed_on': serialize_datetime(username_change.changed_on), 'new_username': '******', 'old_username': '******' } ], }) override_acl(self.user, {'can_see_users_name_history': False}) response = self.client.get('%s?user=%s&search=usernew' % (self.link, self.user.pk)) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), { 'page': 1, 'pages': 1, 'count': 0, 'first': None, 'previous': None, 'next': None, 'last': None, 'before': 0, 'more': 0, 'results': [], })
def test_poll_all_choices_replaced(self): """api edits all poll choices out""" response = self.put(self.api_link, data={ 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'is_public': True, 'choices': [ { 'label': '\nRed ', }, { 'label': 'Green', }, { 'label': 'Blue', }, ], }) self.assertEqual(response.status_code, 200) poll = Poll.objects.all()[0] expected_choices = [] for choice in poll.choices: self.assertIn(choice['label'], ["Red", "Green", "Blue"]) expected_choices.append(choice.copy()) expected_choices[-1]['selected'] = False self.assertEqual( response.json(), { 'id': poll.id, 'poster': { 'id': self.user.id, 'username': self.user.username, 'slug': self.user.slug, }, 'posted_on': serialize_datetime(poll.posted_on), 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'votes': 0, 'is_public': False, 'choices': expected_choices, }) # votes were removed self.assertEqual(self.poll.pollvote_set.count(), 0)
def get_votes_json(self): choices_votes = {choice['hash']: [] for choice in self.poll.choices} queryset = self.poll.pollvote_set.order_by('-id').select_related() for vote in queryset: choices_votes[vote.choice_hash].append({ 'id': vote.voter_id, 'username': vote.voter_name, 'slug': vote.voter_slug, 'voted_on': serialize_datetime(vote.voted_on), }) return choices_votes
def get_votes_json(self): choices_votes = {choice['hash']: [] for choice in self.poll.choices} queryset = self.poll.pollvote_set.order_by('-id').select_related() for vote in queryset: if vote.voter: url = vote.voter.get_absolute_url() else: url = None choices_votes[vote.choice_hash].append({ 'username': vote.voter_name, 'voted_on': serialize_datetime(vote.voted_on), 'url': url }) return choices_votes
def test_vote_change(self): """api handles vote change""" self.poll.allow_revotes = True self.poll.save() add_acl(self.user, self.poll) response = self.post(self.api_link, data=['aaaaaaaaaaaa', 'bbbbbbbbbbbb']) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), { 'id': self.poll.id, 'poster_name': self.user.username, 'posted_on': serialize_datetime(self.poll.posted_on), 'length': 0, 'question': "Lorem ipsum dolor met?", 'allowed_choices': 2, 'allow_revotes': True, 'votes': 4, 'is_public': False, 'acl': self.poll.acl, 'choices': [ { 'hash': 'aaaaaaaaaaaa', 'label': 'Alpha', 'selected': True, 'votes': 2 }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Beta', 'selected': True, 'votes': 1 }, { 'hash': 'gggggggggggg', 'label': 'Gamma', 'selected': False, 'votes': 1 }, { 'hash': 'dddddddddddd', 'label': 'Delta', 'selected': False, 'votes': 0 }, ], 'api': { 'index': self.poll.get_api_url(), 'votes': self.poll.get_votes_api_url(), }, 'url': { 'poster': self.user.get_absolute_url(), }, }) # validate state change poll = Poll.objects.get(pk=self.poll.pk) self.assertEqual(poll.votes, 4) self.assertEqual(poll.choices, [ { 'hash': 'aaaaaaaaaaaa', 'label': 'Alpha', 'votes': 2 }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Beta', 'votes': 1 }, { 'hash': 'gggggggggggg', 'label': 'Gamma', 'votes': 1 }, { 'hash': 'dddddddddddd', 'label': 'Delta', 'votes': 0 }, ]) self.assertEqual(poll.pollvote_set.count(), 4) # validate poll allows for revote response = self.post(self.api_link, data=['aaaaaaaaaaaa']) self.assertEqual(response.status_code, 200)
def test_moderate_user_poll(self): """api edits all poll choices out in other users poll, even if its over""" self.override_acl({'can_edit_polls': 2, 'poll_edit_time': 5}) self.poll.poster = None self.poll.posted_on = timezone.now() - timedelta(days=15) self.poll.length = 5 self.poll.save() response = self.put(self.api_link, data={ 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'is_public': True, 'choices': [ { 'label': '\nRed ', }, { 'label': 'Green', }, { 'label': 'Blue', }, ], }) self.assertEqual(response.status_code, 200) poll = Poll.objects.all()[0] add_acl(self.user, poll) expected_choices = [] for choice in poll.choices: self.assertIn(choice['label'], ["Red", "Green", "Blue"]) expected_choices.append(choice.copy()) expected_choices[-1]['selected'] = False self.assertEqual( response.json(), { 'id': poll.id, 'poster_name': self.user.username, 'posted_on': serialize_datetime(poll.posted_on), 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'votes': 0, 'is_public': False, 'acl': poll.acl, 'choices': expected_choices, 'api': { 'index': poll.get_api_url(), 'votes': poll.get_votes_api_url(), }, 'url': { 'poster': None, }, }) # votes were removed self.assertEqual(self.poll.pollvote_set.count(), 0)
def test_poll_some_choices_edited(self): """api edits some poll choices""" response = self.put(self.api_link, data={ 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'is_public': True, 'choices': [ { 'hash': 'aaaaaaaaaaaa', 'label': '\nFirst ', 'votes': 5555, }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Second', 'votes': 5555, }, { 'hash': 'dsadsadsa788', 'label': 'New Option', 'votes': 5555, }, ], }) self.assertEqual(response.status_code, 200) poll = Poll.objects.all()[0] add_acl(self.user, poll) self.assertEqual( response.json(), { 'id': poll.id, 'poster_name': self.user.username, 'posted_on': serialize_datetime(poll.posted_on), 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'votes': 1, 'is_public': False, 'acl': poll.acl, 'choices': [ { 'hash': 'aaaaaaaaaaaa', 'label': 'First', 'votes': 1, 'selected': False, }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Second', 'votes': 0, 'selected': False, }, { 'hash': poll.choices[2]['hash'], 'label': 'New Option', 'votes': 0, 'selected': False, }, ], 'api': { 'index': poll.get_api_url(), 'votes': poll.get_votes_api_url(), }, 'url': { 'poster': self.user.get_absolute_url(), }, }) # no votes were removed self.assertEqual(self.poll.pollvote_set.count(), 1)
def test_poll_created(self): """api creates public poll if provided with valid data""" response = self.post( self.api_link, data={ 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'is_public': True, 'choices': [ { 'label': '\nRed ', }, { 'label': 'Green', }, { 'label': 'Blue', }, ], } ) self.assertEqual(response.status_code, 200) self.maxDiff = None poll = Poll.objects.all()[0] add_acl(self.user, poll) expected_choices = [] for choice in poll.choices: expected_choices.append(choice.copy()) expected_choices[-1]['selected'] = False self.assertEqual(response.json(), { 'id': poll.id, 'poster_name': self.user.username, 'posted_on': serialize_datetime(poll.posted_on), 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'votes': 0, 'is_public': True, 'acl': poll.acl, 'choices': expected_choices, 'api': { 'index': poll.get_api_url(), 'votes': poll.get_votes_api_url(), }, 'url': { 'poster': self.user.get_absolute_url(), }, }) self.assertEqual(len(poll.choices), 3) self.assertEqual(len(set([c['hash'] for c in poll.choices])), 3) self.assertEqual([c['label'] for c in poll.choices], ['Red', 'Green', 'Blue']) thread = Thread.objects.get(pk=self.thread.pk) self.assertTrue(thread.has_poll) self.assertEqual(thread.poll, poll)
def test_poll_current_choices_edited(self): """api edits current poll choices""" response = self.put(self.api_link, data={ 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'is_public': True, 'choices': [ { 'hash': 'aaaaaaaaaaaa', 'label': '\nFirst ', 'votes': 5555, }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Second', 'votes': 5555, }, { 'hash': 'gggggggggggg', 'label': 'Third', 'votes': 5555, }, { 'hash': 'dddddddddddd', 'label': 'Fourth', 'votes': 5555, }, ], }) self.assertEqual(response.status_code, 200) poll = Poll.objects.all()[0] self.assertEqual( response.json(), { 'id': poll.id, 'poster': { 'id': self.user.id, 'username': self.user.username, 'slug': self.user.slug, }, 'posted_on': serialize_datetime(poll.posted_on), 'length': 40, 'question': "Select two best colors", 'allowed_choices': 2, 'allow_revotes': True, 'votes': 4, 'is_public': False, 'choices': [ { 'hash': 'aaaaaaaaaaaa', 'label': 'First', 'votes': 1, 'selected': False, }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Second', 'votes': 0, 'selected': False, }, { 'hash': 'gggggggggggg', 'label': 'Third', 'votes': 2, 'selected': True, }, { 'hash': 'dddddddddddd', 'label': 'Fourth', 'votes': 1, 'selected': True, }, ], }) # no votes were removed self.assertEqual(self.poll.pollvote_set.count(), 4)
def test_fresh_vote(self): """api handles first vote in poll""" self.delete_user_votes() response = self.post(self.api_link, data=['aaaaaaaaaaaa', 'bbbbbbbbbbbb']) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), { 'id': self.poll.id, 'poster': { 'id': self.user.id, 'username': self.user.username, 'slug': self.user.slug, }, 'posted_on': serialize_datetime(self.poll.posted_on), 'length': 0, 'question': "Lorem ipsum dolor met?", 'allowed_choices': 2, 'allow_revotes': False, 'votes': 4, 'is_public': False, 'choices': [ { 'hash': 'aaaaaaaaaaaa', 'label': 'Alpha', 'selected': True, 'votes': 2 }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Beta', 'selected': True, 'votes': 1 }, { 'hash': 'gggggggggggg', 'label': 'Gamma', 'selected': False, 'votes': 1 }, { 'hash': 'dddddddddddd', 'label': 'Delta', 'selected': False, 'votes': 0 }, ], }) # validate state change poll = Poll.objects.get(pk=self.poll.pk) self.assertEqual(poll.votes, 4) self.assertEqual(poll.choices, [ { 'hash': 'aaaaaaaaaaaa', 'label': 'Alpha', 'votes': 2 }, { 'hash': 'bbbbbbbbbbbb', 'label': 'Beta', 'votes': 1 }, { 'hash': 'gggggggggggg', 'label': 'Gamma', 'votes': 1 }, { 'hash': 'dddddddddddd', 'label': 'Delta', 'votes': 0 }, ]) self.assertEqual(poll.pollvote_set.count(), 4) # validate poll disallows for revote response = self.post(self.api_link, data=['aaaaaaaaaaaa']) self.assertEqual(response.status_code, 403) self.assertEqual(response.json(), { 'detail': "You have already voted in this poll.", })