diff --git a/conntrackt/tests/helpers.py b/conntrackt/tests/helpers.py --- a/conntrackt/tests/helpers.py +++ b/conntrackt/tests/helpers.py @@ -184,3 +184,55 @@ class FakeMessages(object): """ self.messages.append(message) + + +class RenderTestMixin(object): + """ + Mixin class for testing that response rendering works correctly. + + In order to use this mixin, add it the left side of the class list the test + is inheriting from, and configure it providing the following class options: + + view_class - Class used for the CBV that will be tested. + view_function - View function that will be tested. + render_test_view_args - Positional arguments to pass to the view. + render_test_view_kwargs - Keyword arguments to pass to the view. + """ + + view_class = None + view_function = None + render_test_view_args = () + render_test_view_kwargs = {} + + def __init__(self, *args, **kwargs): + """ + Initialises the mixin. Takes care of some basic validation of + passed configuration options. + """ + + super(RenderTestMixin, self).__init__(*args, **kwargs) + + if self.view_class is None and self.view_function is None: + raise ValueError("Render test mixin configured improperly - no CBV class or function was supplied via parameters 'view_class' or 'view_function'.") + + if type(self.render_test_view_kwargs) is not dict: + raise ValueError("Render test mixin configured improperly - parameter 'render_test_view_kwargs' must be a dictionary.") + + if type(self.render_test_view_args) is not tuple: + raise ValueError("Render test mixin configured improperly - parameter 'render_test_view_args' must be a tuple.") + + def test_no_render_exception_thrown_on_GET(self): + + # Get the view. + if self.view_class is not None: + view = self.view_class.as_view() + elif self.view_function is not None: + view = self.view_function + + # Grab the response. + args = self.render_test_view_args + kwargs = self.render_test_view_kwargs + response = generate_get_response(view, *args, **kwargs) + + # Should not raise. + response.render() diff --git a/conntrackt/tests/test_views.py b/conntrackt/tests/test_views.py --- a/conntrackt/tests/test_views.py +++ b/conntrackt/tests/test_views.py @@ -49,12 +49,12 @@ from conntrackt.views import Communicati # Test imports. from .forms import FormWithWidgetCSSClassFormMixin, FormWithPlaceholderFormMixin -from .helpers import PermissionTestMixin, create_get_request, generate_get_response, FakeMessages +from .helpers import PermissionTestMixin, RenderTestMixin, create_get_request, generate_get_response, FakeMessages from .views import RedirectToNextMixinView from .factories import setup_test_data -class IndexViewTest(PermissionTestMixin, TestCase): +class IndexViewTest(RenderTestMixin, PermissionTestMixin, TestCase): sufficient_permissions = ("view",) view_class = IndexView @@ -130,10 +130,11 @@ class IndexViewTest(PermissionTestMixin, self.assertQuerysetEqual(response.context_data["locations"], ["", ""]) -class ProjectViewTest(PermissionTestMixin, TestCase): +class ProjectViewTest(RenderTestMixin, PermissionTestMixin, TestCase): sufficient_permissions = ("view",) permission_test_view_kwargs = {"pk": "1"} + render_test_view_kwargs = {"pk": "1"} view_class = ProjectView def setUp(self): @@ -183,11 +184,12 @@ class ProjectViewTest(PermissionTestMixi self.assertIn("communications", response.context_data.keys()) -class EntityViewTest(PermissionTestMixin, TestCase): +class EntityViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = EntityView sufficient_permissions = ("view",) permission_test_view_kwargs = {"pk": "1"} + render_test_view_kwargs = {"pk": "1"} def setUp(self): """ @@ -233,6 +235,7 @@ class EntityIptablesTest(PermissionTestM view_function = staticmethod(entity_iptables) sufficient_permissions = ("view",) permission_test_view_kwargs = {"pk": "1"} + render_test_view_kwargs = {"pk": "1"} def setUp(self): """ @@ -302,6 +305,7 @@ class ProjectIptablesTest(PermissionTest view_function = staticmethod(project_iptables) sufficient_permissions = ("view",) permission_test_view_kwargs = {"project_id": 1} + render_test_view_kwargs = {"project_id": 1} def setUp(self): """ @@ -430,17 +434,18 @@ class ProjectIptablesTest(PermissionTest zipped_iptables.close() -class ProjectCreateViewTest(PermissionTestMixin, TestCase): +class ProjectCreateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = ProjectCreateView sufficient_permissions = ("add_project",) -class ProjectUpdateViewTest(PermissionTestMixin, TestCase): +class ProjectUpdateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = ProjectUpdateView sufficient_permissions = ("change_project",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -465,11 +470,12 @@ class ProjectUpdateViewTest(PermissionTe self.assertEqual(response.context_data["headline"], "Update project Test Project 1") -class ProjectDeleteViewTest(PermissionTestMixin, TestCase): +class ProjectDeleteViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = ProjectDeleteView sufficient_permissions = ("delete_project",) permission_test_view_kwargs = {"pk": "1"} + render_test_view_kwargs = {"pk": "1"} def setUp(self): """ @@ -516,17 +522,18 @@ class ProjectDeleteViewTest(PermissionTe self.assertIn("Project Test Project 1 has been removed.", request._messages.messages) -class LocationCreateViewTest(PermissionTestMixin, TestCase): +class LocationCreateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = LocationCreateView sufficient_permissions = ("add_location",) -class LocationUpdateViewTest(PermissionTestMixin, TestCase): +class LocationUpdateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = LocationUpdateView sufficient_permissions = ("change_location",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -551,11 +558,12 @@ class LocationUpdateViewTest(PermissionT self.assertEqual(response.context_data["headline"], "Update location Test Location 1") -class LocationDeleteViewTest(PermissionTestMixin, TestCase): +class LocationDeleteViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = LocationDeleteView sufficient_permissions = ("delete_location",) permission_test_view_kwargs = {"pk": "1"} + render_test_view_kwargs = {"pk": "1"} def setUp(self): """ @@ -603,7 +611,7 @@ class LocationDeleteViewTest(PermissionT self.assertIn("Location Test Location 1 has been removed.", request._messages.messages) -class EntityCreateViewTest(PermissionTestMixin, TestCase): +class EntityCreateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = EntityCreateView sufficient_permissions = ("add_entity",) @@ -680,11 +688,12 @@ class EntityCreateViewTest(PermissionTes self.assertDictContainsSubset({"location": "1"}, initial) -class EntityDeleteViewTest(PermissionTestMixin, TestCase): +class EntityDeleteViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = EntityDeleteView sufficient_permissions = ("delete_entity",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -751,11 +760,12 @@ class EntityDeleteViewTest(PermissionTes self.assertEqual(response["Location"], reverse("project", args=(1,))) -class EntityUpdateViewTest(PermissionTestMixin, TestCase): +class EntityUpdateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = EntityUpdateView sufficient_permissions = ("change_entity",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -780,7 +790,7 @@ class EntityUpdateViewTest(PermissionTes self.assertEqual(response.context_data["headline"], "Update entity Test Entity 1") -class InterfaceCreateViewTest(PermissionTestMixin, TestCase): +class InterfaceCreateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = InterfaceCreateView sufficient_permissions = ("add_interface",) @@ -850,11 +860,12 @@ class InterfaceCreateViewTest(Permission self.assertEqual(response.status_code, 302) -class InterfaceUpdateViewTest(PermissionTestMixin, TestCase): +class InterfaceUpdateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = InterfaceUpdateView sufficient_permissions = ("change_interface",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -928,11 +939,12 @@ class InterfaceUpdateViewTest(Permission self.assertEqual(response.status_code, 302) -class InterfaceDeleteViewTest(PermissionTestMixin, TestCase): +class InterfaceDeleteViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = InterfaceDeleteView sufficient_permissions = ("delete_interface",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -999,7 +1011,7 @@ class InterfaceDeleteViewTest(Permission self.assertEqual(response["Location"], reverse("entity", args=(1,))) -class CommunicationCreateViewTest(PermissionTestMixin, TestCase): +class CommunicationCreateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = CommunicationCreateView sufficient_permissions = ("add_communication",) @@ -1221,11 +1233,12 @@ class CommunicationCreateViewTest(Permis self.assertEqual(response.status_code, 302) -class CommunicationUpdateViewTest(PermissionTestMixin, TestCase): +class CommunicationUpdateViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = CommunicationUpdateView sufficient_permissions = ("change_communication",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -1328,11 +1341,12 @@ class CommunicationUpdateViewTest(Permis self.assertEqual(response.status_code, 302) -class CommunicationDeleteViewTest(PermissionTestMixin, TestCase): +class CommunicationDeleteViewTest(RenderTestMixin, PermissionTestMixin, TestCase): view_class = CommunicationDeleteView sufficient_permissions = ("delete_communication",) permission_test_view_kwargs = {"pk": 1} + render_test_view_kwargs = {"pk": 1} def setUp(self): """ @@ -1445,6 +1459,7 @@ class ProjectDiagramTest(PermissionTestM view_function = staticmethod(project_diagram) sufficient_permissions = ("view",) permission_test_view_kwargs = {"pk": "1"} + render_test_view_kwargs = {"pk": "1"} def setUp(self): """ @@ -1540,7 +1555,7 @@ class RedirectToNextMixinTest(TestCase): self.assertEqual("/next", view.get_success_url()) -class SearchViewTest(PermissionTestMixin, TestCase): +class SearchViewTest(RenderTestMixin, PermissionTestMixin, TestCase): sufficient_permissions = ("view",) view_class = SearchView diff --git a/requirements/base.in b/requirements/base.in --- a/requirements/base.in +++ b/requirements/base.in @@ -21,7 +21,7 @@ django-braces~=1.12.0 # Convenience tools for controlling rendering of forms while embracing DRY. -django-crispy-forms~=1.7.0 +django-crispy-forms~=1.6.0 # Web framework used by application. django~=1.5.0 diff --git a/requirements/development.txt b/requirements/development.txt --- a/requirements/development.txt +++ b/requirements/development.txt @@ -10,7 +10,7 @@ certifi==2017.11.5 # via requests chardet==3.0.4 # via requests coverage==4.4.2 django-braces==1.12.0 -django-crispy-forms==1.7.0 +django-crispy-forms==1.6.1 django-discover-runner==0.3 django==1.5.12 docutils==0.14 # via sphinx diff --git a/requirements/test.txt b/requirements/test.txt --- a/requirements/test.txt +++ b/requirements/test.txt @@ -10,7 +10,7 @@ certifi==2017.11.5 # via requests chardet==3.0.4 # via requests coverage==4.4.2 django-braces==1.12.0 -django-crispy-forms==1.7.0 +django-crispy-forms==1.6.1 django-discover-runner==0.3 django==1.5.12 docutils==0.14 # via sphinx diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ from setuptools import setup, find_packa README = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read() INSTALL_REQUIREMENTS = [ "django-braces~=1.12.0", - "django-crispy-forms~=1.7.0", + "django-crispy-forms~=1.6.0", "django~=1.5.0", "palette~=0.2.0", "pydot~=1.2.0",