-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasic_interaction_tests.py
203 lines (171 loc) · 7.92 KB
/
basic_interaction_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
#!/bin/env python
import TestConstants
from abstract_fedora_tests import FedoraTests, register_tests, Test
import os
@register_tests
class FedoraBasicIxnTests(FedoraTests):
# Create test objects all inside here for easy of review
CONTAINER = "/test_nested"
def createTestResource(self, type, files=None):
""" Create a container with an expected type link type and return the URI """
link_type = self.make_type(type)
headers = {
'Link': link_type
}
r = self.do_post(self.getBaseUri(), headers=headers, files=files)
self.assertEqual(201, r.status_code, "Did not create container")
location = self.get_location(r)
self.nodes.append(location)
r = self.do_get(location)
self.assertEqual(200, r.status_code, "Did not get container")
self.assertIsNotNone(r.headers['Link'], "Did not get any link headers returned")
type_headers = FedoraTests.get_link_headers(r)
self.assertIsNotNone(type_headers['type'], "Did not get any link headers with rel=type")
self.assertIn(type, type_headers['type'], "Did not find link header for {}".format(type))
return location
@Test
def testBasicContainer(self):
self.log("Running testBasicContainer")
self.createTestResource(TestConstants.LDP_BASIC)
self.log("Passed")
@Test
def testDirectContainer(self):
self.log("Running testDirectContainer")
self.createTestResource(TestConstants.LDP_DIRECT)
self.log("Passed")
@Test
def testIndirectContainer(self):
self.log("Running testIndirectContainer")
self.createTestResource(TestConstants.LDP_INDIRECT)
self.log("Passed")
@Test
def testNonRdfSource(self):
self.log("Running testNonRdfSource")
testfiles = {'files': ('testdata.csv', 'this,is,some,data\n')}
self.createTestResource(TestConstants.LDP_NON_RDF_SOURCE, files=testfiles)
self.log("Passed")
@Test
def testLdpResource(self):
""" We don't allow you to create a ldp:Resource so this returns 400 Bad Request """
self.log("Running testLdpResource")
link_type = self.make_type(TestConstants.LDP_RESOURCE)
headers = {
'Link': link_type
}
r = self.do_post(self.getBaseUri(), headers=headers)
self.assertEqual(400, r.status_code, "Did not get expected response")
self.log("Passed")
@Test
def testLdpContainer(self):
""" We don't allow you to create a ldp:Container so this returns 400 Bad Request """
self.log("Running testLdpResource")
link_type = self.make_type(TestConstants.LDP_CONTAINER)
headers = {
'Link': link_type
}
r = self.do_post(self.getBaseUri(), headers=headers)
self.assertEqual(400, r.status_code, "Did create container")
self.log("Passed")
@Test
def doNestedTests(self):
self.log("Running doNestedTests")
self.log("Create a container")
headers = {
'Content-type': 'text/turtle'
}
r = self.do_post(self.getBaseUri(), headers=headers, body=TestConstants.OBJECT_TTL)
self.assertEqual(201, r.status_code, "Did not get expected status code")
location = self.get_location(r)
self.log("Create a container in a container")
r = self.do_post(location, headers=headers, body=TestConstants.OBJECT_TTL)
self.assertEqual(201, r.status_code, "Did not get expected status code")
location2 = self.get_location(r)
self.log("Create binary inside a container inside a container")
with open(os.path.join(os.getcwd(), 'resources', 'basic_image.jpg'), 'rb') as fp:
headers = {
'Content-type': 'image/jpeg'
}
data = fp.read()
r = self.do_post(location2, headers=headers, body=data)
self.assertEqual(201, r.status_code, "Did not get expected status code")
binary_location = self.get_location(r)
self.log("Delete binary")
r = self.do_delete(binary_location)
self.assertEqual(204, r.status_code, "Did not get expected status code")
self.log("Verify its gone")
r = self.do_get(binary_location)
self.assertEqual(410, r.status_code, "Did not get expected status code")
self.log("Delete container with a container inside it")
r = self.do_delete(location)
self.assertEqual(204, r.status_code, "Did not get expected status code")
self.log("Verify both are gone")
r = self.do_get(location2)
self.assertEqual(410, r.status_code, "Did not get expected status code")
r = self.do_get(location)
self.assertEqual(410, r.status_code, "Did not get expected status code")
self.log("Passed")
def changeIxnModels(self, location, starting_model):
""" This function uses a created object at {location} with starting type {starting_model}.
The below dictionary of tuples works as such
expected_ixn_change = {
<Initial Model>: [
(<Model to change to>, <expected response status code>),
"""
expected_ixn_change = {
TestConstants.LDP_BASIC: [
(TestConstants.LDP_INDIRECT, 409),
(TestConstants.LDP_DIRECT, 409),
(TestConstants.LDP_NON_RDF_SOURCE, 409),
(TestConstants.LDP_RESOURCE, 400),
(TestConstants.LDP_CONTAINER, 400)
],
TestConstants.LDP_DIRECT: [
(TestConstants.LDP_BASIC, 409),
(TestConstants.LDP_INDIRECT, 409),
(TestConstants.LDP_NON_RDF_SOURCE, 409),
(TestConstants.LDP_RESOURCE, 400),
(TestConstants.LDP_CONTAINER, 400)
],
TestConstants.LDP_INDIRECT: [
(TestConstants.LDP_BASIC, 409),
(TestConstants.LDP_DIRECT, 409),
(TestConstants.LDP_NON_RDF_SOURCE, 409),
(TestConstants.LDP_RESOURCE, 400),
(TestConstants.LDP_CONTAINER, 400)
],
TestConstants.LDP_NON_RDF_SOURCE: [
(TestConstants.LDP_BASIC, 409),
(TestConstants.LDP_DIRECT, 409),
(TestConstants.LDP_INDIRECT, 409),
(TestConstants.LDP_RESOURCE, 400),
(TestConstants.LDP_CONTAINER, 400)
]
}
for model, result in expected_ixn_change[starting_model]:
self.log("Changing from {0} to {1} expect status {2}".format(starting_model, model, result))
if model == TestConstants.LDP_NON_RDF_SOURCE:
files = {'file': ('testcsvdata.csv', 'this,is,changed,data\nnow,go,away,please\n')}
else:
files = None
headers = {
'Link': self.make_type(model)
}
r = self.do_put(location, headers=headers, files=files)
self.assertEqual(result, r.status_code, "Did not get expected response")
self.log("Passed")
@Test
def testChangeIxnModel(self):
self.log("Running changeIxnModel")
self.log("Create a basic container")
basic = self.createTestResource(TestConstants.LDP_BASIC)
self.changeIxnModels(basic, TestConstants.LDP_BASIC)
self.log("Create a direct container")
direct = self.createTestResource(TestConstants.LDP_DIRECT)
self.changeIxnModels(direct, TestConstants.LDP_DIRECT)
self.log("Create a indirect container")
indirect = self.createTestResource(TestConstants.LDP_INDIRECT)
self.changeIxnModels(indirect, TestConstants.LDP_INDIRECT)
self.log("Create a Non Rdf Source")
testfiles = {'files': ('testdata.csv', 'this,is,some,data\n')}
non_rdf = self.createTestResource(TestConstants.LDP_NON_RDF_SOURCE, files=testfiles)
self.changeIxnModels(non_rdf, TestConstants.LDP_NON_RDF_SOURCE)