def test_get_file_entity_provenance_dict__error_not_404(self):
        self.mock_syn.getProvenance.side_effect = SynapseHTTPError(
            response=Mock(status_code=400))

        assert_raises(SynapseHTTPError,
                      synapseutils.sync._get_file_entity_provenance_dict,
                      self.mock_syn, "syn123")
    def test_get_file_entity_provenance_dict__error_is_404(self):
        self.mock_syn.getProvenance.side_effect = SynapseHTTPError(
            response=Mock(status_code=404))

        result_dict = synapseutils.sync._get_file_entity_provenance_dict(
            self.mock_syn, "syn123")
        assert_dict_equal({}, result_dict)
def test_upload_chunk__expired_url():
    upload_parts = [{
        'uploadPresignedUrl': 'https://www.fake.url/fake/news',
        'partNumber': 420
    }, {
        'uploadPresignedUrl': 'https://www.google.com',
        'partNumber': 421
    }, {
        'uploadPresignedUrl': 'https://rito.pls/',
        'partNumber': 422
    }, {
        'uploadPresignedUrl': 'https://never.lucky.gg',
        'partNumber': 423
    }]

    value_doesnt_matter = None
    expired = Value(c_bool, False)
    mocked_get_chunk_function = MagicMock(side_effect=[1, 2, 3, 4])

    with patch.object(multipart_upload, "_put_chunk",
                      side_effect=SynapseHTTPError("useless message",
                                                   response=MagicMock(status_code=403))) as mocked_put_chunk, \
         patch.object(warnings, "warn") as mocked_warn:

        def chunk_upload(part):
            return _upload_chunk(part,
                                 completed=value_doesnt_matter,
                                 status=value_doesnt_matter,
                                 syn=syn,
                                 filename=value_doesnt_matter,
                                 get_chunk_function=mocked_get_chunk_function,
                                 fileSize=value_doesnt_matter,
                                 partSize=value_doesnt_matter,
                                 t0=value_doesnt_matter,
                                 expired=expired,
                                 bytes_already_uploaded=value_doesnt_matter)

        # 2 threads both with urls that have expired
        mp = Pool(4)
        mp.map(chunk_upload, upload_parts)
        assert_true(expired.value)

        # assert warnings.warn was only called once
        mocked_warn.assert_called_once_with(
            "The pre-signed upload URL has expired. Restarting upload...\n")

        # assert _put_chunk was called at least once
        assert_greater_equal(len(mocked_put_chunk.call_args_list), 1)
def test_upload_chunk__expired_url():
    upload_parts = [{
        'uploadPresignedUrl': 'https://www.fake.url/fake/news',
        'partNumber': 420
    }, {
        'uploadPresignedUrl': 'https://www.google.com',
        'partNumber': 421
    }, {
        'uploadPresignedUrl': 'https://rito.pls/',
        'partNumber': 422
    }, {
        'uploadPresignedUrl': 'https://never.lucky.gg',
        'partNumber': 423
    }]

    with patch.object(multipart_upload, "_put_chunk",
                      side_effect=SynapseHTTPError("useless message",response=MagicMock(status_code=403))) as mocked_put_chunk, \
                      patch.object(warnings, "warn") as mocked_warn, \
                      patch.object(multipart_upload, '_start_multipart_upload',
                        return_value=DictObject({'partsState': '0', 'uploadId': '1', 'state': 'COMPLETED', 'resultFileHandleId': '1'})), \
                      patch.object(multipart_upload, "_get_presigned_urls", return_value=upload_parts):

        file_size = 1 * MB
        filepath = make_bogus_binary_file(n=file_size)

        try:
            multipart_upload.multipart_upload(syn, filepath)
        finally:
            if os.path.isfile(filepath):
                os.remove(filepath)

        mocked_warn.assert_called_with(
            'The pre-signed upload URL has expired. Restarting upload...\n')

        # 4 URLs, 7 retries.
        assert mocked_warn.call_count == 28

        # assert _put_chunk was called at least once
        assert_greater_equal(len(mocked_put_chunk.call_args_list), 1)