test_forms.py 5.69 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
16
# Django Imports
from django.test import TestCase
David Haynes's avatar
David Haynes committed
17
from django.contrib.auth.models import User
18
19

# App Imports
David Haynes's avatar
David Haynes committed
20
from go.forms import URLForm, SignupForm
David Haynes's avatar
David Haynes committed
21
from go.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

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

David Haynes's avatar
David Haynes committed
62
63
64
65
66
67
68
69
70
71
72
        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
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
136
    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
137
class SignupFormTest(TestCase):
David Haynes's avatar
David Haynes committed
138
    """
David Haynes's avatar
David Haynes committed
139
    Test cases for the Signup form
David Haynes's avatar
David Haynes committed
140
    """
141

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

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

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

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

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

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

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

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