test_views.py 5.57 KB
Newer Older
1 2 3 4 5 6 7 8 9
"""
go/test_views.py

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

# Django Imports
from django.contrib.auth.models import User
David Haynes's avatar
David Haynes committed
10
from django.test import TestCase, Client
11 12

# App Imports
13
from .models import URL, RegisteredUser
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 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

class IndexTest(TestCase):
    """
    Test cases for the index view
    """

    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):
    """
    Test cases for the new_link view
    """

    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.
        """

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

class MyLinksTest(TestCase):
    """
    Test cases for the my_links view
    """

    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.
        """

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

class PostTest(TestCase):
    """
    Test cases for the post() helper function
    """

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

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

class ViewTest(TestCase):
    """
    Test cases for the "view" view
    """

    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
103 104 105 106
class EditTest(TestCase):
    """
    Test cases for the edit view
    """
David Haynes's avatar
David Haynes committed
107 108

    def setUp(self):
David Haynes's avatar
David Haynes committed
109
        """
David Haynes's avatar
David Haynes committed
110 111
        Set up any variables such as dummy objects that will be utilised in
        testing methods
David Haynes's avatar
David Haynes committed
112 113
        """

David Haynes's avatar
David Haynes committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        # 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
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
class DeleteTest(TestCase):
    """
    Test cases for the delete view
    """

    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 RedirectionTest(TestCase):
    """
    Test cases for the redirection view
    """

    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', target='https://srct.gmu.edu')

    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)

class StaffMemberRequiredTest(TestCase):
    """
    Test cases for the staff_member_required() helper function
    """

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

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

class UserAdminTest(TestCase):
    """
    Test cases for the useradmin view
    """

    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.
        """

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