test_forms.py 5.67 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
9
10
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division

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

14
15
# Django Imports
from django.test import TestCase
David Haynes's avatar
David Haynes committed
16
from django.contrib.auth.models import User
17
18

# App Imports
David Haynes's avatar
David Haynes committed
19
from go.forms import URLForm, SignupForm
David Haynes's avatar
David Haynes committed
20
from go.models import URL, RegisteredUser
21
22
23

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

David Haynes's avatar
David Haynes committed
27
28
29
30
31
32
33
34
35
36
37
38
39
    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
40
    def test_valid_form_no_custom(self):
41
        """
David Haynes's avatar
David Haynes committed
42
        Test that form fields are validated correctly given valid data.
43
44
        """

David Haynes's avatar
David Haynes committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        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.
        """
David Haynes's avatar
David Haynes committed
60

David Haynes's avatar
David Haynes committed
61
62
63
64
65
66
67
68
69
70
71
        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())

David Haynes's avatar
David Haynes committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    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 = {
            'target': 'https://srct.gmu.edu',
            '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 = {
            'target': 'https://srct.gmu.edu',
            '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 = {
            '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.assertFalse(form.is_valid())

David Haynes's avatar
David Haynes committed
136
class SignupFormTest(TestCase):
David Haynes's avatar
David Haynes committed
137
    """
David Haynes's avatar
David Haynes committed
138
    Test cases for the Signup form
David Haynes's avatar
David Haynes committed
139
    """
140

David Haynes's avatar
David Haynes committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    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):
        """
David Haynes's avatar
David Haynes committed
159
        Test invalid full_name field
David Haynes's avatar
David Haynes committed
160
161
        """

David Haynes's avatar
David Haynes committed
162
163
164
165
166
167
168
169
170
171
        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
172
173
174

    def test_invalid_organization(self):
        """
David Haynes's avatar
David Haynes committed
175
        Test invalid organization field
David Haynes's avatar
David Haynes committed
176
177
        """

David Haynes's avatar
David Haynes committed
178
179
180
181
182
183
184
185
186
187
        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
188

David Haynes's avatar
David Haynes committed
189
    def test_blank_description(self):
David Haynes's avatar
David Haynes committed
190
        """
David Haynes's avatar
David Haynes committed
191
        Test blank description field
David Haynes's avatar
David Haynes committed
192
193
        """

David Haynes's avatar
David Haynes committed
194
195
196
197
198
199
200
201
202
203
        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
204
205

    def test_invalid_registered(self):
206
        """
David Haynes's avatar
David Haynes committed
207
        Test invalid registered field
208
209
        """

David Haynes's avatar
David Haynes committed
210
211
212
213
214
215
216
217
218
219
        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())