def test_rewrite_same_name_no_old_key_new_key_done(self):
    import base64
    import hashlib
    KEY = b'01234567890123456789012345678901'
    KEY_B64 = base64.b64encode(KEY).rstrip().decode('ascii')
    KEY_HASH = hashlib.sha256(KEY).digest()
    KEY_HASH_B64 = base64.b64encode(KEY_HASH).rstrip().decode('ascii')
    BLOB_NAME = 'blob'
    RESPONSE = {'totalBytesRewritten': 42, 'objectSize': 42, 'done': True, 'resource': {'etag': 'DEADBEEF'}}
    response = ({'status': http_client.OK}, RESPONSE)
    connection = _Connection(response)
    client = _Client(connection)
    bucket = _Bucket(client=client)
    plain = self._make_one(BLOB_NAME, bucket=bucket)
    encrypted = self._make_one(BLOB_NAME, bucket=bucket, encryption_key=KEY)
    (token, rewritten, size) = encrypted.rewrite(plain)
    self.assertIsNone(token)
    self.assertEqual(rewritten, 42)
    self.assertEqual(size, 42)
    kw = connection._requested
    self.assertEqual(len(kw), 1)
    self.assertEqual(kw[0]['method'], 'POST')
    PATH = ('/b/name/o/%s/rewriteTo/b/name/o/%s' % (BLOB_NAME, BLOB_NAME))
    self.assertEqual(kw[0]['path'], PATH)
    self.assertEqual(kw[0]['query_params'], {})
    SENT = {}
    self.assertEqual(kw[0]['data'], SENT)
    headers = {key.title(): str(value) for (key, value) in kw[0]['headers'].items()}
    self.assertNotIn('X-Goog-Copy-Source-Encryption-Algorithm', headers)
    self.assertNotIn('X-Goog-Copy-Source-Encryption-Key', headers)
    self.assertNotIn('X-Goog-Copy-Source-Encryption-Key-Sha256', headers)
    self.assertEqual(headers['X-Goog-Encryption-Algorithm'], 'AES256')
    self.assertEqual(headers['X-Goog-Encryption-Key'], KEY_B64)
    self.assertEqual(headers['X-Goog-Encryption-Key-Sha256'], KEY_HASH_B64)
def test_rewrite_same_name_no_old_key_new_key_done(self):
    import base64
    import hashlib
    KEY = b'01234567890123456789012345678901'
    KEY_B64 = base64.b64encode(KEY).rstrip().decode('ascii')
    KEY_HASH = hashlib.sha256(KEY).digest()
    KEY_HASH_B64 = base64.b64encode(KEY_HASH).rstrip().decode('ascii')
    BLOB_NAME = 'blob'
    RESPONSE = {'totalBytesRewritten': 42, 'objectSize': 42, 'done': True, 'resource': {'etag': 'DEADBEEF'}}
    response = ({'status': http_client.OK}, RESPONSE)
    connection = _Connection(response)
    client = _Client(connection)
    bucket = _Bucket(client=client)
    plain = self._make_one(BLOB_NAME, bucket=bucket)
    encrypted = self._make_one(BLOB_NAME, bucket=bucket, encryption_key=KEY)
    (token, rewritten, size) = encrypted.rewrite(plain)
    self.assertIsNone(token)
    self.assertEqual(rewritten, 42)
    self.assertEqual(size, 42)
    kw = connection._requested
    self.assertEqual(len(kw), 1)
    self.assertEqual(kw[0]['method'], 'POST')
    PATH = ('/b/name/o/%s/rewriteTo/b/name/o/%s' % (BLOB_NAME, BLOB_NAME))
    self.assertEqual(kw[0]['path'], PATH)
    self.assertEqual(kw[0]['query_params'], {})
    SENT = {}
    self.assertEqual(kw[0]['data'], SENT)
    headers = {key.title(): str(value) for (key, value) in kw[0]['headers'].items()}
    self.assertNotIn('X-Goog-Copy-Source-Encryption-Algorithm', headers)
    self.assertNotIn('X-Goog-Copy-Source-Encryption-Key', headers)
    self.assertNotIn('X-Goog-Copy-Source-Encryption-Key-Sha256', headers)
    self.assertEqual(headers['X-Goog-Encryption-Algorithm'], 'AES256')
    self.assertEqual(headers['X-Goog-Encryption-Key'], KEY_B64)
    self.assertEqual(headers['X-Goog-Encryption-Key-Sha256'], KEY_HASH_B64)