Files @ 3d702d4d7154
Branch filter:

Location: conntrackt/conntrackt/tests/test_tags.py - annotation

branko
CONNT-25: Updating application and project to use Django 1.9.x:

- Updated the html_link tag implementation to produce safe output, and
added tests for it.
- Updated URL configuration in both the application and test project
according to deprecation warning.
- Updated views that return customized forms to ensure the form_class
passed in to get_form is optional.
- Bumped Django in both setup script and development requirements to
version 1.9.x.
- Updated test project configuration to match the version produced by
Django 1.9.x admin commands.
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
3d702d4d7154
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Branko Majic
#
# This file is part of Django Conntrackt.
#
# Django Conntrackt is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Django Conntrackt is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Django Conntrackt.  If not, see <http://www.gnu.org/licenses/>.
#


# Standard library imports.
import json
from StringIO import StringIO
from zipfile import ZipFile, ZIP_DEFLATED

# Python third-party library imports.
import mock

# Django imports.
from django.template import Context, Template, TemplateSyntaxError
from django.test import TestCase

# Application imports
from conntrackt.templatetags.conntrackt_tags import html_link, active_link, current_url_equals


@mock.patch('conntrackt.templatetags.conntrackt_tags.urlresolvers.reverse')
class HtmlLinkTest(TestCase):

    def test_url_reverse_called_with_passed_in_args(self, mock_reverse):
        """
        Tests if URL reversing is performed using the correct set of
        passed-in arguments.
        """

        html_link("My link", "my_view", 'arg1', 'arg2', 'arg3')

        mock_reverse.assert_called_once_with("my_view", args=('arg1', 'arg2', 'arg3'))

    def test_url_reverse_called_without_args_if_they_are_not_passed_in(self, mock_reverse):
        """
        Tests if URL reverse is performed without using any positional
        arguments if they are not specified.
        """

        kwargs = {
            "id": "myid",
            "class": "myclass",
            "title": "mytitle",
        }


        html_link("My link", "my_view", **kwargs)

        mock_reverse.assert_called_once_with("my_view", args=())

    def test_html_id_applied_to_output_element(self, mock_reverse):
        """
        Tests if id attribute is filled-in correctly in the HTML tag.
        """

        # Mock a view we want to reverse.
        mock_reverse.return_value = "/my/url"
        kwargs = {
            'id': "my_id",
        }

        link = html_link("My link", "my_view", **kwargs)

        self.assertIn('id="my_id"', link)

    def test_html_class_applied_to_output_element(self, mock_reverse):
        """
        Tests if class attribute is filled-in correctly in the HTML tag.
        """

        # Mock a view we want to reverse.
        mock_reverse.return_value = "/my/url"
        kwargs = {
            'class': "class1,class2,class3",
        }

        link = html_link("My link", "my_view", **kwargs)

        self.assertIn('class="class1,class2,class3"', link)

    def test_html_title_applied_to_output_element(self, mock_reverse):
        """
        Tests if title attribute is filled-in correctly in the HTML tag.
        """

        # Mock a view we want to reverse.
        mock_reverse.return_value = "/my/url"
        kwargs = {
            'title': "My title",
        }

        link = html_link("My link", "my_view", **kwargs)

        self.assertIn('title="My title"', link)

    def test_get_parameter_applied_to_output_element_link(self, mock_reverse):
        """
        Tests if generated URL contains the passed-in get argument.
        """

        # Mock a view we want to reverse.
        mock_reverse.return_value = "/my/url"
        kwargs = {
            'get': "MyGetParameter",
        }

        link = html_link("My link", "my_view", **kwargs)

        self.assertIn('href="/my/url?MyGetParameter"', link)

    def test_rendered_output_format(self, mock_reverse):
        """
        Tests if the rendered output format is correct.
        """

        mock_reverse.return_value = "/my/url"

        link = html_link(
            "My link",
            "my_view",
            **{
                "id": "my_id",
                "class": "my_class",
                "title": "my_title",
                "get": "MyGetParameter=20",
            }
        )

        self.assertEqual(link, '<a href="/my/url?MyGetParameter=20" class="my_class" title="my_title" id="my_id">My link</a>')

    def test_invalid_passed_in_keyword_argument_raises_exception(self, mock_reverse):
        """
        Tests if passing-in a non-supported keyword argument raises an
        exception (if parameter validation works correctly).
        """

        with self.assertRaises(TemplateSyntaxError):
            html_link("My link", "my_view", invalid_keyword="some-value")

    def test_rendered_output_not_escaped(self, mock_reverse):
        """
        Tests if rendered output is not double-escaped by Django.
        """

        mock_reverse.return_value = "/my/url"

        template = Template('{% load conntrackt_tags %}{% html_link "My link" "my_view" class="my_class" title="my_title" id="my_id" get="MyGetParameter=20" %}')
        context = Context()
        rendered_output = template.render(context)

        self.assertEqual(rendered_output, '<a href="/my/url?MyGetParameter=20" class="my_class" title="my_title" id="my_id">My link</a>')

    def test_html_escapes_passed_in_values(self, mock_reverse):
        """
        Tests if values passed-in as keyword arguments are escaped within
        resulting output.
        """

        mock_reverse.return_value = "/my/url"

        link = html_link(
            "My </a> link",
            "my_view",
            **{
                "id": "my</a>_id",
                "class": "my</a>_class",
                "title": "my</a>_title",
                "get": "MyGetParameter=</a>",
            }
        )

        self.assertEqual(link, '<a href="/my/url?MyGetParameter=&lt;/a&gt;" class="my&lt;/a&gt;_class" title="my&lt;/a&gt;_title" id="my&lt;/a&gt;_id">My &lt;/a&gt; link</a>')