From f016cc317463c0b68507ac0688434c9c28ac2474 Mon Sep 17 00:00:00 2001 From: viliambalaz Date: Mon, 23 Aug 2021 20:13:34 +0200 Subject: [PATCH] #280 Fix inforequests view mine --- .../apps/inforequests/tests/__init__.py | 2 +- .../tests/test_views/skip_test_mine.py | 70 ---------------- .../tests/test_views/test_mine.py | 84 +++++++++++++++++++ .../apps/inforequests/views/__init__.py | 4 + 4 files changed, 89 insertions(+), 71 deletions(-) delete mode 100644 chcemvediet/apps/inforequests/tests/test_views/skip_test_mine.py create mode 100644 chcemvediet/apps/inforequests/tests/test_views/test_mine.py diff --git a/chcemvediet/apps/inforequests/tests/__init__.py b/chcemvediet/apps/inforequests/tests/__init__.py index faabf39d7..f12989ed5 100644 --- a/chcemvediet/apps/inforequests/tests/__init__.py +++ b/chcemvediet/apps/inforequests/tests/__init__.py @@ -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 diff --git a/chcemvediet/apps/inforequests/tests/test_views/skip_test_mine.py b/chcemvediet/apps/inforequests/tests/test_views/skip_test_mine.py deleted file mode 100644 index a84160615..000000000 --- a/chcemvediet/apps/inforequests/tests/test_views/skip_test_mine.py +++ /dev/null @@ -1,70 +0,0 @@ -# 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 - - -class MineViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase): - u""" - Tests ``mine()`` view registered as "inforequests:mine". - """ - - 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() - response = self.client.get(reverse(u'inforequests:mine')) - self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, u'inforequests/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)] - inforequests1 = [self._create_inforequest(applicant=self.user1) for i in range(4)] - inforequests2 = [self._create_inforequest(applicant=self.user2) for i in range(5)] - closed1 = [self._create_inforequest(applicant=self.user1, closed=True) for i in range(3)] - closed2 = [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'inforequests'], inforequests1) - self.assertItemsEqual(response.context[u'drafts'], drafts1) - self.assertItemsEqual(response.context[u'closed_inforequests'], closed1) - - def test_with_user_with_no_his_inforequests_nor_drafts(self): - drafts2 = [self._create_inforequest_draft(applicant=self.user2) for i in range(3)] - inforequests2 = [self._create_inforequest(applicant=self.user2) for i in range(5)] - closed2 = [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'inforequests'], []) - self.assertItemsEqual(response.context[u'drafts'], []) - self.assertItemsEqual(response.context[u'closed_inforequests'], []) - - def test_related_models_are_prefetched_before_render(self): - drafts1 = [self._create_inforequest_draft(applicant=self.user1) for i in range(5)] - inforequests1 = [self._create_inforequest(applicant=self.user1) for i in range(4)] - closed1 = [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, temaplate, context): - list(context[u'inforequests']) - list(context[u'drafts']) - list(context[u'closed_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) diff --git a/chcemvediet/apps/inforequests/tests/test_views/test_mine.py b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py new file mode 100644 index 000000000..fd43dc55a --- /dev/null +++ b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py @@ -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) diff --git a/chcemvediet/apps/inforequests/views/__init__.py b/chcemvediet/apps/inforequests/views/__init__.py index 299bd1d6a..5b0b3dca6 100644 --- a/chcemvediet/apps/inforequests/views/__init__.py +++ b/chcemvediet/apps/inforequests/views/__init__.py @@ -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