test_forms.py 6.09 KB
Newer Older
David Haynes's avatar
David Haynes committed
1 2 3 4 5 6 7
"""
Unit test the Go forms.

References:
    - http://stackoverflow.com/a/7304658
"""

8
# Future Imports
David Haynes's avatar
David Haynes committed
9 10
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
11

David Haynes's avatar
David Haynes committed
12 13 14
# Python stdlib Imports
from datetime import datetime, timedelta

15
# Django Imports
16
from django.contrib.auth.models import User
17
from django.test import TestCase
18 19

# App Imports
20 21
from .forms import SignupForm, URLForm, EditForm
from .models import URL, RegisteredUser
22 23 24

class URLFormTest(TestCase):
    """
David Haynes's avatar
David Haynes committed
25
    Test cases for the URL form
26
    """
27

28 29 30 31 32 33 34 35 36 37 38 39 40
    def setUp(self):
        """
        Set up any variables such as dummy objects that will be utilised in
        testing methods
        """

        # Setup a blank URL object with an owner
        User.objects.create(username='dhaynes', password='password')
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        URL.objects.create(owner=get_registered_user, short='test')


David Haynes's avatar
David Haynes committed
41
    def test_valid_form_no_custom(self):
42
        """
David Haynes's avatar
David Haynes committed
43
        Test that form fields are validated correctly given valid data.
44 45
        """

David Haynes's avatar
David Haynes committed
46
        form_data = {
David Haynes's avatar
David Haynes committed
47
            'target': 'http://srct.gmu.edu',
David Haynes's avatar
David Haynes committed
48 49 50 51 52 53 54 55 56 57 58 59 60
            '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.
        """
David Haynes's avatar
David Haynes committed
61

David Haynes's avatar
David Haynes committed
62
        form_data = {
David Haynes's avatar
David Haynes committed
63
            'target': 'http://srct.gmu.edu',
David Haynes's avatar
David Haynes committed
64 65 66 67 68 69 70 71 72
            '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())

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    def test_invalid_target(self):
        """
        Test that form fields are validated correctly given valid data.
        """

        form_data = {
            'target': '.gmu.edu',
            'short': 'pls',
            'expires': '1 Day',
            'expires_custom': ''
        }

        form = URLForm(data=form_data)
        print(form.errors)
        self.assertFalse(form.is_valid())

    def test_invalid_short(self):
        """
        Test that form fields are validated correctly given valid data.
        """

        form_data = {
David Haynes's avatar
David Haynes committed
95
            'target': 'http://srct.gmu.edu',
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
            'short': 'test',
            'expires': '1 Day',
            'expires_custom': ''
        }

        form = URLForm(data=form_data)
        print(form.errors)
        self.assertFalse(form.is_valid())

    def test_invalid_expires(self):
        """
        Test that form fields are validated correctly given valid data.
        """

        form_data = {
David Haynes's avatar
David Haynes committed
111
            'target': 'http://srct.gmu.edu',
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            'short': 'pls',
            'expires': 'None',
            'expires_custom': ''
        }

        form = URLForm(data=form_data)
        print(form.errors)
        self.assertFalse(form.is_valid())

    def test_invalid_expires_custom(self):
        """
        Test that form fields are validated correctly given valid data.
        """

        form_data = {
David Haynes's avatar
David Haynes committed
127
            'target': 'http://srct.gmu.edu',
128 129 130 131 132 133 134 135 136
            'short': 'pls',
            'expires': 'Custom Date',
            'expires_custom': datetime.now() - timedelta(days=1)
        }

        form = URLForm(data=form_data)
        print(form.errors)
        self.assertFalse(form.is_valid())

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
class EditForm(TestCase):
    """
    Test cases for the edit URL form.

    As currently this form inherits from the URLForm and does not add any fields,
    we cannot test any values. It exists purely for aesthetics.
    """

    def test_django_test(self):
        """
        Default test case, does not actually test anything
        """

        self.assertEqual("Hello World!", "Hello World!")

David Haynes's avatar
David Haynes committed
152
class SignupFormTest(TestCase):
David Haynes's avatar
David Haynes committed
153
    """
David Haynes's avatar
David Haynes committed
154
    Test cases for the Signup form
David Haynes's avatar
David Haynes committed
155
    """
156

David Haynes's avatar
David Haynes committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    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):
        """
175
        Test invalid full_name field
David Haynes's avatar
David Haynes committed
176 177
        """

178 179 180 181 182 183 184 185 186 187
        form_data = {
            'full_name': '',
            '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.assertFalse(form.is_valid())
David Haynes's avatar
David Haynes committed
188 189 190

    def test_invalid_organization(self):
        """
191
        Test invalid organization field
David Haynes's avatar
David Haynes committed
192 193
        """

194 195 196 197 198 199 200 201 202 203
        form_data = {
            'full_name': 'David Haynes',
            'organization': '',
            'description': 'the big brown fox jumps over the lazy dog',
            'registered': 'True'
        }

        form = SignupForm(request=None, data=form_data)
        print(form.errors)
        self.assertFalse(form.is_valid())
David Haynes's avatar
David Haynes committed
204

205
    def test_blank_description(self):
David Haynes's avatar
David Haynes committed
206
        """
207
        Test blank description field
David Haynes's avatar
David Haynes committed
208 209
        """

210 211 212 213 214 215 216 217 218 219
        form_data = {
            'full_name': 'David Haynes',
            'organization': 'SRCT',
            'description': '',
            'registered': 'True'
        }

        form = SignupForm(request=None, data=form_data)
        print(form.errors)
        self.assertTrue(form.is_valid())
David Haynes's avatar
David Haynes committed
220 221

    def test_invalid_registered(self):
222
        """
223
        Test invalid registered field
224 225
        """

226 227 228 229 230 231 232 233 234 235
        form_data = {
            'full_name': 'David Haynes',
            'organization': 'SRCT',
            'description': 'the big brown fox jumps over the lazy dog',
            'registered': 'False'
        }

        form = SignupForm(request=None, data=form_data)
        print(form.errors)
        self.assertFalse(form.is_valid())