@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_filename_w_key(self, fake_session_factory):
    import os
    import time
    from google.cloud._testing import _NamedTemporaryFile
    blob_name = 'blob-name'
    fake_session_factory.return_value = self._mock_download_transport()
    client = mock.Mock(_credentials=_make_credentials(), spec=['_credentials'])
    bucket = _Bucket(client)
    media_link = 'http://example.com/media/'
    properties = {'mediaLink': media_link, 'updated': '2014-12-06T13:13:50.690Z'}
    key = b'EXAMPLE_KEY'
    blob = self._make_one(blob_name, bucket=bucket, properties=properties, encryption_key=key)
    blob._CHUNK_SIZE_MULTIPLE = 1
    blob.chunk_size = 3
    with _NamedTemporaryFile() as temp:
        blob.download_to_filename(temp.name)
        with open(temp.name, 'rb') as file_obj:
            wrote = file_obj.read()
            mtime = os.path.getmtime(temp.name)
            updated_time = time.mktime(blob.updated.timetuple())
    self.assertEqual(wrote, b'abcdef')
    self.assertEqual(mtime, updated_time)
    header_key_value = 'EXAMPLE_KEY'
    header_key_hash_value = 'EXAMPLE_KEY'
    key_headers = {'X-Goog-Encryption-Key-Sha256': header_key_hash_value, 'X-Goog-Encryption-Algorithm': 'AES256', 'X-Goog-Encryption-Key': header_key_value}
    self._check_session_mocks(client, fake_session_factory, media_link, headers=key_headers)
@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_filename_w_key(self, fake_session_factory):
    import os
    import time
    from google.cloud._testing import _NamedTemporaryFile
    blob_name = 'blob-name'
    fake_session_factory.return_value = self._mock_download_transport()
    client = mock.Mock(_credentials=_make_credentials(), spec=['_credentials'])
    bucket = _Bucket(client)
    media_link = 'http://example.com/media/'
    properties = {'mediaLink': media_link, 'updated': '2014-12-06T13:13:50.690Z'}
    key = b'EXAMPLE_KEY'
    blob = self._make_one(blob_name, bucket=bucket, properties=properties, encryption_key=key)
    blob._CHUNK_SIZE_MULTIPLE = 1
    blob.chunk_size = 3
    with _NamedTemporaryFile() as temp:
        blob.download_to_filename(temp.name)
        with open(temp.name, 'rb') as file_obj:
            wrote = file_obj.read()
            mtime = os.path.getmtime(temp.name)
            updated_time = time.mktime(blob.updated.timetuple())
    self.assertEqual(wrote, b'abcdef')
    self.assertEqual(mtime, updated_time)
    header_key_value = 'EXAMPLE_KEY'
    header_key_hash_value = 'EXAMPLE_KEY'
    key_headers = {'X-Goog-Encryption-Key-Sha256': header_key_hash_value, 'X-Goog-Encryption-Algorithm': 'AES256', 'X-Goog-Encryption-Key': header_key_value}
    self._check_session_mocks(client, fake_session_factory, media_link, headers=key_headers)