Files
@ 632e4747042f
Branch filter:
Location: conntrackt/conntrackt/tests/test_tags.py
632e4747042f
10.1 KiB
text/x-python
CONNT-34: Updated implementation for active_link Django template tag:
- Added possibility of passing-in the view positional and keyword
arguments.
- Updated inline documentation.
- Implemented tests.
- Added possibility of passing-in the view positional and keyword
arguments.
- Updated inline documentation.
- Implemented tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | # -*- 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
from django.urls import reverse
# Application imports
from conntrackt.templatetags.conntrackt_tags import html_link, active_link, current_url_equals
# Test imports.
from .helpers import create_get_request
@mock.patch('conntrackt.templatetags.conntrackt_tags.urls.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=</a>" class="my</a>_class" title="my</a>_title" id="my</a>_id">My </a> link</a>')
class CurrentUrlEqualsTest(TestCase):
def get_context_for_view(self, view, *args, **kwargs):
"""
Returns a Context instance where the request path has been
constructed using the passed-in view (or view name), and view
positional/keyword arguments.
Arguments:
view
View function or name for request object.
args
Positional arguments to pass into the view.
kwargs
Keyword arguments to pass into the view.
Returns:
django.template.Context instance with request.
"""
request = create_get_request(reverse(view, args=args, kwargs=kwargs))
context = Context({'request': request})
return context
def test_non_matching_url_returns_false(self):
request = create_get_request("/this/url/does/not/exist")
context = Context({'request': request})
self.assertEqual(current_url_equals(context, 'index'), False)
def test_matching_url_returns_true(self):
context = self.get_context_for_view('project_create')
self.assertEqual(current_url_equals(context, 'project_create'), True)
def test_matching_url_with_different_args_returns_false(self):
context = self.get_context_for_view('project', 1)
self.assertEqual(current_url_equals(context, 'project', 2), False)
def test_matching_url_with_different_kwargs_returns_false(self):
context = self.get_context_for_view('project', pk=1)
self.assertEqual(current_url_equals(context, 'project', pk=2), False)
def test_matching_url_with_GET_parameters_returns_true(self):
request = create_get_request(reverse('project', kwargs={'pk': 1}) + '?my_get_param=10')
context = Context({'request': request})
self.assertEqual(current_url_equals(context, 'project', pk=1), True)
class ActiveLinkTest(TestCase):
def get_context_for_view(self, view, *args, **kwargs):
"""
Returns a Context instance where the request path has been
constructed using the passed-in view (or view name), and view
positional/keyword arguments.
Arguments:
view
View function or name for request object.
args
Positional arguments to pass into the view.
kwargs
Keyword arguments to pass into the view.
Returns:
django.template.Context instance with request.
"""
request = create_get_request(reverse(view, args=args, kwargs=kwargs))
context = Context({'request': request})
return context
def test_empty_string_returned_on_view_without_arguments_and_no_match(self):
context = self.get_context_for_view('index')
self.assertEqual(active_link(context, 'project_create'), '')
def test_default_value_returned_on_view_without_arguments_and_match(self):
context = self.get_context_for_view('index')
self.assertEqual(active_link(context, 'index'), 'active')
def test_empty_string_returned_on_view_with_positional_arguments_and_no_match(self):
context = self.get_context_for_view('project', 1)
self.assertEqual(active_link(context, 'project', 'active', 2), '')
def test_value_returned_on_view_with_positional_arguments_and_match(self):
context = self.get_context_for_view('project', 1)
self.assertEqual(active_link(context, 'project', 'active', 1), 'active')
def test_empty_string_returned_on_view_with_keyword_arguments_and_no_match(self):
context = self.get_context_for_view('project', 1)
self.assertEqual(active_link(context, 'project', pk=2), '')
def test_default_value_returned_on_view_with_keyword_arguments_and_match(self):
context = self.get_context_for_view('project', 1)
self.assertEqual(active_link(context, 'project', pk=1), 'active')
def test_provided_value_returned_on_view_with_matching_url(self):
context = self.get_context_for_view('project', 1)
self.assertEqual(active_link(context, 'project', 'myvalue', 1), 'myvalue')
self.assertEqual(active_link(context, 'project', 'myvalue', pk=1), 'myvalue')
|