test_views.py 9.06 KB
Newer Older
1
# standard library imports
2
from __future__ import absolute_import, print_function, unicode_literals
3
4
5
6
7
8
9
10
11
# core django imports
from django.test import TestCase
from django.test import Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
# imports from your apps
from .models import Student, Major, Confirmation
from housing.models import Room, Floor
from housing.test_views import RoomlistViewTest
12
from .cas_callbacks import pfinfo
13
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
# peoplefinder lookup tests
# these specific examples will eventually have to change
class PeoplefinderTest(TestCase):

    # presently enrolled student who has been added to peoplefinder
    def test_pf_peoplefinder_method(self):
        username = 'dhaynes'
        pf_data = pfinfo(username)
        self.assertEqual(pf_data[0], ['David', 'Haynes'])
        self.assertEqual(pf_data[1], 'CYSE')

    # student no longer in peoplefinder, or who hasn't yet been added
    def test_pf_ldap_method(self):
        username = 'lfaraone'
        pf_data = pfinfo(username)
        self.assertEqual(pf_data[0], ['Luke W', 'Faraone'])
        self.assertEqual(pf_data[1], '')

    # student employees will have their staff info return before their student info
    def test_pf_employee_method(self):
        username = 'nander13'
        pf_data = pfinfo(username)
        self.assertEqual(pf_data[0], ['Nicholas', 'Anderson'])
        self.assertEqual(pf_data[1], 'Undeclared')

    # a name not found for either (should never happen, but gracefully handle anyway)
    def test_pf_dne(self):
        username = 'bobama'
        pf_data = pfinfo(username)
        self.assertEqual(pf_data[0], ['', ''])
        self.assertEqual(pf_data[1], '')


# view tests
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
class ListMajorsTest(RoomlistViewTest):

    def test_list_majors_ok(self):
        client = Client()
        response = client.get(reverse('list_majors'))
        self.assertEqual(response.status_code, 200)


class DetailMajorTest(RoomlistViewTest):

    def setUp(self):
        major = Major.objects.create(name='Government and International Politics, BA')
        major.save()
        return super(DetailMajorTest, self).setUp()

    def test_list_majors_ok(self):
        client = self.client_login()
        response = client.get(reverse('detail_major',
                                      kwargs={'slug': 'government-and-international-politics-ba'}))
        self.assertEqual(response.status_code, 200)


class DetailStudentTest(RoomlistViewTest):

    def test_detail_student_ok(self):
        client = self.client_login()
        response = client.get(reverse('detail_student',
                                      kwargs={'slug': 'gmason'}))
        self.assertEqual(response.status_code, 200)


class UpdateStudentTest(RoomlistViewTest):

    def test_update_student_ok(self):
        client = self.client_login()
        response = client.get(reverse('update_student',
                                      kwargs={'slug': 'gmason'}))
        self.assertEqual(response.status_code, 200)

88
89
90
91
92
93
94
95
96
97
98
99
100
101
    def test_update_student_redirect(self):
        tjefferson = User.objects.create_user(username='tjefferson',
                                              first_name='Thomas',
                                              last_name='Jefferson',
                                              email='tjefferson@masonlive.gmu.edu',
                                              password='louisiana')
        thomas = Student.objects.create(user=tjefferson)

        client = self.client_login()
        response = client.get(reverse('update_student',
                                      kwargs={'slug': 'tjefferson'}))
        # you may not update other people-- we helpfully send you
        # to your own uptate page
        self.assertEqual(response.status_code, 302)
102
103
104
105
106
107
108

class DeleteStudentTest(RoomlistViewTest):

    def test_delete_student_ok(self):
        client = self.client_login()
        response = client.get(reverse('delete_student',
                                      kwargs={'slug': 'gmason'}))
109
        # you may delete yourself
110
111
        self.assertEqual(response.status_code, 200)

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    def test_delete_student_redirect(self):
        tjefferson = User.objects.create_user(username='tjefferson',
                                              first_name='Thomas',
                                              last_name='Jefferson',
                                              email='tjefferson@masonlive.gmu.edu',
                                              password='louisiana')
        thomas = Student.objects.create(user=tjefferson)

        client = self.client_login()
        response = client.get(reverse('delete_student',
                                      kwargs={'slug': 'tjefferson'}))
        # you may not delete other people-- we helpfully send you
        # to your own deletion page
        self.assertEqual(response.status_code, 302)

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

class RemoveSocialConfirmationTest(RoomlistViewTest):

    def test_remove_social_redirect(self):
        client = self.client_login()
        response = client.get(reverse('remove_social',
                                      kwargs={'slug': 'gmason'}))
        # student does not have any social media acounts set
        self.assertEqual(response.status_code, 302)


class ConfirmationViewTest(RoomlistViewTest):

    def setUp(self):
        # 'extending' the method
        setUp = super(ConfirmationViewTest, self).setUp()

        wilson_third = Floor.objects.get(building__name='Wilson', number='3')
        wilson_307 = Room.objects.create(floor=wilson_third, number='307')
        tjefferson = User.objects.create_user(username='tjefferson',
                                              first_name='Thomas',
                                              last_name='Jefferson',
                                              email='tjefferson@masonlive.gmu.edu',
                                              password='louisiana')
151
        self.thomas = Student.objects.create(user=tjefferson, room=wilson_307)
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169


class CreateConfirmationTest(ConfirmationViewTest):

    def test_create_confirmation_self(self):
        client = self.client_login()
        response = client.get(reverse('createConfirmation',
                                      kwargs={'confirmer_slug': 'gmason',
                                              'student_slug': 'gmason'}))
        self.assertEqual(response.status_code, 404)

    def test_create_confirmation_same_floor(self):
        client = self.client_login()
        response = client.get(reverse('createConfirmation',
                                      kwargs={'confirmer_slug': 'tjefferson',
                                              'student_slug': 'gmason'}))
        self.assertEqual(response.status_code, 200)

170
171
172
173
174
175
    def test_create_confirmation_already_flagged(self):
        george = Student.objects.get(user__username='gmason')
        confirmation = Confirmation.objects.create(confirmer=george, student=self.thomas)
        confirmation.save()
        # just to verify that, yes, the confirmation was created
        self.assertEqual(self.thomas.get_flag_count(), 1)
176

177
178
179
180
181
        client = self.client_login()
        response = client.get(reverse('createConfirmation',
                                      kwargs={'confirmer_slug': 'gmason',
                                              'student_slug': 'tjefferson'}))
        self.assertEqual(response.status_code, 403)
182
183


184
185
186
class DeleteConfirmationTest(ConfirmationViewTest):

    def test_delete_confirmation_ok(self):
187
        george = Student.objects.get(user__username='gmason')
188
        confirmation = Confirmation.objects.create(confirmer=george, student=self.thomas)
189
190
191
        confirmation.save()

        client = self.client_login()
192
        # george accesses his own confirmation
193
194
195
196
        response = client.get(reverse('deleteConfirmation',
                                      kwargs={'confirmer_slug': 'gmason',
                                              'student_slug': 'tjefferson'}))
        self.assertEqual(response.status_code, 200)
Daniel W Bond's avatar
Daniel W Bond committed
197

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    def test_delete_confirmation_dne(self):
        client = self.client_login()
        response = client.get(reverse('deleteConfirmation',
                                      kwargs={'confirmer_slug': 'gmason',
                                              'student_slug': 'tjefferson'}))
        # try to go to a delete confirmation that does not exist
        self.assertEqual(response.status_code, 404)


    def test_delete_confirmation_other_student(self):
        george = Student.objects.get(user__username='gmason')
        confirmation = Confirmation.objects.create(confirmer=self.thomas, student=george)
        confirmation.save()

        client = self.client_login()
        # george tries to access thomas' confirmation
        response = client.get(reverse('deleteConfirmation',
                                      kwargs={'confirmer_slug': 'tjefferson',
                                              'student_slug': 'gmason'}))
        self.assertEqual(response.status_code, 403)

Daniel W Bond's avatar
Daniel W Bond committed
219
220
221
222
223
224
225

class SearchViewTest(RoomlistViewTest):

    def test_search_ok(self):
        client = self.client_login()
        response = client.get(reverse('search'))
        self.assertEqual(response.status_code, 200)