test_view_forum.py
11.3 KB
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
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
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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from django.test import TestCase, Client
from django.core.urlresolvers import reverse
from rolepermissions.shortcuts import assign_role
from users.models import User
from courses.models import CourseCategory, Course, Subject, Topic
from forum.models import Forum, Post, PostAnswer
class ForumViewTestCase (TestCase):
def setUp(self):
self.user = User.objects.create_user(
username = 'test',
email = 'testing@amadeus.com',
is_staff = True,
is_active = True,
password = 'testing'
)
assign_role(self.user, 'system_admin')
self.user_professor = User.objects.create_user(
username = 'professor',
email = 'professor@amadeus.com',
is_staff = False,
is_active = True,
password = 'testing',
type_profile = 1
)
assign_role(self.user_professor, 'professor')
self.user_student = User.objects.create_user(
username = 'student',
email = 'student@amadeus.com',
is_staff = False,
is_active = True,
password = 'testing',
type_profile = 2
)
assign_role(self.user_student, 'student')
self.category = CourseCategory.objects.create(
name = 'Category test',
)
self.category.save()
self.course = Course.objects.create(
name = 'Course Test',
max_students = 50,
init_register_date = '2016-08-26',
end_register_date = '2016-10-01',
init_date = '2016-10-05',
end_date = '2017-10-05',
category = self.category
)
self.course.save()
self.subject = Subject.objects.create(
name = 'Subject Test',
description = "description of the subject test",
visible = True,
course = self.course,
init_date = '2016-10-05',
end_date = '2017-10-05',
)
self.subject.save()
self.topic = Topic.objects.create(
name = 'Topic Test',
description = "description of the topic test",
subject = self.subject,
owner = self.user,
)
self.topic.save()
self.forum = Forum.objects.create(
topic=self.topic,
name = 'forum test',
description = 'description of the forum test',
create_date = '2016-10-02',
modification_date = '2016-10-03',
limit_date = '2017-10-05',
)
self.forum.save()
self.post = Post.objects.create(
user = self.user_professor,
message = 'posting a test',
modification_date = '2016-11-09',
post_date = '2016-10-03',
forum = self.forum,
)
self.post.save()
self.answer = PostAnswer.objects.create(
user = self.user,
post = self.post,
message = 'testing a post answer',
modification_date = '2016-10-05',
answer_date = '2016-10-04',
)
self.answer.save()
self.client = Client()
self.client.login(username='test', password='testing')
self.client_professor = Client()
self.client_professor.login (username='professor', password='testing')
self.client_student = Client()
self.client_student.login (username='student', password='testing')
######################### ForumDetailView #########################
def test_ForumDetail_view_ok (self):
url = reverse('course:forum:view', kwargs={'slug':self.forum.slug})
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
response = self.client_professor.get(url)
self.assertEquals(response.status_code, 302)
response = self.client_student.get(url)
self.assertEquals(response.status_code, 302)
def test_ForumDetail_context(self):
url = reverse('course:forum:view', kwargs={'slug':self.forum.slug})
response = self.client.get(url)
self.assertTrue('form' in response.context)
######################### CreateForumView #########################
def test_CreateForum_view_ok (self):
url = reverse('course:forum:create')
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
response = self.client_professor.get(url)
self.assertEquals(response.status_code, 200)
#response = self.client_student.get(url)
#self.assertEquals(response.status_code, 400)
def test_CreateForum_context(self):
url = reverse('course:forum:create')
response = self.client.get(url)
self.assertTrue('form' in response.context)
response = self.client_professor.get(url)
self.assertTrue('form' in response.context)
def test_CreateForum_form_error (self):
url = reverse('course:forum:create')
data = {'name':'', 'limit_date': '', 'description':'', 'topic':''}
list_forum = Forum.objects.all().count()
response = self.client.post(url, data)
self.assertEquals (response.status_code, 400)
self.assertEquals(list_forum, Forum.objects.all().count())
response = self.client_professor.post(url, data)
self.assertEquals (response.status_code, 400)
self.assertEquals(list_forum, Forum.objects.all().count())
response = self.client_student.post(url, data)
self.assertEquals (response.status_code, 403)
self.assertEquals(list_forum, Forum.objects.all().count())
def test_CreateForum_form_ok (self):
url = reverse('course:forum:create')
data = {
'name':'Forum Test2',
'limit_date': '2017-10-05',
'description':'Test',
'topic':str(self.topic.id)
}
list_forum = Forum.objects.all().count()
response = self.client.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertEquals(list_forum+1, Forum.objects.all().count())
response = self.client_professor.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertEquals(list_forum+2, Forum.objects.all().count())
#response = self.client_student.post(url, data)
#self.assertEquals (response.status_code, 400)
#self.assertEquals(list_forum+2, Forum.objects.all().count())
######################### UpdateForumView #########################
def test_UpdateForum_view_ok (self):
url = reverse('course:forum:update', kwargs={'pk':self.forum.pk})
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
response = self.client_professor.get(url)
self.assertEquals(response.status_code, 302)
def test_UpdateForum_form_error (self):
url = reverse('course:forum:update', kwargs={'pk':self.forum.pk})
data = {'name':'', 'limit_date': '', 'description':''}
response = self.client.post(url, data)
self.assertEquals (response.status_code, 400)
response = self.client_professor.post(url, data)
self.assertEquals (response.status_code, 302)
response = self.client_student.post(url, data)
self.assertEquals (response.status_code, 302)
def test_UpdateForum_form_ok (self):
url = reverse('course:forum:update', kwargs={'pk':self.forum.pk})
data = {
'name':'Forum Updated',
'limit_date': '2017-10-05',
'description':'Test',
'topic':str(self.topic.id)
}
self.assertEquals(Forum.objects.all()[0].name, 'forum test')
response = self.client.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertEquals(Forum.objects.all()[0].name, 'Forum Updated')
forum = Forum.objects.get(name='Forum Updated')
data['name'] = 'Forum Updated as professor'
self.assertEquals(Forum.objects.all()[0].name, 'Forum Updated')
response = self.client.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertEquals(Forum.objects.all()[0].name, 'Forum Updated as professor')
forum = Forum.objects.get(name='Forum Updated as professor')
data['name'] = 'Forum Updated as student'
self.assertEquals(Forum.objects.all()[0].name, 'Forum Updated as professor')
response = self.client_student.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertNotEquals(Forum.objects.all()[0].name, 'Forum Updated as student')
forum = Forum.objects.get(name='Forum Updated as professor')
######################### CreatePostView #########################
def test_CreatePost_form_ok (self):
url = reverse ('course:forum:create_post')
data = {
'forum': str(self.forum.id),
'message':'posting a test2'
}
list_post = Post.objects.all().count()
self.assertEquals(list_post, Post.objects.all().count())
response = self.client.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertEquals(list_post+1, Post.objects.all().count())
self.assertEquals(list_post+1, Post.objects.all().count())
response = self.client_professor.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertEquals(list_post+2, Post.objects.all().count())
self.assertEquals(list_post+2, Post.objects.all().count())
response = self.client_student.post(url, data)
self.assertEquals (response.status_code, 302)
self.assertEquals(list_post+3, Post.objects.all().count())
######################### UpdatePostView #########################
def test_UpdatePost_form_error (self):
url = reverse('course:forum:update_post', kwargs={'pk':self.post.pk})
data = {'message': ''}
response = self.client.post(url, data)
self.assertFormError (response, 'form', 'message', 'Este campo é obrigatório.')
response = self.client_professor.post(url, data)
self.assertFormError (response, 'form', 'message', 'Este campo é obrigatório.')
response = self.client_student.post(url, data)
self.assertFormError (response, 'form', 'message', 'Este campo é obrigatório.')
def test_UpdatePost_form_ok (self):
url = reverse('course:forum:update_post', kwargs={'pk':self.post.pk})
data = {'message':'updating a post'}
list_post = Post.objects.all().count()
self.assertEquals (list_post, Post.objects.all().count())
response = self.client.post(url, data)
self.assertEquals (response.status_code, 200)
self.assertEquals (list_post, Post.objects.all().count())
response = self.client_professor.post(url, data)
self.assertEquals (response.status_code, 200)
self.assertEquals (list_post, Post.objects.all().count())
response = self.client_student.post(url, data)
self.assertEquals (response.status_code, 200)
self.assertEquals (list_post, Post.objects.all().count())
######################### UpdatePostAnswerView #########################
def test_UpdatePostAnswer_form_error (self):
url = reverse('course:forum:update_post_answer', kwargs={'pk':self.answer.pk})
data = {'message': ''}
response = self.client.post(url, data)
self.assertFormError (response, 'form', 'message', 'Este campo é obrigatório.')
response = self.client_professor.post(url, data)
self.assertFormError (response, 'form', 'message', 'Este campo é obrigatório.')
response = self.client_student.post(url, data)
self.assertFormError (response, 'form', 'message', 'Este campo é obrigatório.')
def test_UpdatePost_form_ok (self):
url = reverse('course:forum:update_post_answer', kwargs={'pk':self.answer.pk})
data = {'message':'updating a answer'}
list_post = PostAnswer.objects.all().count()
self.assertEquals (list_post, PostAnswer.objects.all().count())
response = self.client.post(url, data)
self.assertEquals (response.status_code, 200)
self.assertEquals (list_post, PostAnswer.objects.all().count())
response = self.client_professor.post(url, data)
self.assertEquals (response.status_code, 200)
self.assertEquals (list_post, PostAnswer.objects.all().count())
response = self.client_student.post(url, data)
self.assertEquals (response.status_code, 200)
self.assertEquals (list_post, PostAnswer.objects.all().count())