Skip to content

Commit

Permalink
Test security of widget
Browse files Browse the repository at this point in the history
  • Loading branch information
jpic committed Jan 29, 2014
1 parent 4e4a993 commit a284841
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions autocomplete_light/tests/widgets.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import re
import unittest

from lxml import etree
from lxml.html import etree
from lxml.cssselect import CSSSelector

try:
from unittest import mock
except ImportError: # python2
import mock

from django import template
from django.test import TestCase

import autocomplete_light

from ..example_apps.basic.models import FkModel
from ..example_apps.security_test.models import Item


class LazyAutocomplete(autocomplete_light.AutocompleteModelBase):
pass


class WidgetBaseTestCase(unittest.TestCase):
class WidgetBaseTestCase(TestCase):
widget_class = autocomplete_light.WidgetBase
fixtures = ['security_test.json']

def autocomplete_input(self, et):
return CSSSelector('input.autocomplete')(et)[0]
Expand Down Expand Up @@ -152,6 +152,17 @@ def test_lazy_autcomplete_access(self):
self.fail('widget.autocomplete access should not raise '
'AutocompleteNotRegistered')

def test_value_out_of_queryset(self):
widget = self.widget_class('ItemAutocomplete')
html = widget.render('somewidget', [1, 2])
span = etree.fromstring(html)

choices = CSSSelector('[data-value]')(span)

self.assertEqual(len(choices), 1)
self.assertEqual(int(choices[0].attrib['data-value']), 1)


class ChoiceWidgetTestCase(WidgetBaseTestCase):
widget_class = autocomplete_light.ChoiceWidget

Expand Down Expand Up @@ -180,3 +191,6 @@ def test_autocomplete_widget_template(self):

def test_widget_attrs(self):
pass # no widget_attrs for TextWidget

def test_value_out_of_queryset(self):
pass # no queryset for text widget

0 comments on commit a284841

Please sign in to comment.