def test_download_check_only_with_bad_hash(self, mock_hashlib,
                                               mock_requests, mock_logging,
                                               mock_os):
        verified = {}
        locks = {'myrelpath': {'checksum': '77badhash890'}}
        mocked_open = mock_open()
        mock_os.path.isfile.return_value = True
        mock_os.path.relpath.return_value = 'myrelpath'
        mock_hashlib.sha1.return_value.hexdigest.return_value = '00myhash123'
        mocked_open = mock_open()
        with patch("cwldep.open", mocked_open):
            cwldep.download(tgt="myfile.cwl",
                            url="someurl",
                            version="1",
                            locks=locks,
                            verified=verified,
                            check_only=True)

        mocked_open.assert_called_with('myfile.cwl_download_', 'wb')
        mock_os.remove.assert_called_with('myfile.cwl_download_')
        mock_os.rename.assert_not_called()

        mock_logging.info.assert_called_with('Fetching %s to %s', 'someurl',
                                             'myrelpath')
        mock_logging.warn.assert_called_with('Upstream has changed: %s',
                                             'myrelpath')
        mock_requests.get.assert_called_with('someurl', stream=True)
    def test_download_replace_with_bad_hash(self, mock_hashlib, mock_requests,
                                            mock_logging, mock_os):
        verified = {}
        locks = {'myrelpath': {'checksum': '77badhash890'}}
        mocked_open = mock_open()
        mock_os.path.isfile.return_value = True
        mock_os.path.relpath.return_value = 'myrelpath'
        mock_hashlib.sha1.return_value.hexdigest.return_value = '00myhash123'
        mock_requests.get.return_value.__enter__.return_value.iter_content.return_value = [
            'cwlVersion: v1.0'
        ]
        mocked_open = mock_open()

        with patch("cwldep.open", mocked_open):
            cwldep.download(tgt="myfile.cwl",
                            url="someurl",
                            version="1",
                            locks=locks,
                            verified=verified,
                            check_only=False)
        mocked_open.return_value.write.assert_called_with('cwlVersion: v1.0')

        mock_logging.info.assert_called_with('Fetching %s to %s', 'someurl',
                                             'myrelpath')
        mock_logging.warn.assert_called_with('Upstream has changed: %s',
                                             'myrelpath')
        mock_requests.get.assert_called_with('someurl', stream=True)

        mock_os.rename.assert_called_with('myfile.cwl_download_', 'myfile.cwl')
        self.assertEqual(verified['myrelpath']['checksum'], '00myhash123')
        self.assertEqual(verified['myrelpath']['installed_to'], ['myrelpath'])
 def test_download_check_only_not_a_file(self, mock_requests, mock_logging,
                                         mock_os):
     verified = {}
     locks = {}
     mocked_open = mock_open()
     mock_os.path.isfile.return_value = False
     mock_os.path.relpath.return_value = 'myrelpath'
     cwldep.download(tgt="myfile.cwl",
                     url="someurl",
                     version="1",
                     locks=locks,
                     verified=verified,
                     check_only=True)
     mock_logging.info.assert_called_with('Fetching %s to %s', 'someurl',
                                          'myrelpath')
     mock_requests.get.assert_not_called()