Commit 7b31e9ed8f007e3f3fb65dd25c4d8bb51c9cf105
1 parent
10aca292
Exists in
master
and in
2 other branches
added tests and modified category view to pass some of them
Showing
3 changed files
with
73 additions
and
1 deletions
Show diff stats
... | ... | @@ -0,0 +1,66 @@ |
1 | +from django.test import TestCase, Client, override_settings | |
2 | +from django.core.urlresolvers import resolve | |
3 | +from reports.views import ReportView | |
4 | +from subjects.models import Subject, Tag | |
5 | +from users.models import User | |
6 | +from topics.models import Topic, Resource | |
7 | +from chat.models import Conversation, TalkMessages | |
8 | +from categories.models import Category | |
9 | +from datetime import datetime | |
10 | +from log.models import Log | |
11 | +from django.db.models import Q | |
12 | +from django.http import HttpResponse, JsonResponse | |
13 | + | |
14 | + | |
15 | +class RedirectingRulesTest(TestCase): | |
16 | + | |
17 | + def setUp(self): | |
18 | + self.c = Client() | |
19 | + self.student = User.objects.create(username = "student01", email= "student01@amadeus.br") | |
20 | + self.student.set_password("amadeus") #because of the hash function used | |
21 | + self.student.save() | |
22 | + if self.c.login(email="student01@amadeus.br", password="amadeus"): | |
23 | + print("student01 logged in") | |
24 | + | |
25 | + | |
26 | + self.student02 = User.objects.create(username= "student02", email = "student02@amadeus.br") | |
27 | + self.student02.set_password("amadeus") | |
28 | + self.student02.save() | |
29 | + c1 = Category.objects.create(name ="test category", visible = True) | |
30 | + c1.coordinators.add(self.student02) | |
31 | + c1.save() | |
32 | + | |
33 | + | |
34 | + @override_settings(STATICFILES_STORAGE = None) # added decorator | |
35 | + def test_admin_connection(self): | |
36 | + admin = User.objects.create_superuser(username="admin" ,email="admin@amadeus.br", password="amadeus") | |
37 | + admin.save() | |
38 | + self.c.logout() | |
39 | + if self.c.login(email="admin@amadeus.br", password="amadeus"): | |
40 | + print("admin logged in") | |
41 | + | |
42 | + response = self.c.get('/dashboards/general/') | |
43 | + self.assertEqual(response.status_code, 200) | |
44 | + | |
45 | + @override_settings(STATICFILES_STORAGE = None) # added decorator | |
46 | + def test_admin_dashboard_redirect(self): | |
47 | + #as student 01 is already logged in | |
48 | + response = self.c.get('/dashboards/general/') | |
49 | + self.assertEqual(response.status_code, 302) | |
50 | + | |
51 | + @override_settings(STATICFILES_STORAGE = None) # added decorator | |
52 | + def test_category_redirect(self): | |
53 | + response = self.c.get('/dashboards/categories/') | |
54 | + self.assertEqual(response.status_code, 302) | |
55 | + print("a user which is not a coordinator is any category was redirected") | |
56 | + | |
57 | + @override_settings(STATICFILES_STORAGE = None) # added decorator | |
58 | + def test_category_connection(self): | |
59 | + self.c.logout() | |
60 | + if self.c.login(email="student02@amadeus.br", password="amadeus"): | |
61 | + print("student 02 logged in") | |
62 | + | |
63 | + response = self.c.get('/dashboards/categories/') | |
64 | + | |
65 | + self.assertEqual(response.status_code, 200) | |
66 | + print("coordinator is accessing category dashboard") | |
0 | 67 | \ No newline at end of file | ... | ... |
dashboards/views.py
... | ... | @@ -24,6 +24,8 @@ from log.mixins import LogMixin |
24 | 24 | from log.decorators import log_decorator_ajax |
25 | 25 | from log.models import Log |
26 | 26 | |
27 | +from amadeus.permissions import has_category_permissions | |
28 | + | |
27 | 29 | |
28 | 30 | class GeneralView(LogMixin, generic.TemplateView): |
29 | 31 | template_name = "dashboards/general.html" |
... | ... | @@ -77,7 +79,11 @@ class CategoryView(LogMixin, generic.TemplateView): |
77 | 79 | log_context = {} |
78 | 80 | |
79 | 81 | def dispatch(self, request, *args, **kwargs): |
80 | - return super(CategoryView, self).dispatch(request, *args, **kwargs) | |
82 | + if Category.objects.filter(coordinators__id = self.request.user.id).exists() or self.request.user.is_staff: | |
83 | + return super(CategoryView, self).dispatch(request, *args, **kwargs) | |
84 | + else: | |
85 | + return redirect('users:login') | |
86 | + | |
81 | 87 | |
82 | 88 | def get_context_data(self, **kwargs): |
83 | 89 | context = {} | ... | ... |