-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathversion_tests.py
231 lines (184 loc) · 9.64 KB
/
version_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
#!/bin/env python
import TestConstants
from abstract_fedora_tests import FedoraTests, register_tests, Test
import time
@register_tests
class FedoraVersionTests(FedoraTests):
# Create test objects all inside here for easy of review
CONTAINER = "/test_version"
@staticmethod
def count_mementos(response):
body = response.content.decode('UTF-8')
mementos = [x for x in body.split('\n') if x.find("rel=\"memento\"") >= 0]
return len(mementos)
@Test
def doContainerVersioningTest(self):
self.log("Running doContainerVersioningTest")
headers = {
'Link': self.make_type(TestConstants.LDP_BASIC)
}
r = self.do_post(self.getBaseUri(), headers=headers)
self.log("Create a basic container")
self.assertEqual(201, r.status_code, "Did not create container")
location = self.get_location(r)
version_endpoint = location + "/" + TestConstants.FCR_VERSIONS
r = self.do_get(version_endpoint)
self.assertEqual(200, r.status_code, "Did not find versions where we should")
self.log("Create a version")
r = self.do_post(version_endpoint)
self.assertEqual(201, r.status_code, 'Did not create a version')
self.log("Get the resource content")
headers = {
'Accept': TestConstants.JSONLD_MIMETYPE
}
r = self.do_get(location, headers=headers)
self.assertEqual(200, r.status_code, "Could not get the resource")
body = r.content.decode('UTF-8').rstrip('\n')
new_date = FedoraTests.get_rfc_date("2000-06-01 08:21:00")
self.log("Create a version with provided datetime")
headers = {
'Content-Type': TestConstants.JSONLD_MIMETYPE,
'Prefer': TestConstants.PUT_PREFER_LENIENT,
'Memento-Datetime': new_date
}
r = self.do_post(version_endpoint, headers=headers, body="[]")
self.assertEqual(400, r.status_code, "Empty body should cause Bad request")
r = self.do_post(version_endpoint, headers=headers, body=body)
self.assertEqual(201, r.status_code, "Did not create memento will body.")
memento_location = self.get_location(r)
self.log("Try creating another version at the same location")
r = self.do_post(version_endpoint, headers=headers, body=body)
self.assertEqual(409, r.status_code, "Did not get conflict trying to create a second memento")
self.log("Check memento exists")
r = self.do_get(memento_location)
self.assertEqual(200, r.status_code, "Memento was not found")
found_datetime = r.headers['Memento-Datetime']
self.assertIsNotNone(found_datetime, "Did not find Memento-Datetime header")
self.assertEqual(0, self.compare_rfc_dates(new_date, found_datetime), "Returned Memento-Datetime did not match sent")
self.log("Patch the original resource")
sparql_body = "prefix dc: <http://purl.org/dc/elements/1.1/> " \
"DELETE { <> dc:title ?o . }" \
"INSERT { <> dc:title \"Updated title\" . }" \
"WHERE { <> dc:title ?o . }"
headers = {
"Content-Type": TestConstants.SPARQL_UPDATE_MIMETYPE
}
r = self.do_patch(location, headers=headers, body=sparql_body)
self.assertEqual(204, r.status_code, "Unable to patch")
self.log("Try to patch the memento")
r = self.do_patch(memento_location, headers=headers, body=sparql_body)
self.assertEqual(405, r.status_code, "Did not get denied PATCH request.")
# Delay one second to ensure we don't POST at the same time
time.sleep(1)
self.log("Create another version")
r = self.do_post(version_endpoint)
self.assertEqual(201, r.status_code, 'Did not create a version')
self.log("Count mementos")
headers = {
'Accept': 'application/link-format'
}
r = self.do_get(version_endpoint, headers=headers)
self.assertEqual(200, r.status_code, "Did not get the fcr:versions content")
self.assertEqual(3, FedoraVersionTests.count_mementos(r), "Incorrect number of Mementos")
self.log("Delete a Memento")
r = self.do_delete(memento_location)
self.assertEqual(204, r.status_code, "Did not delete the Memento")
self.log("Validate delete")
r = self.do_get(memento_location)
self.assertEqual(404, r.status_code, "Memento was not gone")
self.log("Validate delete with another count")
r = self.do_get(version_endpoint, headers=headers)
self.assertEqual(200, r.status_code, "Did not get the fcr:versions content")
self.assertEqual(2, FedoraVersionTests.count_mementos(r), "Incorrect number of Mementos")
self.log("Create a memento at the deleted datetime")
headers = {
'Content-Type': TestConstants.JSONLD_MIMETYPE,
'Prefer': TestConstants.PUT_PREFER_LENIENT,
'Memento-Datetime': new_date
}
r = self.do_post(version_endpoint, headers=headers, body=body)
self.assertEqual(201, r.status_code, "Did not create version with specific date and body")
self.log("Check the memento exists again")
r = self.do_get(memento_location)
self.assertEqual(200, r.status_code, "Memento was not found")
self.log("Passed")
@Test
def doBinaryVersioningTest(self):
self.log("Running doBinaryVersioningTest")
headers = {
'Link': self.make_type(TestConstants.LDP_NON_RDF_SOURCE),
'Content-Type': 'text/csv'
}
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = self.do_post(self.getBaseUri(), headers=headers, files=files)
self.log("Create a NonRdfSource")
self.assertEqual(201, r.status_code, "Did not create NonRdfSource")
location = self.get_location(r)
version_endpoint = location + "/" + TestConstants.FCR_VERSIONS
r = self.do_get(version_endpoint)
self.assertEqual(200, r.status_code, "Did not find versions where we should")
self.log("Create a version")
r = self.do_post(version_endpoint)
self.assertEqual(201, r.status_code, 'Did not create a version')
new_date = FedoraTests.get_rfc_date("2000-06-01 08:21:00")
self.log("Create a version with provided datetime")
headers = {
'Content-Type': 'text/csv',
'Memento-Datetime': new_date
}
r = self.do_post(version_endpoint, headers=headers, files=files)
self.assertEqual(201, r.status_code, "Did not create version with specific date and body")
memento_location = self.get_location(r)
self.log("Try creating another version at the same location")
r = self.do_post(version_endpoint, headers=headers, files=files)
self.assertEqual(409, r.status_code, "Did not get conflict trying to create a second memento")
self.log("Check memento exists")
r = self.do_head(memento_location)
self.assertEqual(200, r.status_code, "Memento was not found")
found_datetime = r.headers['Memento-Datetime']
self.assertIsNotNone(found_datetime, "Did not find Memento-Datetime header")
self.assertEqual(0, self.compare_rfc_dates(new_date, found_datetime), "Returned Memento-Datetime did not match sent")
self.log("PUT to the original resource")
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\nevent,more,data,tosend\n')}
headers = {
'Content-Type': 'text/csv'
}
r = self.do_put(location, headers=headers, files=files)
self.assertEqual(204, r.status_code, "Unable to put")
self.log("Try to put to the memento")
r = self.do_put(memento_location, headers=headers, files=files)
self.assertEqual(405, r.status_code, "Did not get denied PUT request.")
# Delay one second to ensure we don't POST at the same time
time.sleep(1)
self.log("Create another version")
r = self.do_post(version_endpoint)
self.assertEqual(201, r.status_code, 'Did not create a version')
self.log("Count mementos")
headers = {
'Accept': 'application/link-format'
}
r = self.do_get(version_endpoint, headers=headers)
self.assertEqual(200, r.status_code, "Did not get the fcr:versions content")
self.assertEqual(3, FedoraVersionTests.count_mementos(r), "Incorrect number of Mementos")
self.log("Delete a Memento")
r = self.do_delete(memento_location)
self.assertEqual(204, r.status_code, "Did not delete the Memento")
self.log("Validate delete")
r = self.do_get(memento_location)
self.assertEqual(404, r.status_code, "Memento was not gone")
self.log("Validate delete with another count")
r = self.do_get(version_endpoint, headers=headers)
self.assertEqual(200, r.status_code, "Did not get the fcr:versions content")
self.assertEqual(2, FedoraVersionTests.count_mementos(r), "Incorrect number of Mementos")
self.log("Create a memento at the deleted datetime")
headers = {
'Content-Type': TestConstants.JSONLD_MIMETYPE,
'Prefer': TestConstants.PUT_PREFER_LENIENT,
'Memento-Datetime': new_date
}
r = self.do_post(version_endpoint, headers=headers, files=files)
self.assertEqual(201, r.status_code, "Did not create version with specific date and body")
self.log("Check the memento exists again")
r = self.do_head(memento_location)
self.assertEqual(200, r.status_code, "Memento was not found")
self.log("Passed")