From ce9f9f2721a6ed3ff00d0fe3c486adf47358ba8b Mon Sep 17 00:00:00 2001 From: Viliam Balaz Date: Wed, 6 Apr 2022 17:56:27 +0200 Subject: [PATCH] #280 Fix test_detail view (#423) * #280 Fix test_detail view * #280 Add inforequest test_detail tests * #280 Refactroring tests names --- .../apps/inforequests/tests/__init__.py | 4 +- .../tests/render_query_patterns.py | 15 + .../tests/test_models/test_inforequest.py | 8 +- .../tests/test_views/skip_test_detail.py | 192 ------------ .../tests/test_views/test_detail.py | 286 ++++++++++++++++++ .../tests/test_views/test_mine.py | 15 +- 6 files changed, 309 insertions(+), 211 deletions(-) create mode 100644 chcemvediet/apps/inforequests/tests/render_query_patterns.py delete mode 100644 chcemvediet/apps/inforequests/tests/test_views/skip_test_detail.py create mode 100644 chcemvediet/apps/inforequests/tests/test_views/test_detail.py diff --git a/chcemvediet/apps/inforequests/tests/__init__.py b/chcemvediet/apps/inforequests/tests/__init__.py index e7cea54f5..f837805a3 100644 --- a/chcemvediet/apps/inforequests/tests/__init__.py +++ b/chcemvediet/apps/inforequests/tests/__init__.py @@ -166,8 +166,8 @@ def _create_inforequest_scenario(self, *args): ``_create_message()`` and arguments for ``_create_recipient()``. """ args = list(args) - applicant = args.pop(0) if args and isinstance(args[0], User) else self.user1 - obligee = args.pop(0) if args and isinstance(args[0], Obligee) else self.obligee1 + applicant = args.pop(0) if args and isinstance(args[0], User) else self.user + obligee = args.pop(0) if args and isinstance(args[0], Obligee) else self.obligee extra = args.pop(0) if args and isinstance(args[0], dict) else {} inforequest = Inforequest.objects.create(applicant=applicant, **extra) branch, actions = self._create_inforequest_scenario__branch(inforequest, obligee, None, u'request', args) diff --git a/chcemvediet/apps/inforequests/tests/render_query_patterns.py b/chcemvediet/apps/inforequests/tests/render_query_patterns.py new file mode 100644 index 000000000..6df4b9db2 --- /dev/null +++ b/chcemvediet/apps/inforequests/tests/render_query_patterns.py @@ -0,0 +1,15 @@ +from poleno.utils.misc import squeeze + + +base = [ + u'FROM "accounts_profile" WHERE "accounts_profile"."user_id" = %s LIMIT 21', + squeeze(u""" + SELECT COUNT\(\*\) FROM "mail_message" + INNER JOIN "inforequests_inforequestemail" ON \( "mail_message"."id" = "inforequests_inforequestemail"."email_id" \) + INNER JOIN "inforequests_inforequest" ON \( "inforequests_inforequestemail"."inforequest_id" = "inforequests_inforequest"."id" \) + WHERE \("inforequests_inforequestemail"."type" = %s + AND "inforequests_inforequest"."applicant_id" = %s + AND "inforequests_inforequest"."closed" = %s\) + """), + u'FROM "invitations_invitationsupply" WHERE "invitations_invitationsupply"."user_id" = %s LIMIT 21', +] diff --git a/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py b/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py index be3c80865..e8c9b4d0f 100644 --- a/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py +++ b/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py @@ -250,7 +250,7 @@ def test_slug_property_with_empty_subject(self): self.assertEqual(inforequest.slug, ugettext(u'inforequests:Inforequest:fallback_slug')) def test_prefetch_branches_staticmethod(self): - inforequest, branch1, actions = self._create_inforequest_scenario(u'advancement') + inforequest, branch1, actions = self._create_inforequest_scenario(self.user1, u'advancement') _, (_, ((branch2, _),)) = actions # Without arguments @@ -288,7 +288,7 @@ def test_branches_property(self): self.assertEqual(inforequest.branches, [branch1, branch2]) def test_prefetch_main_branch_staticmethod(self): - inforequest, branch, _ = self._create_inforequest_scenario(u'advancement') + inforequest, branch, _ = self._create_inforequest_scenario(self.user1, u'advancement') # Without arguments with self.assertNumQueries(2): @@ -372,7 +372,7 @@ def test_undecided_emails_set_property_is_not_ordered(self): self.assertFalse(inforequest.undecided_emails_set.all().ordered) def test_prefetch_undecided_emails_staticmethod(self): - inforequest, _, _ = self._create_inforequest_scenario() + inforequest, _, _ = self._create_inforequest_scenario(self.user1) _, rel1 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNKNOWN) _, rel2 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNRELATED) _, rel3 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNDECIDED) @@ -661,7 +661,7 @@ def test_oldest_undecided_email_property_with_no_undecided_emails(self): self.assertIsNone(inforequest.oldest_undecided_email) def test_prefetch_newest_undecided_email_staticmethod(self): - inforequest, _, _ = self._create_inforequest_scenario() + inforequest, _, _ = self._create_inforequest_scenario(self.user1) _, rel1 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNKNOWN) _, rel2 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNRELATED) _, rel3 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNDECIDED) diff --git a/chcemvediet/apps/inforequests/tests/test_views/skip_test_detail.py b/chcemvediet/apps/inforequests/tests/test_views/skip_test_detail.py deleted file mode 100644 index b4934a92a..000000000 --- a/chcemvediet/apps/inforequests/tests/test_views/skip_test_detail.py +++ /dev/null @@ -1,192 +0,0 @@ -# vim: expandtab -# -*- coding: utf-8 -*- -from collections import defaultdict - -from django.test import TestCase - -from poleno.utils.test import ViewTestCaseMixin -from poleno.utils.urls import reverse - -from .. import InforequestsTestCaseMixin - -class DetailViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase): - u""" - Tests ``detail()`` view registered as "inforequests:detail". - """ - - def assertTemplateUsedCount(self, response, template_name, count): - used = sum(1 for t in response.templates if t.name == template_name) - self.assertEqual(used, count) - - - def test_allowed_http_methods(self): - inforequest, _, _ = self._create_inforequest_scenario() - allowed = [u'HEAD', u'GET'] - self.assert_allowed_http_methods(allowed, reverse(u'inforequests:detail', args=(inforequest.pk,))) - - def test_anonymous_user_is_redirected(self): - inforequest, _, _ = self._create_inforequest_scenario() - self.assert_anonymous_user_is_redirected(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - def test_authenticated_user_gets_inforequest_detail(self): - inforequest, _, _ = self._create_inforequest_scenario() - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, u'inforequests/detail.html') - - def test_invalid_inforequest_returns_404_not_found(self): - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(47,))) - self.assertEqual(response.status_code, 404) - - def test_inforequest_owned_by_another_user_returns_404_not_found(self): - inforequest, _, _ = self._create_inforequest_scenario(self.user2) - self._login_user(self.user1) - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - self.assertEqual(response.status_code, 404) - - def test_inforequest_with_single_branch(self): - inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'extension') - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - # Three actions in single branch - self.assertTemplateUsedCount(response, u'inforequests/detail_branch.html', 1) - self.assertTemplateUsedCount(response, u'inforequests/detail_action.html', 3) - - def test_inforequest_with_single_branch_related_models_are_prefetched_before_render(self): - inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'extension') - self._login_user() - with self.assertQueriesDuringRender([]): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - def test_inforequest_with_multiple_branches(self): - inforequest, _, _ = self._create_inforequest_scenario( - u'confirmation', - (u'advancement', - [u'disclosure'], - [u'refusal'], - [u'confirmation', (u'advancement', [u'refusal'])]), - ) - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - # Total 12 actions in 5 branches. - # -- Main Branch: - # -- request - # -- confirmation - # -- advancement - # -- branch: - # -- advanced_request - # -- disclosure - # -- branch: - # -- advanced_request - # -- refusal - # -- branch: - # -- advanced_request - # -- confirmation - # -- advancement: - # -- branch: - # -- advanced_request - # -- refusal - self.assertTemplateUsedCount(response, u'inforequests/detail_branch.html', 5) - self.assertTemplateUsedCount(response, u'inforequests/detail_action.html', 12) - - def test_inforequest_with_multiple_branches_related_models_are_prefetched_before_render(self): - inforequest, _, _ = self._create_inforequest_scenario( - u'confirmation', - (u'advancement', - [u'disclosure'], - [u'refusal'], - [u'confirmation', (u'advancement', [u'refusal'])]), - ) - self._login_user() - with self.assertQueriesDuringRender([]): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - def test_inforequest_with_undecided_email(self): - inforequest, _, _ = self._create_inforequest_scenario() - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - # User can decide - self.assertTemplateUsed(response, u'inforequests/detail_decide.html') - self.assertTemplateUsed(response, u'inforequests/detail_undecided.html') - # User may not act - self.assertTemplateNotUsed(response, u'inforequests/detail_add_smail.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_new_action.html') - - templates = defaultdict(list) - for template in response.templates: - templates[template.name].append(template) - - self.assertTemplateUsedCount(response, u'inforequests/detail_email.html', 3) - - def test_inforequest_with_undecided_email_related_models_are_prefetched_before_render(self): - inforequest, _, _ = self._create_inforequest_scenario() - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - - self._login_user() - with self.assertQueriesDuringRender([]): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - def test_inforequest_without_undecided_email(self): - inforequest, _, _ = self._create_inforequest_scenario() - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - # There is nothing to decide - self.assertTemplateNotUsed(response, u'inforequests/detail_decide.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_undecided.html') - # User may act - self.assertTemplateUsed(response, u'inforequests/detail_add_smail.html') - self.assertTemplateUsed(response, u'inforequests/detail_new_action.html') - - def test_inforequest_without_undecided_email_related_models_are_prefetched_before_render(self): - inforequest, _, _ = self._create_inforequest_scenario() - self._login_user() - with self.assertQueriesDuringRender([]): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - def test_closed_inforequest_with_undecided_email(self): - inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True)) - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - # Inforequest is closed, user may do nothing - self.assertTemplateNotUsed(response, u'inforequests/detail_decide.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_undecided.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_add_smail.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_new_action.html') - - def test_closed_inforequest_without_undecided_email(self): - inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True)) - self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) - - # Inforequest is closed, user may do nothing - self.assertTemplateNotUsed(response, u'inforequests/detail_decide.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_undecided.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_add_smail.html') - self.assertTemplateNotUsed(response, u'inforequests/detail_new_action.html') - - def test_closed_inforequest_related_models_are_prefetched_before_render(self): - inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True)) - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - self._create_inforequest_email(inforequest=inforequest) - - self._login_user() - with self.assertQueriesDuringRender([]): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) diff --git a/chcemvediet/apps/inforequests/tests/test_views/test_detail.py b/chcemvediet/apps/inforequests/tests/test_views/test_detail.py new file mode 100644 index 000000000..a07b17980 --- /dev/null +++ b/chcemvediet/apps/inforequests/tests/test_views/test_detail.py @@ -0,0 +1,286 @@ +# vim: expandtab +# -*- coding: utf-8 -*- +from django.test import TestCase + +from poleno.timewarp import timewarp +from poleno.utils.date import local_datetime_from_local, naive_date +from poleno.utils.templatetags.poleno.utils import url +from poleno.utils.test import ViewTestCaseMixin +from poleno.utils.urls import reverse + +from .. import InforequestsTestCaseMixin +from .. import render_query_patterns + + +class DetailViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase): + u""" + Tests ``detail()`` view registered as "inforequests:detail". + """ + + def assertTemplateUsedCount(self, response, template_name, count): + used = sum(1 for t in response.templates if t.name == template_name) + self.assertEqual(used, count) + + + def test_allowed_http_methods(self): + inforequest, _, _ = self._create_inforequest_scenario() + allowed = [u'HEAD', u'GET'] + self.assert_allowed_http_methods(allowed, reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_owner_gets_inforequest_detail(self): + inforequest, _, _ = self._create_inforequest_scenario({u'published': False}) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, u'inforequests/detail/detail.html') + + def test_invalid_inforequest_returns_404_not_found(self): + response = self.client.get(reverse(u'inforequests:detail', args=(47,))) + self.assertEqual(response.status_code, 404) + + def test_published_inforequest_owned_by_another_user_returns_inforequest_detail(self): + inforequest, _, _ = self._create_inforequest_scenario(self.user, {u'published': True}) + self._login_user(self.user1) + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, u'inforequests/detail/detail.html') + + def test_published_inforequest_returns_inforequest_detail_to_anonymous_user(self): + inforequest, _, _ = self._create_inforequest_scenario({u'published': True}) + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, u'inforequests/detail/detail.html') + + def test_non_published_inforequest_owned_by_another_user_returns_404_not_found(self): + inforequest, _, _ = self._create_inforequest_scenario(self.user, {u'published': False}) + self._login_user(self.user1) + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + self.assertEqual(response.status_code, 404) + + def test_non_published_inforequest_returns_404_not_found_to_anonymous_user(self): + inforequest, _, _ = self._create_inforequest_scenario({u'published': False}) + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + self.assertEqual(response.status_code, 404) + + def test_invalid_inforequest_slug_is_redirected_to_inforequest_detail(self): + inforequest, _, _ = self._create_inforequest_scenario({u'published': True}) + response1 = self.client.get(reverse(u'inforequests:detail', args=(u'invalid', inforequest.pk))) + response2 = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,))) + self.assertRedirects(response1, reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + self.assertRedirects(response2, reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_inforequest_with_single_branch(self): + inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'reversion') + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + # Three actions in single branch + self.assertTemplateUsedCount(response, u'inforequests/detail/branch/main.html', 1) + self.assertTemplateUsedCount(response, u'inforequests/detail/action/main.html', 3) + + def test_inforequest_with_single_branch_related_models_are_prefetched_before_render(self): + inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'reversion') + self._login_user() + with self.assertQueriesDuringRender(render_query_patterns.base): + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_inforequest_with_multiple_branches(self): + inforequest, _, _ = self._create_inforequest_scenario( + u'confirmation', + (u'advancement', + [u'disclosure'], + [u'refusal'], + [u'confirmation', (u'advancement', [u'refusal'])]), + ) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + # Total 12 actions in 5 branches. + # -- Main Branch: + # -- request + # -- confirmation + # -- advancement + # -- branch: + # -- advanced_request + # -- disclosure + # -- branch: + # -- advanced_request + # -- refusal + # -- branch: + # -- advanced_request + # -- confirmation + # -- advancement: + # -- branch: + # -- advanced_request + # -- refusal + self.assertTemplateUsedCount(response, u'inforequests/detail/branch/main.html', 1) + self.assertTemplateUsedCount(response, u'inforequests/detail/branch/sub.html', 4) + self.assertTemplateUsedCount(response, u'inforequests/detail/action/main.html', 12) + + def test_inforequest_with_multiple_branches_related_models_are_prefetched_before_render(self): + inforequest, _, _ = self._create_inforequest_scenario( + u'confirmation', + (u'advancement', + [u'disclosure'], + [u'refusal'], + [u'confirmation', (u'advancement', [u'refusal'])]), + ) + self._login_user() + with self.assertQueriesDuringRender(render_query_patterns.base): + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_inforequest_with_undecided_email(self): + inforequest, _, _ = self._create_inforequest_scenario() + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + # User can decide + self.assertTemplateUsed(response, u'inforequests/detail/texts/add_email.html') + # User may not act + self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_smail.html') + + def test_inforequest_with_undecided_email_related_models_are_prefetched_before_render(self): + inforequest, _, _ = self._create_inforequest_scenario() + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + + self._login_user() + with self.assertQueriesDuringRender(render_query_patterns.base): + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_inforequest_without_undecided_email(self): + inforequest, _, _ = self._create_inforequest_scenario() + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + # There is nothing to decide + self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_email.html') + # User may act + self.assertTemplateUsed(response, u'inforequests/detail/texts/add_smail.html') + + def test_inforequest_without_undecided_email_related_models_are_prefetched_before_render(self): + inforequest, _, _ = self._create_inforequest_scenario() + self._login_user() + with self.assertQueriesDuringRender(render_query_patterns.base): + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_closed_inforequest_with_undecided_email(self): + inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True)) + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + # Inforequest is closed, user may do nothing + self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_email.html') + self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_smail.html') + + def test_closed_inforequest_without_undecided_email(self): + inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True)) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + # Inforequest is closed, user may do nothing + self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_email.html') + self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_smail.html') + + def test_closed_inforequest_related_models_are_prefetched_before_render(self): + inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True)) + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + self._create_inforequest_email(inforequest=inforequest) + + self._login_user() + with self.assertQueriesDuringRender(render_query_patterns.base): + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_inforequest_with_undecided_email_cannot_applicant_snooze(self): + inforequest, _, _ = self._create_inforequest_scenario( + (u'request', dict(legal_date=naive_date(u'2010-10-05'), delivered_date=naive_date(u'2010-10-05'))) + ) + self._create_inforequest_email(inforequest=inforequest) + timewarp.jump(local_datetime_from_local(u'2010-10-16 10:33:00')) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + snooze_url = url(u'inforequests:snooze', action=inforequest.last_action) + self.assertTrue(inforequest.last_action.can_applicant_snooze) + self.assertNotContains(response, u'type="button" action="{}"'.format(snooze_url)) + + def test_inforequest_without_undecided_email_can_applicant_snooze(self): + inforequest, _, _ = self._create_inforequest_scenario( + (u'request', dict(legal_date=naive_date(u'2010-10-05'), delivered_date=naive_date(u'2010-10-05'))) + ) + timewarp.jump(local_datetime_from_local(u'2010-10-16 10:33:00')) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + snooze_url = url(u'inforequests:snooze', action=inforequest.last_action) + self.assertTrue(inforequest.last_action.can_applicant_snooze) + self.assertContains(response, u'type="button" action="{}"'.format(snooze_url)) + + def test_closed_inforequest_without_undecided_email_cannot_applicant_snooze(self): + inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True), + (u'request', dict(legal_date=naive_date(u'2010-10-05'), delivered_date=naive_date(u'2010-10-05'))) + ) + timewarp.jump(local_datetime_from_local(u'2010-10-16 10:33:00')) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + snooze_url = url(u'inforequests:snooze', action=inforequest.last_action) + self.assertTrue(inforequest.last_action.can_applicant_snooze) + self.assertNotContains(response, u'type="button" action="{}"'.format(snooze_url)) + + def test_inforequest_with_undecided_email_applicant_cannot_add_clarification_response(self): + inforequest, _, _ = self._create_inforequest_scenario(u'clarification_request') + self._create_inforequest_email(inforequest=inforequest) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + clarification_response_url = url(u'inforequests:clarification_response', branch=inforequest.main_branch) + self.assertTrue(inforequest.can_add_clarification_response) + self.assertNotContains(response, u'href="{}"'.format(clarification_response_url)) + + def test_inforequest_without_undecided_email_applicant_can_add_clarification_response(self): + inforequest, _, _ = self._create_inforequest_scenario(u'clarification_request') + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + clarification_response_url = url(u'inforequests:clarification_response', branch=inforequest.main_branch) + self.assertTrue(inforequest.can_add_clarification_response) + self.assertContains(response, u'href="{}"'.format(clarification_response_url)) + + def test_closed_inforequest_without_undecided_email_applicant_cannot_add_clarification_response(self): + inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True), u'clarification_request') + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + clarification_response_url = url(u'inforequests:clarification_response', branch=inforequest.main_branch) + self.assertTrue(inforequest.can_add_clarification_response) + self.assertNotContains(response, u'href="{}"'.format(clarification_response_url)) + + def test_inforequest_with_undecided_email_applicant_cannot_add_appeal(self): + inforequest, _, _ = self._create_inforequest_scenario(u'advancement') + self._create_inforequest_email(inforequest=inforequest) + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + appeal_url = url(u'inforequests:appeal', branch=inforequest.main_branch) + self.assertTrue(inforequest.can_add_appeal) + self.assertNotContains(response, u'href="{}"'.format(appeal_url)) + + def test_inforequest_without_undecided_email_applicant_can_add_appeal(self): + inforequest, _, _ = self._create_inforequest_scenario(u'advancement') + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + appeal_url = url(u'inforequests:appeal', branch=inforequest.main_branch) + self.assertTrue(inforequest.can_add_appeal) + self.assertContains(response, u'href="{}"'.format(appeal_url)) + + def test_closed_inforequest_without_undecided_email_applicant_cannot_add_appeal(self): + inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True), u'advancement') + self._login_user() + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + appeal_url = url(u'inforequests:appeal', branch=inforequest.main_branch) + self.assertTrue(inforequest.can_add_appeal) + self.assertNotContains(response, u'href="{}"'.format(appeal_url)) diff --git a/chcemvediet/apps/inforequests/tests/test_views/test_mine.py b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py index 863114f2a..d89a0bacc 100644 --- a/chcemvediet/apps/inforequests/tests/test_views/test_mine.py +++ b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py @@ -6,6 +6,7 @@ from poleno.utils.test import ViewTestCaseMixin from poleno.utils.urls import reverse +from .. import render_query_patterns from .. import InforequestsTestCaseMixin from ...models import Action @@ -85,18 +86,6 @@ def pre_mock_render(request, template, context): list(context[u'unsuccessful_inforequests']) self._login_user(self.user1) - patterns_single_column = [ - u'FROM "accounts_profile" WHERE "accounts_profile"."user_id" = %s LIMIT 21', - squeeze(u""" - SELECT COUNT\(\*\) FROM "mail_message" - INNER JOIN "inforequests_inforequestemail" ON \( "mail_message"."id" = "inforequests_inforequestemail"."email_id" \) - INNER JOIN "inforequests_inforequest" ON \( "inforequests_inforequestemail"."inforequest_id" = "inforequests_inforequest"."id" \) - WHERE \("inforequests_inforequestemail"."type" = %s - AND "inforequests_inforequest"."applicant_id" = %s - AND "inforequests_inforequest"."closed" = %s\) - """), - u'FROM "invitations_invitationsupply" WHERE "invitations_invitationsupply"."user_id" = %s LIMIT 21', - ] # TODO: Optimize N+1 queries in #419 patterns_pending_inforequests = [ squeeze(u""" @@ -145,7 +134,7 @@ def pre_mock_render(request, template, context): """), ] * 3 patterns = list( - patterns_single_column + + render_query_patterns.base + patterns_pending_inforequests + patterns_successful_inforequests + patterns_unsuccessful_inforequests