Commits

Anonymous committed 2e91802 Merge

merge in tests

  • Participants
  • Parent commits ee20c74, 7ead0c9

Comments (0)

Files changed (14)

.hgignore

File contents unchanged.
+#!/usr/bin/env python
+import os
+import sys
+
+if __name__ == "__main__":
+    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings")
+
+    from django.core.management import execute_from_command_line
+
+    execute_from_command_line(sys.argv)

tests/templates/base.html

+<html>
+
+<head>
+</head>
+
+<body>
+    {% block content %}
+    {% endblock %}
+</body>
+
+</html>

tests/templates/home.html

+{% extends "base.html" %}

tests/testapp/__init__.py

Empty file added.

tests/testapp/forms.py

+from django import forms
+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 = {
+    '*': ['display', 'clear', 'text-align'],
+    'p': ['border*'],
+}
+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_ATTRIBUTES, ALLOWED_STYLES)
+
+class HTMLEleReplaceForm(forms.Form):
+    html = HTMLField(ALLOWED_TAGS, element_replacements=ELEMENT_REPLACEMENTS)
+
+class USCurrencyForm(forms.Form):
+    price = USCurrencyField()
+
+class CreditCardForm(forms.Form):
+    number = CreditCardField()

tests/testapp/models.py

+from django.db import models
+
+# Create your models here.

tests/testapp/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+from lxml import objectify
+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())
+
+    def test_tags_no_change(self):
+        input = '<p>Testing tags.</p>'
+        data = {
+            'html': input
+        }
+        form = HTMLTagsForm(data)
+        self.assertTrue(form.is_valid())
+        output = form.cleaned_data['html']
+        self.assertEqual(input, output)
+
+    def test_tags_removes_tags(self):
+        input = '<d>This should be without tags after.</d>'
+        data = {
+            'html': input
+        }
+        form = HTMLTagsForm(data)
+        self.assertTrue(form.is_valid())
+        output = form.cleaned_data['html']
+        self.assertEqual(input, output)
+
+    def test_tags_escaped(self):
+        data = {
+            'html': '<p>This has an <b>escaped</b> tag.</p>'
+        }
+        form = HTMLTagsForm(data)
+        self.assertTrue(form.is_valid())
+        expected = '<p>This has an &lt;b&gt;escaped&lt;/b&gt;tag.</p>'
+        output = form.cleaned_data['html']
+        self.assertEqual(expected, output)
+
+    def test_attrs_no_change(self):
+        input = '<p class="example">Testing attributes.</p>'
+        data = {
+            'html': input
+        }
+        form = HTMLAttrsForm(data)
+        self.assertTrue(form.is_valid())
+        output = form.cleaned_data['html']
+        self.assertEqual(input, output)
+
+    def test_attrs_remove(self):
+        data = {
+            'html': '<p align="right">Testing attributes.</p>'
+        }
+        form = HTMLTagsForm(data)
+        self.assertTrue(form.is_valid())
+        expected = '<p>Testing attributes.</p>'
+        output = form.cleaned_data['html']
+        self.assertEqual(expected, output)
+
+
+    def test_styles_no_change(self):
+        input = '<p style="border: 2px solid black;">Testing styles.</p>'
+        data = {
+            'html': input
+        }
+        form = HTMLStylesForm(data)
+        self.assertTrue(form.is_valid())
+        output = form.cleaned_data['html']
+        self.assertEqual(input, output)
+
+    def test_styles_remove(self):
+        data = {
+            'html': '<p style="background-color:red;">Testing styles.</p>'
+        }
+        form = HTMLTagsForm(data)
+        self.assertTrue(form.is_valid())
+        expected = '<p>Testing styles.</p>'
+        output = form.cleaned_data['html']
+        self.assertEqual(expected, output)
+
+    def test_element_replace_no_change(self):
+        data = {
+            'html': '<b>Testing element replacements.</b>'
+        }
+        form = HTMLEleReplaceForm(data)
+        self.assertTrue(form.is_valid())
+        expected = '<strong>Testing element replacements.</strong>'
+        output = form.cleaned_data['html']
+        self.assertEqual(expected, output)
+
+    def test_element_replace_escaped(self):
+        data = {
+            'html': '<p>Testing element <t>replacements</t>.</p>'
+        }
+        form = HTMLTagsForm(data)
+        self.assertTrue(form.is_valid())
+        expected = '<p>Testing element &lt;t&gt;replacements&lt;/t&gt;.</p>'
+        output = form.cleaned_data['html']
+        self.assertEqual(expected, output)
+
+class USCurrencyFieldTests(TestCase):
+
+    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.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())

tests/testapp/views.py

+# Create your views here.

tests/testsite/__init__.py

Empty file added.

tests/testsite/settings.py

+# Django settings for testsite project.
+import os
+DIRNAME = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2])
+
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+
+ADMINS = (
+    ('Testing Testerson', 'test@example.com'),
+)
+
+MANAGERS = ADMINS
+
+DATABASES = {
+    'default': {
+        'ENGINE': 'django.db.backends.sqlite3',
+        'NAME': 'django-accounts.db',
+    }
+}
+
+TIME_ZONE = 'America/Chicago'
+LANGUAGE_CODE = 'en-us'
+
+SITE_ID = 1
+
+USE_I18N = False
+USE_L10N = True
+USE_TZ = True
+
+MEDIA_ROOT = os.path.join(DIRNAME, 'media/')
+MEDIA_URL = '/media/'
+
+STATIC_ROOT = os.path.join(DIRNAME, 'files/')
+STATIC_URL = '/files/'
+
+STATICFILES_DIRS = (
+    os.path.join(DIRNAME, 'static/'),
+)
+
+STATICFILES_FINDERS = (
+    'django.contrib.staticfiles.finders.FileSystemFinder',
+    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
+)
+
+SECRET_KEY = 'dkr+86m#8)$5b*y(c3n)jp5-kwr&amp;y+4a@tvi@rt2^h0e3%mnp^'
+
+TEMPLATE_LOADERS = (
+    'django.template.loaders.filesystem.Loader',
+    'django.template.loaders.app_directories.Loader',
+)
+
+MIDDLEWARE_CLASSES = (
+    'django.middleware.common.CommonMiddleware',
+    'django.contrib.sessions.middleware.SessionMiddleware',
+    'django.middleware.csrf.CsrfViewMiddleware',
+    'django.contrib.auth.middleware.AuthenticationMiddleware',
+    'django.contrib.messages.middleware.MessageMiddleware',
+)
+
+ROOT_URLCONF = 'testsite.urls'
+
+WSGI_APPLICATION = 'testsite.wsgi.application'
+
+TEMPLATE_DIRS = (
+    os.path.join(DIRNAME, 'templates/'),
+)
+
+INSTALLED_APPS = (
+    'django.contrib.auth',
+    'django.contrib.contenttypes',
+    'django.contrib.sessions',
+    'django.contrib.sites',
+    'django.contrib.messages',
+    'django.contrib.staticfiles',
+    'form_extensions',
+    'testapp',
+)
+
+
+LOGGING = {
+    'version': 1,
+    'disable_existing_loggers': False,
+    'filters': {
+        'require_debug_false': {
+            '()': 'django.utils.log.RequireDebugFalse'
+        }
+    },
+    'handlers': {
+        'mail_admins': {
+            'level': 'ERROR',
+            'filters': ['require_debug_false'],
+            'class': 'django.utils.log.AdminEmailHandler'
+        }
+    },
+    'loggers': {
+        'django.request': {
+            'handlers': ['mail_admins'],
+            'level': 'ERROR',
+            'propagate': True,
+        },
+    }
+}

tests/testsite/urls.py

+from django.conf.urls import patterns, include, url
+from .views import HomePageView
+
+# Uncomment the next two lines to enable the admin:
+# from django.contrib import admin
+# admin.autodiscover()
+
+urlpatterns = patterns('',
+    # Examples:
+    url(r'^$', HomePageView.as_view(), name='home'),
+    # url(r'^testsite/', include('testsite.foo.urls')),
+
+    # Uncomment the admin/doc line below to enable admin documentation:
+    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
+
+    # Uncomment the next line to enable the admin:
+    # url(r'^admin/', include(admin.site.urls)),
+)

tests/testsite/views.py

+from django.views.generic.base import TemplateView
+
+class HomePageView(TemplateView):
+
+    template_name = "home.html"

tests/testsite/wsgi.py

+"""
+WSGI config for testsite project.
+
+This module contains the WSGI application used by Django's development server
+and any production WSGI deployments. It should expose a module-level variable
+named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
+this application via the ``WSGI_APPLICATION`` setting.
+
+Usually you will have the standard Django WSGI application here, but it also
+might make sense to replace the whole Django WSGI application with a custom one
+that later delegates to the Django one. For example, you could introduce WSGI
+middleware here, or combine a Django application with an application of another
+framework.
+
+"""
+import os
+
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings")
+
+# This application object is used by any WSGI server configured to use this
+# file. This includes Django's development server, if the WSGI_APPLICATION
+# setting points here.
+from django.core.wsgi import get_wsgi_application
+application = get_wsgi_application()
+
+# Apply WSGI middleware here.
+# from helloworld.wsgi import HelloWorldApplication
+# application = HelloWorldApplication(application)