-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
62 lines (44 loc) · 2.28 KB
/
tests.py
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
from django.contrib.auth.models import Group
from django.test import Client
from django.test import TestCase
from users.models import User
class TestViewsAndGroupPermissionsTests(TestCase):
def setUp(self):
# create permissions group
self.group_1 = Group.objects.create(name="View1 Viewers")
self.group_2 = Group.objects.create(name="View2 Viewers")
self.c = Client()
self.user_1_test = User.objects.create_user(username="user_1_test", email="user_1_test@test.com", password="123456")
self.user_2_test = User.objects.create_user(username="user_2_test", email="user_2_test@test.com", password="123456")
self.user_3_test = User.objects.create_user(username="user_3_test", email="user_3_test@test.com", password="123456")
def tearDown(self):
self.group_1.delete()
self.group_2.delete()
self.user_1_test.delete()
self.user_2_test.delete()
self.user_3_test.delete()
def test_user_1_cant_access_view_2(self):
"""user NOT in group should not have access """
self.c.login(username='user_1_test', password='123456')
response = self.c.get("/view-2/")
self.assertEqual(response.status_code, 302, u'User not in group should not have access')
def test_user_2_cant_access_view_1(self):
"""user NOT in group should not have access """
self.c.login(username='user_2_test', password='123456')
response = self.c.get("/view-1/")
self.assertEqual(response.status_code, 302, u'User not in group should not have access')
def test_user_1_can_access_view_1(self):
"""user in group should have access """
self.user_1_test.groups.add(self.group_1)
self.user_1_test.save()
self.c.login(username='user_1_test', password='123456')
response = self.c.get("/view-1/")
self.assertEqual(response.status_code, 200, u'User in group should have access')
def test_user_2_can_access_view_2(self):
"""user in group should have access """
self.user_2_test.groups.add(self.group_2)
self.user_2_test.save()
self.c.login(username='user_2_test', password='123456')
response = self.c.get("/view-2/")
self.assertEqual(response.status_code, 200, u'User in group should have access')
#