-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_perm.py
More file actions
211 lines (189 loc) · 9.02 KB
/
test_perm.py
File metadata and controls
211 lines (189 loc) · 9.02 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
from __future__ import unicode_literals
import json
from django.test import TestCase
class TestAdminPerms(TestCase):
def test_first_signup(self):
# register a first user
username, password = 'firstuser', 'password'
email = 'autotest@deis.io'
submit = {
'username': username,
'password': password,
'email': email,
}
url = '/api/auth/register'
response = self.client.post(url, json.dumps(submit), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertTrue(response.data['is_superuser'])
# register a second user
username, password = 'second', 'password'
email = 'autotest@deis.io'
submit = {
'username': username,
'password': password,
'email': email,
}
url = '/api/auth/register'
response = self.client.post(url, json.dumps(submit), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertFalse(response.data['is_superuser'])
def test_list(self):
submit = {
'username': 'firstuser',
'password': 'password',
'email': 'autotest@deis.io',
}
url = '/api/auth/register'
response = self.client.post(url, json.dumps(submit), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertTrue(response.data['is_superuser'])
self.assertTrue(
self.client.login(username='firstuser', password='password'))
response = self.client.get('/api/admin/perms', content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
self.assertEqual(response.data['results'][0]['username'], 'firstuser')
self.assertTrue(response.data['results'][0]['is_superuser'])
class TestAppPerms(TestCase):
fixtures = ['test_sharing.json']
def setUp(self):
self.assertTrue(
self.client.login(username='autotest-1', password='password'))
def test_create(self):
# check that user 1 sees her lone app
response = self.client.get('/api/apps')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
app_id = response.data['results'][0]['id']
# check that user 2 can't see any apps
self.assertTrue(
self.client.login(username='autotest-2', password='password'))
response = self.client.get('/api/apps')
self.assertEqual(len(response.data['results']), 0)
# TODO: test that git pushing to the app fails
# give user 2 permission to user 1's app
self.assertTrue(
self.client.login(username='autotest-1', password='password'))
url = "/api/apps/{}/perms".format(app_id)
body = {'username': 'autotest-2'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
# check that user 2 can see the app
self.assertTrue(
self.client.login(username='autotest-2', password='password'))
response = self.client.get('/api/apps')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
# TODO: check that user 2 can git push the app
def test_delete(self):
pass
def test_list(self):
# check that user 1 sees her lone app
response = self.client.get('/api/apps')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
app_id = response.data['results'][0]['id']
# create a new object permission
url = "/api/apps/{}/perms".format(app_id)
body = {'username': 'autotest-2'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
# list perms on the app
response = self.client.get(
"/api/apps/{}/perms".format(app_id), content_type='application/json')
self.assertEqual(response.data, {'users': ['autotest-2']})
class TestFormationPerms(TestCase):
fixtures = ['test_sharing.json']
def setUp(self):
self.assertTrue(
self.client.login(username='autotest-1', password='password'))
def test_create(self):
# check that user 1 sees her lone formation
response = self.client.get('/api/formations')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
formation_id = response.data['results'][0]['id']
# check that user 2 can't see any formations
self.assertTrue(
self.client.login(username='autotest-2', password='password'))
response = self.client.get('/api/formations')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 0)
# give user 2 permission to user 1's formation
self.assertTrue(
self.client.login(username='autotest-1', password='password'))
url = '/api/formations/{formation_id}/perms'.format(**locals())
body = {'username': 'autotest-2'}
response = self.client.post(
url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
# check that user 1 can see a formation
self.assertTrue(
self.client.login(username='autotest-2', password='password'))
response = self.client.get('/api/formations')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
self.assertEqual(response.data['results'][0]['id'], formation_id)
# revoke user 2's permission to user 1's formation
self.assertTrue(
self.client.login(username='autotest-1', password='password'))
username = 'autotest-2'
url = '/api/formations/{formation_id}/perms/{username}/'.format(**locals())
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)
def test_create_errors(self):
response = self.client.get('/api/formations')
formation_id = response.data['results'][0]['id']
# try to give user 2 permission to user 1's formation as user 2
self.assertTrue(
self.client.login(username='autotest-2', password='password'))
url = '/api/formations/{formation_id}/perms'.format(**locals())
body = {'username': 'autotest-2'}
response = self.client.post(
url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 403)
# try to give user 1 permission to user 1's formation as user 2
url = '/api/formations/{formation_id}/perms'.format(**locals())
body = {'username': 'autotest-1'}
response = self.client.post(
url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 403)
def test_delete(self):
# give user 2 permission to user 1's formation
response = self.client.get('/api/formations')
formation_id = response.data['results'][0]['id']
url = '/api/formations/{formation_id}/perms'.format(**locals())
body = {'username': 'autotest-2'}
response = self.client.post(
url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
# check that user 1 can see a formation
self.assertTrue(
self.client.login(username='autotest-2', password='password'))
response = self.client.get('/api/formations')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
self.assertEqual(response.data['results'][0]['id'], formation_id)
# revoke user 2's permission to user 1's formation
self.assertTrue(
self.client.login(username='autotest-1', password='password'))
username = 'autotest-2'
url = '/api/formations/{formation_id}/perms/{username}/'.format(**locals())
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)
# check that user 1 can't see any formations
self.assertTrue(
self.client.login(username='autotest-2', password='password'))
response = self.client.get('/api/formations')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 0)
def test_delete_errors(self):
# revoke user 2's permission to user 1's formation
response = self.client.get('/api/formations')
formation_id = response.data['results'][0]['id']
username = 'autotest-2'
url = '/api/formations/{formation_id}/perms/{username}/'.format(**locals())
response = self.client.delete(url)
self.assertEqual(response.status_code, 404)
def test_list(self):
pass