Skip to content

Commit

Permalink
#280 Fix inforequests view mine
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Aug 23, 2021
1 parent f6a3973 commit f016cc3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 71 deletions.
2 changes: 1 addition & 1 deletion chcemvediet/apps/inforequests/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class InforequestsTestCaseMixin(ChcemvedietTestCaseMixin):

@contextlib.contextmanager
def assertQueriesDuringRender(self, *patterns, **kwargs):
def assertQueriesDuringRender(self, patterns, **kwargs):
u"""
Use to assert that views prefetch all related models before rendering their templates.
Views should prefetch their related models to prevent templates from making database
Expand Down
70 changes: 0 additions & 70 deletions chcemvediet/apps/inforequests/tests/test_views/skip_test_mine.py

This file was deleted.

84 changes: 84 additions & 0 deletions chcemvediet/apps/inforequests/tests/test_views/test_mine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# vim: expandtab
# -*- coding: utf-8 -*-
from django.test import TestCase

from poleno.utils.test import ViewTestCaseMixin
from poleno.utils.urls import reverse

from .. import InforequestsTestCaseMixin
from ...models import Action


class MineViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase):
u"""
Tests ``mine()`` view registered as "inforequests:mine".
"""

def _create_successful_inforequest(self, applicant):
inforequest, _, _ = self._create_inforequest_scenario(applicant, {u'closed': True},
(u'disclosure', dict(disclosure_level=Action.DISCLOSURE_LEVELS.PARTIAL)))
return inforequest


def test_allowed_http_methods(self):
allowed = [u'HEAD', u'GET']
self.assert_allowed_http_methods(allowed, reverse(u'inforequests:mine'))

def test_anonymous_user_is_redirected(self):
self.assert_anonymous_user_is_redirected(reverse(u'inforequests:mine'))

def test_authenticated_user_gets_inforequest_mine(self):
self._login_user(self.user1)
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, u'inforequests/mine/mine.html')

def test_user_gets_only_his_inforequests_and_drafts(self):
drafts1 = [self._create_inforequest_draft(applicant=self.user1) for i in range(5)]
drafts2 = [self._create_inforequest_draft(applicant=self.user2) for i in range(3)]
pending_inforequests1 = [self._create_inforequest(applicant=self.user1) for i in range(4)]
pending_inforequests2 = [self._create_inforequest(applicant=self.user2) for i in range(5)]
successful_inforequests1 = [self._create_successful_inforequest(applicant=self.user1) for i in range(3)]
successful_inforequests2 = [self._create_successful_inforequest(applicant=self.user2) for i in range(3)]
unsuccessful_inforequests1 = [self._create_inforequest(applicant=self.user1, closed=True) for i in range(3)]
unsuccessful_inforequests2 = [self._create_inforequest(applicant=self.user2, closed=True) for i in range(3)]

self._login_user(self.user1)
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
self.assertItemsEqual(response.context[u'pending_inforequests'], pending_inforequests1)
self.assertItemsEqual(response.context[u'drafts'], drafts1)
self.assertItemsEqual(response.context[u'successful_inforequests'], successful_inforequests1)
self.assertItemsEqual(response.context[u'unsuccessful_inforequests'], unsuccessful_inforequests1)

def test_with_user_with_no_his_inforequests_nor_drafts(self):
drafts2 = [self._create_inforequest_draft(applicant=self.user2) for i in range(3)]
pending_inforequests2 = [self._create_inforequest(applicant=self.user2) for i in range(5)]
successful_inforequests2 = [self._create_successful_inforequest(applicant=self.user2) for i in range(3)]
unsuccessful_inforequests2 = [self._create_inforequest(applicant=self.user2, closed=True) for i in range(3)]

self._login_user(self.user1)
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
self.assertItemsEqual(response.context[u'pending_inforequests'], [])
self.assertItemsEqual(response.context[u'drafts'], [])
self.assertItemsEqual(response.context[u'successful_inforequests'], [])
self.assertItemsEqual(response.context[u'unsuccessful_inforequests'], [])

def test_related_models_are_prefetched_before_render(self):
drafts1 = [self._create_inforequest_draft(applicant=self.user1) for i in range(5)]
pending_inforequests1 = [self._create_inforequest(applicant=self.user1) for i in range(4)]
successful_inforequests1 = [self._create_successful_inforequest(applicant=self.user1) for i in range(3)]
unsuccessful_inforequests1 = [self._create_inforequest(applicant=self.user1, closed=True) for i in range(3)]

# Force view querysets to evaluate before calling render
def pre_mock_render(request, template, context):
list(context[u'pending_inforequests'])
list(context[u'drafts'])
list(context[u'successful_inforequests'])
list(context[u'unsuccessful_inforequests'])

self._login_user(self.user1)
with self.assertQueriesDuringRender([], pre_mock_render=pre_mock_render):
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
4 changes: 4 additions & 0 deletions chcemvediet/apps/inforequests/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# vim: expandtab
# -*- coding: utf-8 -*-

from django.shortcuts import render # To let tests mock this function.

from poleno.utils.template import render_to_string # To let tests mock this function.

from .inforequest import inforequest_index
from .inforequest import inforequest_mine
from .inforequest import inforequest_create
Expand Down

0 comments on commit f016cc3

Please sign in to comment.