diff --git a/chcemvediet/apps/inforequests/tests/render_query_pattern.py b/chcemvediet/apps/inforequests/tests/render_query_pattern.py new file mode 100644 index 00000000..9e3a4cf0 --- /dev/null +++ b/chcemvediet/apps/inforequests/tests/render_query_pattern.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_views/test_detail.py b/chcemvediet/apps/inforequests/tests/test_views/test_detail.py index bf7ccf4f..8b0ec44e 100644 --- a/chcemvediet/apps/inforequests/tests/test_views/test_detail.py +++ b/chcemvediet/apps/inforequests/tests/test_views/test_detail.py @@ -2,10 +2,14 @@ # -*- 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_pattern class DetailViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase): @@ -21,12 +25,12 @@ def assertTemplateUsedCount(self, response, template_name, 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,))) + 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,))) + 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') @@ -34,30 +38,41 @@ 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_gets_inforequest_detail(self): + 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,))) + 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,))) + 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,))) + 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,))) + 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,))) + 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) @@ -66,8 +81,8 @@ def test_inforequest_with_single_branch(self): 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(self.patterns_single_column): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + with self.assertQueriesDuringRender(render_query_pattern.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( @@ -78,7 +93,7 @@ def test_inforequest_with_multiple_branches(self): [u'confirmation', (u'advancement', [u'refusal'])]), ) self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) # Total 12 actions in 5 branches. # -- Main Branch: @@ -111,8 +126,8 @@ def test_inforequest_with_multiple_branches_related_models_are_prefetched_before [u'confirmation', (u'advancement', [u'refusal'])]), ) self._login_user() - with self.assertQueriesDuringRender(self.patterns_single_column): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + with self.assertQueriesDuringRender(render_query_pattern.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() @@ -121,7 +136,7 @@ def test_inforequest_with_undecided_email(self): self._create_inforequest_email(inforequest=inforequest) self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + 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') @@ -135,13 +150,13 @@ def test_inforequest_with_undecided_email_related_models_are_prefetched_before_r self._create_inforequest_email(inforequest=inforequest) self._login_user() - with self.assertQueriesDuringRender(self.patterns_single_column): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + with self.assertQueriesDuringRender(render_query_pattern.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,))) + 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') @@ -151,8 +166,8 @@ def test_inforequest_without_undecided_email(self): def test_inforequest_without_undecided_email_related_models_are_prefetched_before_render(self): inforequest, _, _ = self._create_inforequest_scenario() self._login_user() - with self.assertQueriesDuringRender(self.patterns_single_column): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + with self.assertQueriesDuringRender(render_query_pattern.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)) @@ -161,7 +176,7 @@ def test_closed_inforequest_with_undecided_email(self): self._create_inforequest_email(inforequest=inforequest) self._login_user() - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + 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') @@ -170,7 +185,7 @@ def test_closed_inforequest_with_undecided_email(self): 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,))) + 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') @@ -183,5 +198,89 @@ def test_closed_inforequest_related_models_are_prefetched_before_render(self): self._create_inforequest_email(inforequest=inforequest) self._login_user() - with self.assertQueriesDuringRender(self.patterns_single_column): - response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,))) + with self.assertQueriesDuringRender(render_query_pattern.base): + response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk))) + + def test_inforequest_with_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'))) + ) + 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_can_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_can_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_can_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_can_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_can_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 209456a9..ba308841 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_pattern from .. import InforequestsTestCaseMixin from ...models import Action @@ -133,7 +134,7 @@ def pre_mock_render(request, template, context): """), ] * 3 patterns = list( - self.patterns_single_column + + render_query_pattern.base + patterns_pending_inforequests + patterns_successful_inforequests + patterns_unsuccessful_inforequests diff --git a/chcemvediet/tests/__init__.py b/chcemvediet/tests/__init__.py index 29bc69f4..1c399e14 100644 --- a/chcemvediet/tests/__init__.py +++ b/chcemvediet/tests/__init__.py @@ -15,7 +15,6 @@ from poleno.attachments.models import Attachment from poleno.mail.models import Message, Recipient from poleno.utils.date import local_today, utc_now -from poleno.utils.misc import squeeze from ..apps.geounits.models import Region, District, Municipality, Neighbourhood from ..apps.inforequests.models import Branch, Inforequest, InforequestEmail, InforequestDraft, Action @@ -49,21 +48,6 @@ def run_tests(self, *args, **kwargs): class ChcemvedietTestCaseMixin(TestCase): - def __init__(self, methodName): - super(ChcemvedietTestCaseMixin, self).__init__(methodName) - self.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', - ] - def _pre_setup(self): super(ChcemvedietTestCaseMixin, self)._pre_setup() self.counter = itertools.count()