diff --git a/conntrackt/tests/test_tags.py b/conntrackt/tests/test_tags.py new file mode 100644 --- /dev/null +++ b/conntrackt/tests/test_tags.py @@ -0,0 +1,189 @@ +# -*- 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 . +# + + +# 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, 'My link') + + 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, 'My link') + + 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 link", + "my_view", + **{ + "id": "my_id", + "class": "my_class", + "title": "my_title", + "get": "MyGetParameter=", + } + ) + + self.assertEqual(link, 'My </a> link')