test_views.py 7.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
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
49
50
51
52
# standard library imports
from __future__ import absolute_import, print_function
# 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


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)

53
54
55
56
57
58
59
60
61
62
63
64
65
66
    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)
67
68
69
70
71
72
73

class DeleteStudentTest(RoomlistViewTest):

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

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    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)

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

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')
116
        self.thomas = Student.objects.create(user=tjefferson, room=wilson_307)
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134


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)

135
136
137
138
139
140
    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)
141

142
143
144
145
146
        client = self.client_login()
        response = client.get(reverse('createConfirmation',
                                      kwargs={'confirmer_slug': 'gmason',
                                              'student_slug': 'tjefferson'}))
        self.assertEqual(response.status_code, 403)
147
148


149
150
151
class DeleteConfirmationTest(ConfirmationViewTest):

    def test_delete_confirmation_ok(self):
152
        george = Student.objects.get(user__username='gmason')
153
        confirmation = Confirmation.objects.create(confirmer=george, student=self.thomas)
154
155
156
        confirmation.save()

        client = self.client_login()
157
        # george accesses his own confirmation
158
159
160
161
        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
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    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
184
185
186
187
188
189
190

class SearchViewTest(RoomlistViewTest):

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