def test_upload_file_remote_info_not_none_valid(self): with my_vcr.use_cassette('ohapi/cassettes/test_upload_file_' + 'remote_info_not_none_valid.yaml') as cass: with patch('%s.open' % __name__, mock_open(), create=True): with patch('ohapi.api.open', mock_open(), create=True): try: filename = 'foo' def fake_stat(arg): if arg == filename: faked = list(orig_os_stat('/tmp')) faked[stat.ST_SIZE] = len('some stuff') return stat_result(faked) else: return orig_os_stat(arg) orig_os_stat = os.stat os.stat = fake_stat with open('foo', 'w') as h: h.write('some stuff') upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertEqual(cass.responses[0][ "status"]["code"], 200) self.assertEqual(cass.responses[1][ "status"]["code"], 201) self.assertEqual(cass.responses[1]["body"]["string"] .decode('utf-8'), '{"id": "file_id"}') finally: os.stat = orig_os_stat
def test_upload_file_remote_info_not_none_valid(self): """ Test assumes remote_file_info['download_url'] matches 'lorum_ipsum.txt' """ with my_vcr.use_cassette('ohapi/cassettes/test_upload_file_' + 'remote_info_not_none_valid.yaml') as cass: upload_file(target_filepath=TARGET_FILEPATH, metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertEqual(cass.responses[0]["status"]["code"], 200) self.assertEqual(cass.responses[0]["headers"]["Content-Length"], ['446'])
def test_upload_file_empty_remote_info_not_none(self): with my_vcr.use_cassette('ohapi/cassettes/test_upload_file_empty_' + 'remote_info_not_none.yaml') as cass: with self.assertRaises(Exception): with patch('ohapi.api.open', mock_open(), create=True): upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertEqual(cass.responses[0]["status"]["code"], 200) self.assertEqual( cass.responses[1]["body"]["string"].decode('utf-8'), '{"data_file": ["The submitted file is' + ' empty."]}')
def test_upload_file_remote_info_not_none_expired_access_token(self): with my_vcr.use_cassette('ohapi/cassettes/test_upload_file_remote_' + 'info_not_none_expired_access_' + 'token.yaml') as cass: with self.assertRaises(Exception): with patch('ohapi.api.open', mock_open(), create=True): upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN_EXPIRED, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertEqual(cass.responses[0]["status"]["code"], 200) self.assertEqual( cass.responses[1]["body"]["string"].decode('utf-8'), '{"detail": "Expired token."}')
def test_upload_file_remote_info_not_none_invalid_metadata(self): with my_vcr.use_cassette('ohapi/cassettes/test_upload_file_remote_in' + 'fo_not_none_invalid_metadata.yaml') as cass: with self.assertRaises(Exception): with patch('ohapi.api.open', mock_open(), create=True): upload_file(target_filepath='foo', metadata=FILE_METADATA_INVALID, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertEqual(cass.responses[0]["status"]["code"], 200) self.assertEqual( cass.responses[1]["body"]["string"].decode('utf-8'), '{"metadata":["\\"description\\" is a ' + 'required field of the metadata"]}')
def test_upload_file_empty(self): with self.assertRaises(Exception): with patch('%s.open' % __name__, mock_open(), create=True): with patch('ohapi.api.open', mock_open(), create=True): try: filename = 'foo' def fake_stat(arg): if arg == filename: faked = list(orig_os_stat('/tmp')) faked[stat.ST_SIZE] = len('some stuff') return stat_result(faked) else: return orig_os_stat(arg) orig_os_stat = os.stat os.stat = fake_stat with open('foo', 'w') as h: h.write('') response = upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1) assert response.json() == { "data_file": ["The submitted file is empty."]} finally: os.stat = orig_os_stat
def test_upload_file_remote_info_not_none_invalid_access_token(self): with my_vcr.use_cassette('ohapi/cassettes/test_upload_file_remote' + '_info_not_none_invalid_access_' + 'token.yaml') as cass: with patch('__main__.open', mock_open(), create=True): with open('foo', 'w') as h: h.write('some stuff') upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN_INVALID, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertEqual(cass.responses[0]["status"]["code"], 200) self.assertEqual( cass.responses[1]["body"]["string"].decode('utf-8'), '{"detail": "Invalid token."}')
def test_upload_valid_file_valid_access_token(self): with patch('%s.open' % __name__, mock_open(), create=True): with patch('ohapi.api.open', mock_open(), create=True): try: filename = 'foo' def fake_stat(arg): if arg == filename: faked = list(orig_os_stat('/tmp')) faked[stat.ST_SIZE] = len('some stuff') return stat_result(faked) else: return orig_os_stat(arg) orig_os_stat = os.stat os.stat = fake_stat with open('foo', 'w') as h: h.write('some stuff') response = upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1) self.assertEqual(response.status_code, 201) assert response.json() == {"id": "file_id"} finally: os.stat = orig_os_stat
def test_upload_valid_file_valid_access_token(self): response = upload_file(target_filepath=TARGET_FILEPATH, metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1) self.assertEqual(response.status_code, 200) assert response.json() == {'size': 446, 'status': 'ok'}
def test_upload_file_remote_info_not_none_matching_file_size(self): result = upload_file(target_filepath=TARGET_FILEPATH, metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertRegexpMatches(result, 'remote exists with matching file size')
def test_upload_file_remote_info_not_none_invalid_metadata_with_desc(self): with my_vcr.use_cassette('ohapi/cassettes/test_upload_file_remote_' + 'info_not_none_invalid_metadata_with_' + 'desc.yaml') as cass: with patch('__main__.open', mock_open(), create=True): with open('foo', 'w') as h: h.write('some stuff') upload_file(target_filepath='foo', metadata=FILE_METADATA_INVALID_WITH_DESC, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1, remote_file_info=REMOTE_FILE_INFO) self.assertEqual(cass.responses[0]["status"]["code"], 200) self.assertEqual( cass.responses[1]["body"]["string"].decode('utf-8'), '{"metadata":["\\"tags\\" is a required ' + 'field of the metadata"]}')
def test_upload_file_expired_access_token(self): with self.assertRaises(Exception): with patch('ohapi.api.open', mock_open(), create=True): response = upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN_EXPIRED, project_member_id=VALID_PMI1) assert response.json() == {"detail": "Expired token."}
def test_upload_file_expired_access_token(self): with patch('__main__.open', mock_open(), create=True): with open('foo', 'w') as h: h.write('some stuff') response = upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN_EXPIRED, project_member_id=VALID_PMI1) assert response.json() == {"detail": "Expired token."}
def test_upload_valid_file_valid_access_token(self): with patch('__main__.open', mock_open(), create=True): with open('foo', 'w') as h: h.write('some stuff') response = upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1) self.assertEqual(response.status_code, 201) assert response.json() == {"id": "file_id"}
def test_upload_file_invalid_metadata_without_description(self): with self.assertRaises(Exception): with patch('ohapi.api.open', mock_open(), create=True): response = upload_file(target_filepath='foo', metadata=FILE_METADATA_INVALID, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1) assert response.json() == { "metadata": ["\"description\" is a " + "required field of the metadata"]}
def test_upload_file_empty(self): with patch('__main__.open', mock_open(), create=True): with open('foo', 'w') as h: h.write('') response = upload_file(target_filepath='foo', metadata=FILE_METADATA, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1) assert response.json() == { "data_file": ["The submitted file is empty."] }
def test_upload_file_invalid_metadata_without_description(self): with patch('__main__.open', mock_open(), create=True): with open('foo', 'w') as h: h.write('some stuff') response = upload_file(target_filepath='foo', metadata=FILE_METADATA_INVALID, access_token=ACCESS_TOKEN, project_member_id=VALID_PMI1) assert response.json() == { "metadata": ["\"description\" is a " + "required field of the metadata"] }