Anonymous avatar Anonymous committed 8ada010

Set up to create more HTML field tests

Comments (0)

Files changed (2)

tests/testapp/forms.py

 from django import forms
-from form_extensions.fields import USCurrencyField
+from form_extensions.fields import HoneypotField, HTMLField, USCurrencyField, CreditCardField
 
 
+ALLOWED_TAGS = ['a', 'em', 'h1', 'h2', 'p', 'strong']
+ALLOWED_ATTRIBUTES = ['class','id','style','title',]
+ALLOWED_STYLES = ['border','height','width',]
+ELEMENT_REPLACEMENTS = {
+    'b': 'strong',
+    'i': 'em',
+    's': 'del',
+    'strike': 'del',
+    'u': 'ins',
+}
+
+
+class HoneypotForm(forms.Form):
+    target = HoneypotField()
+
+class HoneypotInitForm(forms.Form):
+    target = HoneypotField(initial='something')
+
+class HTMLForm(forms.Form):
+    html = HTMLField(ALLOWED_TAGS, ALLOWED_ATTRIBUTES, ALLOWED_STYLES, ELEMENT_REPLACEMENTS)
+
+class HTMLTagsForm(forms.Form):
+    html = HTMLField(ALLOWED_TAGS)
+
+class HTMLAttrsForm(forms.Form):
+    html = HTMLField(ALLOWED_TAGS, ALLOWED_ATTRIBUTES)
+
+class HTMLStylesForm(forms.Form):
+    html = HTMLField(ALLOWED_TAGS, allowed_styles=ALLOWED_STYLES)
+
+class HTMLEleReplaceForm(forms.Form):
+    html = HTMLField(ALLOWED_TAGS, element_replacements=ELEMENT_REPLACEMENTS)
+
 class USCurrencyForm(forms.Form):
-    price = USCurrencyField()
+    price = USCurrencyField()
+
+class CreditCardForm(forms.Form):
+    number = CreditCardField()

tests/testapp/tests.py

 """
 
 from django.test import TestCase
-from .forms import USCurrencyForm
+from .forms import HoneypotForm, HoneypotInitForm, USCurrencyForm, CreditCardForm
+from .forms import HTMLForm, HTMLTagsForm, HTMLAttrsForm, HTMLStylesForm, HTMLEleReplaceForm
+
+class HoneypotFieldTests(TestCase):
+
+    def test_correct(self):
+        data = {
+            'target': ''
+        }
+        form = HoneypotForm(data)
+        self.assertTrue(form.is_valid())
+
+    def test_correct2(self):
+        data = {
+            'target': 'something'
+        }
+        form = HoneypotInitForm(data)
+        self.assertTrue(form.is_valid())
+
+    def test_incorrect(self):
+        data = {
+            'target': 'bot'
+        }
+        form = HoneypotForm(data)
+        self.assertFalse(form.is_valid())
+
+    def test_incorrect2(self):
+        data = {
+            'target': 'anything'
+        }
+        form = HoneypotInitForm(data)
+        self.assertFalse(form.is_valid())
+
+class HTMLFieldTests(TestCase):
+
+    def test_not_escaped(self):
+        data = {
+            'html': '<p>This is an example</p>'
+        }
+        form = HTMLForm(data)
+        self.assertTrue(form.is_valid())
+
+    def test_escaped(self):
+        data = {
+            'html': '<p>This is <b>another</b> example</p>'
+        }
+        form = HTMLForm(data)
+        self.assertTrue(form.is_valid())
 
 
 class USCurrencyFieldTests(TestCase):
-    def test_1(self):
+
+    def test_correct(self):
+        data = {
+            'price': '12'
+        }
+        form = USCurrencyForm(data)
+        self.assertTrue(form.is_valid())
+
+    def test_incorrect(self):
         data = {
             'price': 'cat',
         }
         form = USCurrencyForm(data)
-        self.assertTrue(form.is_valid())
+        self.assertFalse(form.is_valid())
+
+    def test_invalid_chars(self):
+        data = {
+            'price': '@234#45.3#'
+        }
+        form = USCurrencyForm(data)
+        self.assertFalse(form.is_valid())
+
+    def test_with_commas(self):
+        data = {
+            'price': '1,000'
+        }
+        form = USCurrencyForm(data)
+        self.assertTrue(form.is_valid())
+
+    def test_with_dollar_sign(self):
+        data = {
+            'price': '$100'
+        }
+        form = USCurrencyForm(data)
+        self.assertTrue(form.is_valid())
+
+    def test_with_decimal(self):
+        data = {
+            'price': '123.00'
+        }
+        form = USCurrencyForm(data)
+        self.assertTrue(form.is_valid())
+
+class CreditCardFieldTests(TestCase):
+
+    def test_correct(self):
+        data = {
+            'number': '4000001234567899'
+        }
+        form = CreditCardForm(data)
+        self.assertTrue(form.is_valid())
+
+    def test_incorrect(self):
+        data = {
+            'number': '1234567890123456'
+        }
+        form = CreditCardForm(data)
+        self.assertFalse(form.is_valid())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.