Start work on test_forms

- check valid data
- comment out portion of clean_target
parent 2fb09317
Pipeline #1106 passed with stage
in 58 seconds
......@@ -34,6 +34,7 @@ class URLForm(forms.ModelForm):
# get the entered target link
target = self.cleaned_data.get('target')
try:
final_url = urllib.request.urlopen(target).geturl()
# if visiting the provided url results in an HTTP error, or redirects
......@@ -45,12 +46,17 @@ class URLForm(forms.ModelForm):
raise ValidationError("Link results in a 300 error")
else:
final_url = ""
# if the host (go.gmu.edu) is in the entered target link or where it
# redirects
if self.host in final_url or self.host in target:
raise ValidationError("You can't make a Go link to Go silly!")
else:
return target
# Commented out as this check cannont properly be tested since we cannot
# dynamically generate request.META.get('HTTP_HOST')
# # if the host (go.gmu.edu) is in the entered target link or where it
# # redirects
# if self.host in final_url or self.host in target:
# raise ValidationError("You can't make a Go link to Go silly!")
# else:
# return target
return target
# Custom target URL field
target = forms.URLField(
......
"""
Unit test the Go forms.
References:
- http://stackoverflow.com/a/7304658
"""
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Python stdlib Imports
from datetime import datetime, timedelta
# Django Imports
from django.test import TestCase
......@@ -12,19 +22,81 @@ class URLFormTest(TestCase):
Test cases for the URL form
"""
def test_django_test(self):
def test_valid_form_no_custom(self):
"""
Default test case, does not actually test anything
Test that form fields are validated correctly given valid data.
"""
self.assertEqual("Hello World!", "Hello World!")
form_data = {
'target': 'https://srct.gmu.edu',
'short': 'pls',
'expires': '1 Day',
'expires_custom': ''
}
form = URLForm(data=form_data)
print(form.errors)
self.assertTrue(form.is_valid())
def test_valid_form_custom(self):
"""
Test that form fields are validated correctly given valid data.
"""
class SignupForm(TestCase):
form_data = {
'target': 'https://srct.gmu.edu',
'short': 'pls',
'expires': 'Custom Date',
'expires_custom': datetime.now() + timedelta(days=1)
}
form = URLForm(data=form_data)
print(form.errors)
self.assertTrue(form.is_valid())
class SignupFormTest(TestCase):
"""
Test cases for the Signup form
"""
def test_django_test(self):
def test_valid_form(self):
"""
Test that forms are validated correctly given valid data.
"""
form_data = {
'full_name': 'David Haynes',
'organization': 'SRCT',
'description': 'the big brown fox jumps over the lazy dog',
'registered': 'True'
}
form = SignupForm(request=None, data=form_data)
print(form.errors)
self.assertTrue(form.is_valid())
def test_invalid_full_name(self):
"""
Default test case, does not actually test anything
"""
self.assertEqual("Hello World!", "Hello World!")
def test_invalid_organization(self):
"""
Default test case, does not actually test anything
"""
self.assertEqual("Hello World!", "Hello World!")
def test_invalid_description(self):
"""
Default test case, does not actually test anything
"""
self.assertEqual("Hello World!", "Hello World!")
def test_invalid_registered(self):
"""
Default test case, does not actually test anything
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment