-
Notifications
You must be signed in to change notification settings - Fork 3
/
authz_tests.py
241 lines (197 loc) · 11.9 KB
/
authz_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
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
#!/bin/env python
import TestConstants
from abstract_fedora_tests import FedoraTests, register_tests, Test
import random
import uuid
@register_tests
class FedoraAuthzTests(FedoraTests):
# Create test objects all inside here for easy of review
CONTAINER = "/test_authz"
COVER_ACL = "@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n" \
"@prefix pcdm: <http://pcdm.org/models#> .\n" \
"<#writeauth> a acl:Authorization ;" \
"acl:accessToClass pcdm:Object ;" \
"acl:mode acl:Read, acl:Write;" \
"acl:default <{0}>; " \
"acl:agent \"{1}\" ."
FILES_ACL = "@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n" \
"@prefix pcdm: <http://pcdm.org/models#> .\n" \
"<#writeauth> a acl:Authorization ;" \
"acl:accessTo <{0}> ;" \
"acl:mode acl:Read, acl:Write;" \
"acl:agent \"{1}\" ."
def verifyAuthEnabled(self):
self.log("Checking that authZ is enabled")
lst = [random.choice('0123456789abcdef') for n in range(16)]
random_string = "".join(lst)
temp_auth = FedoraTests.create_auth(random_string, random_string)
r = self.do_get(self.getFedoraBase(), admin=temp_auth)
self.assertEqual(401, r.status_code, "Did not get expected response code")
def getAclUri(self, response):
acls = self.get_link_headers(response)
self.assertIsNotNone(acls['acl'])
return acls['acl'][0]
@Test
def doAuthTests(self):
self.log("Running doAuthTests")
self.verifyAuthEnabled()
self.log("Create \"cover\" container")
r = self.do_put(self.getBaseUri() + "/cover")
self.assertEqual(201, r.status_code, "Did not get expected response code")
cover_location = self.get_location(r)
cover_acl = self.getAclUri(r)
self.log("Make \"cover\" a pcdm:Object")
sparql = "PREFIX pcdm: <http://pcdm.org/models#>" \
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" \
"INSERT { <> rdf:type pcdm:Object } WHERE { }"
headers = {
'Content-type': TestConstants.SPARQL_UPDATE_MIMETYPE
}
r = self.do_patch(cover_location, headers=headers, body=sparql)
self.assertEqual(204, r.status_code, "Did not get expected response code")
self.log("Verify no current ACL")
r = self.do_get(cover_acl)
self.assertEqual(404, r.status_code, "Did not get expected response code")
self.log("Add ACL to \"cover\"")
headers = {
'Content-type': 'text/turtle'
}
body = self.COVER_ACL.format(cover_location, self.config[TestConstants.USER_NAME_PARAM])
r = self.do_put(cover_acl, headers=headers, body=body)
self.assertEqual(201, r.status_code, "Did not get expected response code")
self.log("Create \"files\" inside \"cover\"")
r = self.do_put(cover_location + "/files")
self.assertEqual(201, r.status_code, "Did not get expected response code")
files_location = self.get_location(r)
files_acl = self.getAclUri(r)
self.log("Anonymous can't access \"cover\"")
r = self.do_get(cover_location, admin=None)
self.assertEqual(401, r.status_code, "Did not get expected response code")
self.log("Anonymous can't access \"cover/files\"")
r = self.do_get(files_location, admin=None)
self.assertEqual(401, r.status_code, "Did not get expected response code")
self.log("{0} can access \"cover\"".format(self.config[TestConstants.ADMIN_USER_PARAM]))
r = self.do_get(cover_location)
self.assertEqual(200, r.status_code, "Did not get expected response code")
self.log("{0} can access \"cover/files\"".format(self.config[TestConstants.ADMIN_USER_PARAM]))
r = self.do_get(files_location)
self.assertEqual(200, r.status_code, "Did not get expected response code")
self.log("{0} can access \"cover\"".format(self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_get(cover_location, admin=False)
self.assertEqual(200, r.status_code, "Did not get expected response code")
self.log("{0} can access \"cover/files\"".format(self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_get(files_location, admin=False)
self.assertEqual(200, r.status_code, "Did not get expected response code")
auth = self.create_auth(self.config[TestConstants.USER2_NAME_PARAM],
self.config[TestConstants.USER2_PASS_PARAM])
self.log("{0} can't access \"cover\"".format(self.config[TestConstants.USER2_NAME_PARAM]))
r = self.do_get(cover_location, admin=auth)
self.assertEqual(403, r.status_code, "Did not get expected response code")
self.log("{0} can't access \"cover/files\"".format(self.config[TestConstants.USER2_NAME_PARAM]))
r = self.do_get(files_location, admin=auth)
self.assertEqual(403, r.status_code, "Did not get expected response code")
self.log("Verify \"cover/files\" has no ACL")
r = self.do_get(files_acl)
self.assertEqual(404, r.status_code, "Did not get expected response code")
self.log("PUT Acl to \"cover/files\" to allow access for {0}".format(self.config[TestConstants.USER2_NAME_PARAM]))
headers = {
'Content-type': 'text/turtle'
}
body = self.FILES_ACL.format(files_location, self.config[TestConstants.USER2_NAME_PARAM])
r = self.do_put(files_acl, headers=headers, body=body)
self.assertEqual(201, r.status_code, "Did not get expected response code")
self.log("{0} can't access \"cover\"".format(self.config[TestConstants.USER2_NAME_PARAM]))
r = self.do_get(cover_location, admin=auth)
self.assertEqual(403, r.status_code, "Did not get expected response code")
self.log("{0} can access \"cover/files\"".format(self.config[TestConstants.USER2_NAME_PARAM]))
r = self.do_get(files_location, admin=auth)
self.assertEqual(200, r.status_code, "Did not get expected response code")
self.log("Passed")
@Test
def doDirectIndirectAuthTests(self):
self.log("Running doDirectIndirectAuthTests")
self.verifyAuthEnabled()
self.log("Create a target container")
r = self.do_post()
self.assertEqual(201, r.status_code, "Did not get expected response code")
target_location = self.get_location(r)
target_acl = self.getAclUri(r)
self.log("Create a write container")
r = self.do_post()
self.assertEqual(201, r.status_code, "Did not get expected response code")
write_location = self.get_location(r)
write_acl = self.getAclUri(r)
self.log("Make sure the /target resource is readonly")
target_ttl = "@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n"\
"<#readauthz> a acl:Authorization ;\n" \
" acl:agent \"{0}\" ;\n" \
" acl:mode acl:Read ;\n" \
" acl:accessTo <{1}> .\n".format(self.config[TestConstants.USER_NAME_PARAM], target_location)
headers = {
'Content-type': 'text/turtle'
}
r = self.do_put(target_acl, headers=headers, body=target_ttl)
self.assertEqual(201, r.status_code, "Did not get expected response code")
self.log("Make sure the write resource is writable by \"{0}\"".format(self.config[TestConstants.USER_NAME_PARAM]))
write_ttl = "@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n" \
"<#writeauth> a acl:Authorization ;\n" \
" acl:agent \"{0}\" ;\n" \
" acl:mode acl:Read, acl:Write ;\n" \
" acl:accessTo <{1}> ;\n" \
" acl:default <{1}> .\n".format(self.config[TestConstants.USER_NAME_PARAM], write_location)
r = self.do_put(write_acl, headers=headers, body=write_ttl)
self.assertEqual(201, r.status_code, "Did not get expected response code")
self.log("Verify that \"{0}\" can create a simple resource under write resource (POST)".format(self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_post(write_location, admin=False)
self.assertEqual(201, r.status_code, "Did not get expected response code")
uuid_value = str(uuid.uuid4())
self.log("Verify that \"{0}\" can create a simple resource under write resource (PUT)".format(
self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_put(write_location + "/" + uuid_value, admin=False)
self.assertEqual(201, r.status_code, "Did not get expected response code")
self.log("Verify that \"{0}\" CANNOT create a resource under target resource".format(self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_post(target_location, admin=False)
self.assertEqual(403, r.status_code, "Did not get expected response code")
self.log("Verify that \"{0}\" CANNOT create direct or indirect containers that reference target resources".format(self.config[TestConstants.USER_NAME_PARAM]))
headers = {
'Content-type': 'text/turtle',
'Link': self.make_type(TestConstants.LDP_DIRECT)
}
direct_ttl = "@prefix ldp: <http://www.w3.org/ns/ldp#> .\n" \
"@prefix test: <http://example.org/test#> .\n" \
"<> ldp:membershipResource <{0}> ;\n" \
"ldp:hasMemberRelation test:predicateToCreate .\n".format(target_location)
r = self.do_post(write_location, headers=headers, body=direct_ttl, admin=False)
self.assertEqual(403, r.status_code, "Did not get expected response code")
headers = {
'Content-type': 'text/turtle',
'Link': self.make_type(TestConstants.LDP_INDIRECT)
}
indirect_ttl = "@prefix ldp: <http://www.w3.org/ns/ldp#> .\n" \
"@prefix test: <http://example.org/test#> .\n" \
"<> ldp:insertedContentRelation test:something ;\n" \
"ldp:membershipResource <{0}> ;\n" \
"ldp:hasMemberRelation test:predicateToCreate .\n".format(target_location)
r = self.do_post(write_location, headers=headers, body=indirect_ttl, admin=False)
self.assertEqual(403, r.status_code, "Did not get expected response code")
self.log("Go ahead and create the indirect and direct containers as admin")
r = self.do_post(write_location, headers=headers, body=direct_ttl)
self.assertEqual(201, r.status_code, "Did not get expected response code")
direct_location = self.get_location(r)
r = self.do_post(write_location, headers=headers, body=indirect_ttl)
self.assertEqual(201, r.status_code, "Did not get expected response code")
indirect_location = self.get_location(r)
self.log("Attempt to verify that \"{0}\" can not actually create relationships on the readonly resource via " \
"direct or indirect container".format(self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_post(direct_location, admin=False)
self.assertEqual(403, r.status_code, "Did not get expected status code")
r = self.do_post(indirect_location, admin=False)
self.assertEqual(403, r.status_code, "Did not get expected status code")
self.log("Verify that \"{0}\" can still create a simple resource under write resource (POST)".format(self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_post(write_location, admin=False)
self.assertEqual(201, r.status_code, "Did not get expected response code")
uuid_value = str(uuid.uuid4())
self.log("Verify that \"{0}\" can still create a simple resource under write resource (PUT)".format(
self.config[TestConstants.USER_NAME_PARAM]))
r = self.do_put(write_location + "/" + uuid_value, admin=False)
self.assertEqual(201, r.status_code, "Did not get expected response code")