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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
import os
import sys
from contextlib import contextmanager
from io import StringIO
import responses
from internetarchive.utils import json
from tests.conftest import IaRequestsMock, ia_call, load_test_data_file
PROTOCOL = 'https:'
STATUS_CHECK_RESPONSE = load_test_data_file('s3_status_check.json')
def test_ia_upload(tmpdir_ch, caplog):
with open('test.txt', 'w') as fh:
fh.write('foo')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', 'nasa', 'test.txt'])
assert f'uploaded test.txt to {PROTOCOL}//s3.us.archive.org/nasa/test.txt' in caplog.text
def test_ia_upload_invalid_identifier(capsys, caplog):
with open('test.txt', 'w') as fh:
fh.write('foo')
ia_call(['ia', '--log', 'upload', 'føø', 'test.txt'],
expected_exit_code=2)
out, err = capsys.readouterr()
assert "Identifier can only contain alphanumeric" in err
def test_ia_upload_status_check(capsys):
with IaRequestsMock() as rsps:
rsps.add(responses.GET, f'{PROTOCOL}//s3.us.archive.org',
body=STATUS_CHECK_RESPONSE,
content_type='application/json')
ia_call(['ia', 'upload', 'nasa', '--status-check'])
out, err = capsys.readouterr()
assert 'success: nasa is accepting requests.' in err
j = json.loads(STATUS_CHECK_RESPONSE)
j['over_limit'] = 1
rsps.reset()
rsps.add(responses.GET, f'{PROTOCOL}//s3.us.archive.org',
body=json.dumps(j),
content_type='application/json')
ia_call(['ia', 'upload', 'nasa', '--status-check'], expected_exit_code=1)
out, err = capsys.readouterr()
assert ('warning: nasa is over limit, and not accepting requests. '
'Expect 503 SlowDown errors.') in err
def test_ia_upload_debug(capsys, tmpdir_ch, nasa_mocker):
with open('test.txt', 'w') as fh:
fh.write('foo')
ia_call(['ia', 'upload', '--debug', 'nasa', 'test.txt'])
out, err = capsys.readouterr()
assert 'User-Agent' in err
assert 's3.us.archive.org/nasa/test.txt' in err
assert 'Accept:*/*' in err
assert 'Authorization:LOW ' in err
assert 'Connection:close' in err
assert 'Content-Length:3' in err
assert 'Accept-Encoding:gzip, deflate' in err
def test_ia_upload_403(capsys):
s3_error = ('<Error>'
'<Code>SignatureDoesNotMatch</Code>'
'<Message>The request signature we calculated does not match '
'the signature you provided. Check your AWS Secret Access Key '
'and signing method. For more information, see REST '
'Authentication and SOAP Authentication for details.</Message>'
"<Resource>'PUT\n\n\n\n/iacli-test-item60/test-replace.txt'</Resource>"
'<RequestId>18a9c5ea-088f-42f5-9fcf-70651cc085ca</RequestId>'
'</Error>')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT,
f'{PROTOCOL}//s3.us.archive.org/nasa/test_ia_upload.py',
body=s3_error,
status=403,
content_type='text/plain')
ia_call(['ia', 'upload', 'nasa', __file__], expected_exit_code=1)
out, err = capsys.readouterr()
assert 'error uploading test_ia_upload.py' in err
def test_ia_upload_invalid_cmd(capsys):
ia_call(['ia', 'upload', 'nasa', 'nofile.txt'], expected_exit_code=2)
out, err = capsys.readouterr()
assert "'nofile.txt' is not a valid file or directory" in err
def test_ia_upload_size_hint(capsys, tmpdir_ch, nasa_mocker):
with open('test.txt', 'w') as fh:
fh.write('foo')
ia_call(['ia', 'upload', '--debug', '--size-hint', '30', 'nasa', 'test.txt'])
out, err = capsys.readouterr()
assert 'User-Agent' in err
assert 's3.us.archive.org/nasa/test.txt' in err
assert 'x-archive-size-hint:30' in err
assert 'Accept:*/*' in err
assert 'Authorization:LOW ' in err
assert 'Connection:close' in err
assert 'Content-Length:3' in err
assert 'Accept-Encoding:gzip, deflate' in err
def test_ia_upload_automatic_size_hint_files(capsys, tmpdir_ch, nasa_mocker):
with open('foo', 'w') as fh:
fh.write('foo')
with open('bar', 'w') as fh:
fh.write('bar')
ia_call(['ia', 'upload', '--debug', 'nasa', 'foo', 'bar'])
out, err = capsys.readouterr()
assert 'x-archive-size-hint:6' in err
def test_ia_upload_automatic_size_hint_dir(capsys, tmpdir_ch, nasa_mocker):
with open('foo', 'w') as fh:
fh.write('foo')
with open('bar', 'w') as fh:
fh.write('bar')
ia_call(['ia', 'upload', '--debug', 'nasa', '.'], expected_exit_code=2)
out, err = capsys.readouterr()
assert 'x-archive-size-hint:115' in err
def test_ia_upload_unicode(tmpdir_ch, caplog):
with open('தமிழ் - baz ∆.txt', 'w') as fh:
fh.write('unicode foo')
efname = '%E0%AE%A4%E0%AE%AE%E0%AE%BF%E0%AE%B4%E0%AF%8D%20-%20baz%20%E2%88%86.txt'
with IaRequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT,
f'{PROTOCOL}//s3.us.archive.org/nasa/{efname}',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', 'nasa', 'தமிழ் - baz ∆.txt',
'--metadata', 'foo:∆'])
assert (f'uploaded தமிழ் - baz ∆.txt to {PROTOCOL}//s3.us.archive.org/nasa/'
'%E0%AE%A4%E0%AE%AE%E0%AE%BF%E0%AE%B4%E0%AF%8D%20-%20'
'baz%20%E2%88%86.txt') in caplog.text
def test_ia_upload_remote_name(tmpdir_ch, caplog):
with open('test.txt', 'w') as fh:
fh.write('foo')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/hi.txt',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', 'nasa', 'test.txt', '--remote-name',
'hi.txt'])
assert f'uploaded hi.txt to {PROTOCOL}//s3.us.archive.org/nasa/hi.txt' in caplog.text
def test_ia_upload_stdin(tmpdir_ch, caplog):
@contextmanager
def replace_stdin(f):
original_stdin = sys.stdin
sys.stdin = f
try:
yield
finally:
sys.stdin = original_stdin
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/hi.txt',
body='',
content_type='text/plain')
with replace_stdin(StringIO('foo')):
ia_call(['ia', '--log', 'upload', 'nasa', '-', '--remote-name', 'hi.txt'])
assert f'uploaded hi.txt to {PROTOCOL}//s3.us.archive.org/nasa/hi.txt' in caplog.text
def test_ia_upload_inexistent_file(tmpdir_ch, capsys, caplog):
ia_call(['ia', 'upload', 'foo', 'test.txt'], expected_exit_code=2)
out, err = capsys.readouterr()
assert "'test.txt' is not a valid file or directory" in err
def test_ia_upload_spreadsheet(tmpdir_ch, capsys):
with open('foo.txt', 'w') as fh:
fh.write('foo')
with open('test.txt', 'w') as fh:
fh.write('bar')
with open('test.csv', 'w') as fh:
fh.write('identifier,file,REMOTE_NAME\n')
fh.write('nasa,foo.txt,\n')
fh.write(',test.txt,bar.txt\n')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/foo.txt',
body='',
content_type='text/plain')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/bar.txt',
body='',
content_type='text/plain')
ia_call(['ia', 'upload', '--spreadsheet', 'test.csv'])
out, err = capsys.readouterr()
assert 'uploading foo.txt' in err
assert 'uploading bar.txt' in err
def test_ia_upload_spreadsheet_item_column(tmpdir_ch, capsys):
with open('test.txt', 'w') as fh:
fh.write('foo')
with open('test.csv', 'w') as fh:
fh.write('item,file\n')
fh.write('nasa,test.txt\n')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', 'upload', '--spreadsheet', 'test.csv'])
out, err = capsys.readouterr()
assert 'uploading test.txt' in err
def test_ia_upload_spreadsheet_item_and_identifier_column(tmpdir_ch, capsys):
# item is preferred, and both are discarded
with open('test.txt', 'w') as fh:
fh.write('foo')
with open('test.csv', 'w') as fh:
fh.write('item,identifier,file\n')
fh.write('nasa,uhoh,test.txt\n')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', 'upload', '--spreadsheet', 'test.csv'])
# Verify that the item and identifier columns are not in the PUT request headers
putCalls = [c for c in rsps.calls if c.request.method == 'PUT']
assert len(putCalls) == 1
assert 'x-archive-meta00-identifier' not in putCalls[0].request.headers
assert 'x-archive-meta00-item' not in putCalls[0].request.headers
out, err = capsys.readouterr()
assert 'uploading test.txt' in err
def test_ia_upload_spreadsheet_missing_identifier(tmpdir_ch, capsys, caplog):
with open('test.txt', 'w') as fh:
fh.write('foo')
with open('test.csv', 'w') as fh:
fh.write('file\n')
fh.write('test.txt\n')
ia_call(['ia', 'upload', '--spreadsheet', 'test.csv'], expected_exit_code=1)
assert 'error: no identifier column on spreadsheet.' in capsys.readouterr().err
def test_ia_upload_spreadsheet_empty_identifier(tmpdir_ch, capsys, caplog):
with open('test.txt', 'w') as fh:
fh.write('foo')
with open('test.csv', 'w') as fh:
fh.write('identifier,file\n')
fh.write(',test.txt\n')
ia_call(['ia', 'upload', '--spreadsheet', 'test.csv'], expected_exit_code=1)
assert 'error: no identifier column on spreadsheet.' in capsys.readouterr().err
def test_ia_upload_spreadsheet_bom(tmpdir_ch, capsys):
with open('test.txt', 'w') as fh:
fh.write('foo')
with open('test.csv', 'wb') as fh:
fh.write(b'\xef\xbb\xbf')
fh.write(b'identifier,file\n')
fh.write(b'nasa,test.txt\n')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', 'upload', '--spreadsheet', 'test.csv'])
out, err = capsys.readouterr()
assert 'uploading test.txt' in err
def test_ia_upload_checksum(tmpdir_ch, caplog):
with open('test.txt', 'w') as fh:
fh.write('foo')
# First upload, file not in metadata yet
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', 'nasa', 'test.txt', '--checksum'])
assert f'uploaded test.txt to {PROTOCOL}//s3.us.archive.org/nasa/test.txt' in caplog.text
caplog.clear()
# Second upload with file in metadata
def insert_test_txt(body):
body = json.loads(body)
body['files'].append({'name': 'test.txt', 'md5': 'acbd18db4cc2f85cedef654fccc4a4d8'})
return json.dumps(body)
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa', transform_body=insert_test_txt)
ia_call(['ia', '--log', 'upload', 'nasa', 'test.txt', '--checksum'], expected_exit_code=1)
assert f'test.txt already exists: {PROTOCOL}//s3.us.archive.org/nasa/test.txt' in caplog.text
caplog.clear()
# Second upload with spreadsheet
with open('test.csv', 'w') as fh:
fh.write('identifier,file\n')
fh.write('nasa,test.txt\n')
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa', transform_body=insert_test_txt)
ia_call(['ia', '--log', 'upload', '--spreadsheet', 'test.csv', '--checksum'],
expected_exit_code=1)
assert f'test.txt already exists: {PROTOCOL}//s3.us.archive.org/nasa/test.txt' in caplog.text
def test_ia_upload_keep_directories(tmpdir_ch, caplog):
os.mkdir('foo')
with open('foo/test.txt', 'w') as fh:
fh.write('foo')
with open('test.csv', 'w') as fh:
fh.write('identifier,file\n')
fh.write('nasa,foo/test.txt\n')
# Default behaviour
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', 'nasa', 'foo/test.txt'])
assert f'uploaded test.txt to {PROTOCOL}//s3.us.archive.org/nasa/test.txt' in caplog.text
caplog.clear()
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', '--spreadsheet', 'test.csv'])
assert f'uploaded test.txt to {PROTOCOL}//s3.us.archive.org/nasa/test.txt' in caplog.text
caplog.clear()
# With the option
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/foo/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', 'nasa', 'foo/test.txt', '--keep-directories'])
assert f'uploaded foo/test.txt to {PROTOCOL}//s3.us.archive.org/nasa/foo/test.txt' in caplog.text
caplog.clear()
with IaRequestsMock() as rsps:
rsps.add_metadata_mock('nasa')
rsps.add(responses.PUT, f'{PROTOCOL}//s3.us.archive.org/nasa/foo/test.txt',
body='',
content_type='text/plain')
ia_call(['ia', '--log', 'upload', '--spreadsheet', 'test.csv', '--keep-directories'])
assert f'uploaded foo/test.txt to {PROTOCOL}//s3.us.archive.org/nasa/foo/test.txt' in caplog.text
|