test_views.py 5.34 KB
Newer Older
1
2
3
"""
go/test_views.py

David Haynes's avatar
David Haynes committed
4
5
Test Go's controller logic.

6
7
8
9
10
References:
    - http://stackoverflow.com/a/11887308
"""
# Django Imports
from django.contrib.auth.models import User
David Haynes's avatar
David Haynes committed
11
from django.test import TestCase, Client
12
13

# App Imports
14
from .models import URL, RegisteredUser
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

class IndexTest(TestCase):
    def test_index_get_anon(self):
        """
        Test that the index view loads (200 OK) for anons
        """
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)

    def test_index_get_auth(self):
        """
        Test that the index view loads (200 OK) for authed users
        """
        client = Client()
        client.login(username='dhaynes', password='password')
        response = client.get('/')
        self.assertEqual(response.status_code, 200)

class NewLinkTest(TestCase):
    def test_new_link_get_anon(self):
        """
        Test that the index view redirects anons to login with cas on an EXTERNAL
        CAS link, so 302 REDIRECT.
        """
David Haynes's avatar
David Haynes committed
39
        response = self.client.get('/new')
40
41
42
43
44
45
46
47
        self.assertEqual(response.status_code, 302)

class MyLinksTest(TestCase):
    def test_new_link_get_anon(self):
        """
        Test that the index view redirects anons to login with cas on an EXTERNAL
        CAS link, so 302 REDIRECT.
        """
David Haynes's avatar
David Haynes committed
48
        response = self.client.get('/my')
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        self.assertEqual(response.status_code, 302)

class PostTest(TestCase):
    def test_django_test(self):
        """
        Default test case, does not actually test anything
        """
        self.assertEqual("Hello World!", "Hello World!")

class ViewTest(TestCase):
    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')

    def test_view_get_anon(self):
        """
        Test that the view Go link view loads (200 OK) for anon users
        """
        response = self.client.get('/view/test')
        self.assertEqual(response.status_code, 200)

David Haynes's avatar
David Haynes committed
77
class EditTest(TestCase):
David Haynes's avatar
David Haynes committed
78
    def setUp(self):
David Haynes's avatar
David Haynes committed
79
        """
David Haynes's avatar
David Haynes committed
80
81
        Set up any variables such as dummy objects that will be utilised in
        testing methods
David Haynes's avatar
David Haynes committed
82
        """
David Haynes's avatar
David Haynes committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        # 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')

    def test_edit_get_anon(self):
        """
        Test that the delete view redirects anons to login with cas on an EXTERNAL
        CAS link, so 302 REDIRECT.
        """

        response = self.client.get('/edit/test')
        self.assertEqual(response.status_code, 302)

David Haynes's avatar
David Haynes committed
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
137
class DeleteTest(TestCase):
    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')

    def test_delete_get_anon(self):
        """
        Test that the delete view redirects anons to login with cas on an EXTERNAL
        CAS link, so 302 REDIRECT.
        """
        response = self.client.get('/delete/test')
        self.assertEqual(response.status_code, 302)

class SignupTest(TestCase):
    def test_signup_get_anon(self):
        """
        Test that the signup view redirects anons to login with cas on an EXTERNAL
        CAS link, so 302 REDIRECT.
        """
        response = self.client.get('/signup')
        self.assertEqual(response.status_code, 302)

class RedirectionTest(TestCase):
    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)
David Haynes's avatar
David Haynes committed
138
        URL.objects.create(owner=get_registered_user, short='test', destination='https://srct.gmu.edu')
139

140
141
142
143
144
145
    # def test_redirect_get_anon(self):
    #     """
    #     Test that redirection works as intentioned on anon users.
    #     """
    #     response = self.client.get('/test')
    #     self.assertEqual(response.status_code, 302) TODO
146
147
148
149
150
151
152
153
154
155
156
157
158
159

class StaffMemberRequiredTest(TestCase):
    def test_django_test(self):
        """
        Default test case, does not actually test anything
        """
        self.assertEqual("Hello World!", "Hello World!")

class UserAdminTest(TestCase):
    def test_useradmin_get_anon(self):
        """
        Test that the useradmin view redirects anons to login with cas on an EXTERNAL
        CAS link, so 302 REDIRECT.
        """
David Haynes's avatar
David Haynes committed
160
        response = self.client.get('/manage')
161
        self.assertEqual(response.status_code, 302)