blob: 191eae6d4ae91c17740add74b0ba47415bc7106d [file] [log] [blame]
Tony Mackc5889322015-02-11 12:26:05 -05001import unittest
2from core.models import *
3
4class TestPermission(unittest.TestCase):
5
6 def setUp(self):
7 self.test_objects = []
8 # deployment
9 self.deployment = Deployment(name='TestDeployment')
10 self.deployment.save()
11 self.test_objects.append(self.deployment)
12 # site
13 self.site = Site(name='TestSite')
14 self.site.save()
15 self.test_objects.append(self.site)
16 # site deployment
17 self.site_deployment = SiteDeployment(site=self.site, deployment=self.deployment)
18 self.site_deployment.save()
19 self.test_objects.append(self.site_deployment)
20 # node
21 self.node = Node(name='TestNode', site_deployment=self.site_deployment)
22 self.node.save()
23 self.test_objects.append(self.node)
24 # slice
25 self.slice = Slice(name='TestSlice', site=self.site)
26 self.slice.save()
27 self.test_objects.appen(slice.slice)
28 # admin user
29 self.user_admin = User(email='user_admin@test.com', first_name='Test', last_name='Test', is_admin=True)
30 self.user_admin.site = self.site
31 self.user_admin.save()
32 self.test_objects.append(self.user_admin)
33 # read only user
34 self.user_read_only = User(email='user_read_only@test.com', first_name='Test', last_name='Test')
35 self.user_read_only.site = self.site
36 self.user_read_only.save()
37 self.test_objects.append(self.user_read_only)
38 # default user
39 self.user_default = User(email='user_default@test.com', first_name='Test', last_name='Test')
40 self.user_default.site = self.site
41 self.user_default.save()
42 self.test_objects.append(self.user_default)
43
44 # deployment admin
45 self.user_deployment_admin = User(email='user_deployment_admin@test.com', first_name='Test', last_name='Test')
46 self.user_deployment_admin.site = self.site
47 self.user_deployment_admin.save()
48 self.test_objects.append(self.user_deployment_admin)
49 deployment_privilege = DeploymentPrivilege(
50 user=self.user_deployment_admin,
51 deployment=self.deployment,
52 role='admin')
53 deployment_privilege.save()
54 self.test_objects.append(deployment_privilege)
55 # site admin
56 self.user_site_admin = User(email='user_site_admin@test.com', first_name='Test', last_name='Test')
57 self.user_site_admin = self.site
58 self.user_site_admin.save()
59 self.test_objects.append(self.user_site_admin)
60 site_admin_privilege = SitePrivilege(
61 user = self.user_site_admin,
62 site=self.site,
63 role='admin')
64 site_admin_privilege.save()
65 self.test_objects.append(site_admin_privilege)
66 # site pi
67 self.user_site_pi = User(email='user_site_pi@test.com', first_name='Test', last_name='Test')
68 self.user_site_pi = self.site
69 self.user_site_pi.save()
70 self.test_objects.append(self.user_site_pi)
71 site_pi_privilege = SitePrivilege(
72 user = self.user_site_pi,
73 site=self.site,
74 role='pi')
75 site_pi_privilege.save()
76 self.test_objects.append(site_pi_privilege)
77 # site tech
78 self.user_site_tech = User(email='user_site_tech@test.com', first_name='Test', last_name='Test')
79 self.user_site_tech = self.site
80 self.user_site_tech.save()
81 self.test_objects.append(self.user_site_tech)
82 site_tech_privilege = SitePrivilege(
83 user = self.user_site_tech,
84 site=self.site,
85 role='tech')
86 site_tech_privilege.save()
87 self.test_objects.append(site_tech_privilege)
88 # slice admin
89 self.user_slice_admin = User(email='user_slice_admin@test.com', first_name='Test', last_name='Test')
90 self.user_slice_admin = self.site
91 self.user_slice_admin.save()
92 self.test_objects.append(self.user_slice_admin)
93 slice_admin_privilege = SlicePrivilege(
94 user = self.user_slice_admin,
95 slice = self.slice,
96 role='admin')
97 slice_admin_privilege.save()
98 self.test_objects.append(slice_admin_privilege)
99 # slice access
100 self.user_slice_access = User(email='user_slice_access@test.com', first_name='Test', last_name='Test')
101 self.user_slice_access = self.site
102 self.user_slice_access.save()
103 self.test_objects.append(self.user_slice_access)
104 slice_access_privilege = SlicePrivilege(
105 user = self.user_slice_access,
106 slice = self.slice,
107 role='access')
108 slice_access_privilege.save()
109 self.test_objects.append(slice_access_privilege)
110
111
112 def test_deployment(self):
113 for user in [self.user_admin, self.user_deployment_admin]:
114 self.assertEqual(
115 self.deployment.save_by_user(user), None)
116 for user in [self.user_read_only, self.user_default, self.user_site_admin,
117 self.user_site_pi, self.user_site_tech, self.user_slice_admin,
118 self.user_slice_access]:
119 self.assertRaises(
120 PermissionDenied,
121 self.deployment.save_by_user(user,))
122
123 def test_site(self):
124 for user in [self.user_admin, self.user_site_admin, self.user_site_pi]:
125 self.assertEqual(
126 self.site.save_by_user(user), None)
127 for user in [self.user_read_only, self.user_default, self.user_deployment_admin,
128 self.user_site_tech, self.user_slice_admin, self.user_slice_access]:
129 self.assertRaises(
130 PermissionDenied,
131 self.site.save_by_user(user,))
132
133 def test_node(self):
134 for user in [self.user_admin, self.user_site_admin, self.user_site_tech]:
135 self.assertEqual(self.node.save_by_user(user), None)
136 for user in [self.user_read_only, self.user_default, self.user_deployment_admin,
137 self.user_site_pi, self.user_slice_admin, self.user_slice_access]:
138 self.assertRaises(
139 PermissionDenied,
140 self.node.save_by_user(user,))
141
142 def test_slice(self):
143 for user in [self.user_admin, self.user_site_admin, self.user_site_pi,
144 self.user_slice_admin]:
145 self.assertEqual(
146 self.slice.save_by_user(user), None)
147 for user in [self.user_read_only, self.user_default, self.user_deployment_admin,
148 self.user_site_tech, self.user_slice_access]:
149 self.assertRaises(
150 PermissionDenied,
151 self.slice.save_by_user(user,))
152
153 def test_user(self):
154 for user in [self.user_admin, self.user_site_admin, self.user_deployment_admin,
155 self.user_site_pi, self.user_default]:
156 self.assertEqual(
157 self.user_default.save_by_user(user), None)
158 for user in [self.user_read_only, self.user_deployment_admin,
159 self.user_site_tech, self.user_slice_admin, self.user_slice_access]:
160 self.assertRaises(
161 PermissionDenied,
162 self.user_default.save_by_user(user,))
163
164
165 def tearDown(self):
166 for obj in self.test_objects:
167 obj.delete()
168
169if __name__ == '__main__':
170 unittest.main()