test_forms.py 5.89 KB
Newer Older
David Haynes's avatar
David Haynes committed
1
"""
David Haynes's avatar
David Haynes committed
2
3
go/test_forms.py

David Haynes's avatar
David Haynes committed
4
5
6
7
8
9
10
11
Unit test the Go forms.

References:
    - http://stackoverflow.com/a/7304658
"""
# Python stdlib Imports
from datetime import datetime, timedelta

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

# App Imports
17
18
from .forms import SignupForm, URLForm, EditForm
from .models import URL, RegisteredUser
19
20

class URLFormTest(TestCase):
David Haynes's avatar
David Haynes committed
21
22
23
24
25
26
27
28
29
30
31
    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
32
    def test_valid_form_no_custom(self):
33
        """
David Haynes's avatar
David Haynes committed
34
        Test that form fields are validated correctly given valid data.
35
        """
David Haynes's avatar
David Haynes committed
36
        form_data = {
David Haynes's avatar
David Haynes committed
37
            'destination': 'https://srct.gmu.edu',
David Haynes's avatar
David Haynes committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            '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.
        """
        form_data = {
David Haynes's avatar
David Haynes committed
52
            'destination': 'https://srct.gmu.edu',
David Haynes's avatar
David Haynes committed
53
54
55
56
57
58
59
60
61
            '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
62
    def test_invalid_destination(self):
David Haynes's avatar
David Haynes committed
63
64
65
66
        """
        Test that form fields are validated correctly given valid data.
        """
        form_data = {
David Haynes's avatar
David Haynes committed
67
            'destination': '.gmu.edu',
David Haynes's avatar
David Haynes committed
68
69
70
71
72
73
74
75
76
            'short': 'pls',
            'expires': '1 Day',
            'expires_custom': ''
        }

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

David Haynes's avatar
David Haynes committed
77
78
79
80
81
82
83
84
85
86
    # def test_invalid_short(self):
    #     """
    #     Test that form fields are validated correctly given valid data.
    #     """
    #     form_data = {
    #         'destination': 'https://srct.gmu.edu',
    #         'short': '',
    #         'expires': '1 Day',
    #         'expires_custom': ''
    #     }
David Haynes's avatar
David Haynes committed
87

David Haynes's avatar
David Haynes committed
88
89
90
    #     form = URLForm(data=form_data)
    #     print(form.errors)
    #     self.assertFalse(form.is_valid())
David Haynes's avatar
David Haynes committed
91
92
93
94
95
96

    def test_invalid_expires(self):
        """
        Test that form fields are validated correctly given valid data.
        """
        form_data = {
David Haynes's avatar
David Haynes committed
97
            'destination': 'https://srct.gmu.edu',
David Haynes's avatar
David Haynes committed
98
            'short': 'pls',
David Haynes's avatar
David Haynes committed
99
            'expires': '',
David Haynes's avatar
David Haynes committed
100
101
102
103
104
105
106
107
108
109
110
111
            '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
112
            'destination': 'https://srct.gmu.edu',
David Haynes's avatar
David Haynes committed
113
114
115
116
117
118
119
120
121
            '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
122
class EditFormTest(TestCase):
123
124
    """

David Haynes's avatar
David Haynes committed
125
126
    As currently this form inherits from the URLForm and does not add any
    fields, we cannot test any values. It exists purely for aesthetics.
127
128
129
130
131
132
133
134
    """
    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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class SignupFormTest(TestCase):
    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
153
        Test invalid full_name field
David Haynes's avatar
David Haynes committed
154
        """
David Haynes's avatar
David Haynes committed
155
156
157
158
159
160
161
162
163
164
        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
165
166
167

    def test_invalid_organization(self):
        """
David Haynes's avatar
David Haynes committed
168
        Test invalid organization field
David Haynes's avatar
David Haynes committed
169
        """
David Haynes's avatar
David Haynes committed
170
171
172
173
174
175
176
177
178
179
        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
180

David Haynes's avatar
David Haynes committed
181
    def test_blank_description(self):
David Haynes's avatar
David Haynes committed
182
        """
David Haynes's avatar
David Haynes committed
183
        Test blank description field
David Haynes's avatar
David Haynes committed
184
        """
David Haynes's avatar
David Haynes committed
185
186
187
188
189
190
191
192
193
194
        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
195
196

    def test_invalid_registered(self):
197
        """
David Haynes's avatar
David Haynes committed
198
        Test invalid registered field
199
        """
David Haynes's avatar
David Haynes committed
200
201
202
203
204
205
206
207
208
209
        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())