Skip to content

Commit

Permalink
#280 Add inforequest test_detail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Jan 4, 2022
1 parent 63976ae commit 937665e
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 41 deletions.
15 changes: 15 additions & 0 deletions chcemvediet/apps/inforequests/tests/render_query_pattern.py
Original file line number Diff line number Diff line change
@@ -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',
]
147 changes: 123 additions & 24 deletions chcemvediet/apps/inforequests/tests/test_views/test_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -21,43 +25,54 @@ 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')

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)
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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))
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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))
3 changes: 2 additions & 1 deletion chcemvediet/apps/inforequests/tests/test_views/test_mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions chcemvediet/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 937665e

Please sign in to comment.