-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.py
282 lines (234 loc) · 11.5 KB
/
test.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import json
import os
import sys
from os.path import isdir, join
from shutil import copytree, rmtree
from unittest import TestCase, main
from bagit import Bag
from bagit_profile import Profile, ProfileValidationError, find_tag_files
PROFILE_URL = (
"https://raw.github.com/bagit-profiles/bagit-profiles/master/bagProfileBar.json"
)
# pylint: disable=multiple-statements
class TagFilesAllowedTest(TestCase):
def tearDown(self):
if isdir(self.bagdir):
rmtree(self.bagdir)
def setUp(self):
self.bagdir = join("/tmp", "bagit-profile-test-bagdir")
if isdir(self.bagdir):
rmtree(self.bagdir)
copytree("./fixtures/test-tag-files-allowed/bag", self.bagdir)
with open(join("./fixtures/test-tag-files-allowed/profile.json"), "r") as f:
self.profile_dict = json.loads(f.read())
def test_not_given(self):
profile = Profile("TEST", self.profile_dict)
bag = Bag(self.bagdir)
result = profile.validate(bag)
self.assertTrue(result)
def test_required_not_allowed(self):
self.profile_dict["Tag-Files-Allowed"] = []
self.profile_dict["Tag-Files-Required"] = ["tag-foo"]
with open(join(self.bagdir, "tag-foo"), "w"):
pass
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue("Required tag files" in profile.report.errors[0].value)
def test_existing_not_allowed(self):
self.profile_dict["Tag-Files-Allowed"] = []
with open(join(self.bagdir, "tag-foo"), "w"):
pass
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue("Existing tag file" in profile.report.errors[0].value)
class BagitProfileIgnoreBagInfoTagNameCapitalizationTests(TestCase):
def tearDown(self):
if isdir(self.bagdir):
rmtree(self.bagdir)
def setUp(self):
self.bagdir = join("/tmp", "bagit-profile-test-bagdir")
if isdir(self.bagdir):
rmtree(self.bagdir)
copytree("./fixtures/test-tag-files-allowed/bag", self.bagdir)
with open(join("./fixtures/test-tag-files-allowed/profile.json"), "r") as f:
self.profile_dict = json.loads(f.read())
def test_BagInfoTagsShouldAlwaysMatchWhenCaseIsSame_EvenWhenCaseIsRespected(self):
self.profile_dict["Bag-Info"] = {"BagIt-Profile-Identifier": {"required": True}}
profile = Profile("TEST", self.profile_dict, ignore_baginfo_tag_case=False)
result = profile.validate(Bag(self.bagdir))
self.assertTrue(result)
self.assertEqual(len(profile.report.errors), 0)
def test_BagInfoTagsShouldAlwaysMatchWhenCaseIsSame_EvenWhenCaseIsIgnored(self):
self.profile_dict["Bag-Info"] = {"BagIt-Profile-Identifier": {"required": True}}
profile = Profile("TEST", self.profile_dict, ignore_baginfo_tag_case=True)
result = profile.validate(Bag(self.bagdir))
self.assertTrue(result)
self.assertEqual(len(profile.report.errors), 0)
def test_BagInfoTagCaseShouldBeIgnoredWhenRequested(self):
self.profile_dict["Bag-Info"] = {"BAGIT-PROFILE-IDENTIFIER": {"required": True}}
profile = Profile("TEST", self.profile_dict, ignore_baginfo_tag_case=True)
result = profile.validate(Bag(self.bagdir))
self.assertTrue(result)
self.assertEqual(len(profile.report.errors), 0)
def test_BagInfoTagCaseShouldNotBeIgnoredWhenToldNotTo(self):
self.profile_dict["Bag-Info"] = {"BAGIT-PROFILE-IDENTIFIER": {"required": True}}
profile = Profile("TEST", self.profile_dict, ignore_baginfo_tag_case=False)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
def test_BagInfoTagCaseShouldNotBeIgnoredByDefault(self):
self.profile_dict["Bag-Info"] = {"BAGIT-PROFILE-IDENTIFIER": {"required": True}}
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
class BagitProfileV1_3_0_Tests(TestCase):
def tearDown(self):
if isdir(self.bagdir):
rmtree(self.bagdir)
def setUp(self):
self.bagdir = join("/tmp", "bagit-profile-test-bagdir")
if isdir(self.bagdir):
rmtree(self.bagdir)
copytree("./fixtures/test-tag-files-allowed/bag", self.bagdir)
with open(join("./fixtures/test-tag-files-allowed/profile.json"), "r") as f:
self.profile_dict = json.loads(f.read())
self.profile_dict["BagIt-Profile-Info"]["BagIt-Profile-Version"] = "1.3.0"
def test_BagInfoTagDescriptionShouldNotBeABoolean(self):
self.profile_dict["Bag-Info"] = {"FakeTag": {"description": False}}
with self.assertRaises(ProfileValidationError) as context:
profile = Profile("TEST", self.profile_dict)
self.assertTrue("tag description property, when present, must be a string", context.exception.value)
def test_BagInfoTagDescriptionShouldNotBeANumber(self):
self.profile_dict["Bag-Info"] = {"FakeTag": {"description": 0}}
with self.assertRaises(ProfileValidationError) as context:
profile = Profile("TEST", self.profile_dict)
self.assertTrue("tag description property, when present, must be a string", context.exception.value)
def test_BagInfoTagDescriptionShouldBeAString(self):
self.profile_dict["Bag-Info"] = {"FakeTag": {"description": "some string"}}
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertTrue(result)
self.assertEqual(len(profile.report.errors), 0)
def test_PayloadManifestAllowedRequiredConsistency(self):
self.profile_dict["Manifests-Allowed"] = ["foo"]
self.profile_dict["Manifests-Required"] = ["sha256"]
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue("Required payload manifest type(s)" in profile.report.errors[0].value)
def test_TagManifestAllowedRequiredConsistency(self):
self.profile_dict["Tag-Manifests-Allowed"] = ["foo"]
self.profile_dict["Tag-Manifests-Required"] = ["sha256"]
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue("Required tag manifest type(s)" in profile.report.errors[0].value)
def test_DisallowedPayloadManifestInBag(self):
self.profile_dict["Manifests-Allowed"] = ["foo"]
self.profile_dict["Manifests-Required"] = []
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue("Unexpected payload manifest type(s)" in profile.report.errors[0].value)
def test_DisallowedTagManifestInBag(self):
self.profile_dict["Tag-Manifests-Allowed"] = ["foo"]
self.profile_dict["Tag-Manifests-Required"] = []
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue("Unexpected tag manifest type(s)" in profile.report.errors[0].value)
def test_AllowMorePayloadManifestsThanRequired(self):
self.profile_dict["Manifests-Allowed"] = ["sha256", "sha512"]
self.profile_dict["Manifests-Required"] = ["sha256"]
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertTrue(result)
self.assertEqual(len(profile.report.errors), 0)
def test_AllowMoreTagManifestsThanRequired(self):
self.profile_dict["Tag-Manifests-Allowed"] = ["sha256", "sha512"]
self.profile_dict["Tag-Manifests-Required"] = ["sha256"]
profile = Profile("TEST", self.profile_dict)
result = profile.validate(Bag(self.bagdir))
self.assertTrue(result)
self.assertEqual(len(profile.report.errors), 0)
class BagitProfileConstructorTest(TestCase):
def setUp(self):
with open("./fixtures/bagProfileBar.json", "rb") as f:
self.profile_str = (
f.read().decode("utf-8") if sys.version_info > (3,) else f.read()
)
self.profile_dict = json.loads(self.profile_str)
def test_profile_kwarg(self):
profile_url = Profile(PROFILE_URL)
profile_dict = Profile(PROFILE_URL, profile=self.profile_dict)
profile_str = Profile(PROFILE_URL, profile=self.profile_str)
self.assertEqual(
json.dumps(profile_str.profile),
json.dumps(profile_dict.profile),
"Loaded from string",
)
self.assertEqual(
json.dumps(profile_url.profile),
json.dumps(profile_dict.profile),
"Loaded from URL",
)
def testVersionInfo(self):
profile = Profile(PROFILE_URL, profile=self.profile_dict)
self.assertEqual(profile.profile_version_info, (1, 2, 0), "Bundled: 1.2.0")
del self.profile_dict["BagIt-Profile-Info"]["BagIt-Profile-Version"]
profile = Profile(PROFILE_URL, profile=self.profile_dict)
self.assertEqual(
profile.profile_version_info, (1, 1, 0), "Default profile version 1.1.0"
)
class Test_bag_profile(TestCase):
def setUp(self):
self.bag = Bag("fixtures/test-bar")
self.profile = Profile(PROFILE_URL)
self.retrieved_profile = self.profile.get_profile()
def test_validate_bagit_profile_info(self):
self.assertTrue(
self.profile.validate_bagit_profile_info(self.retrieved_profile)
)
def test_report_after_validate(self):
self.assertIsNone(self.profile.report)
self.profile.validate(self.bag)
self.assertTrue(self.profile.report.is_valid)
def test_validate(self):
self.assertTrue(self.profile.validate(self.bag))
def test_validate_bag_info(self):
self.assertTrue(self.profile.validate_bag_info(self.bag))
def test_validate_manifests_required(self):
self.assertTrue(self.profile.validate_manifests_required(self.bag))
def test_validate_allow_fetch(self):
self.assertTrue(self.profile.validate_allow_fetch(self.bag))
def test_validate_accept_bagit_version(self):
self.assertTrue(self.profile.validate_accept_bagit_version(self.bag))
def test_validate_serialization(self):
# Test on unzipped Bag.
self.assertTrue(
self.profile.validate_serialization(os.path.abspath("fixtures/test-bar"))
)
# Test on zipped Bag.
self.profile = Profile(PROFILE_URL)
self.assertTrue(
self.profile.validate_serialization(
os.path.abspath("fixtures/test-foo.zip")
)
)
def test_find_tag_files(self):
expect = [
join(os.getcwd(), "fixtures/test-bar", f)
for f in ["DPN/dpnFirstNode.txt", "DPN/dpnRegistry"]
]
self.assertEqual(sorted(find_tag_files(self.bag.path)), expect)
if __name__ == "__main__":
main()