1616import tempfile
1717
1818from google .cloud import storage
19+ from google .cloud .storage import Blob
1920import pytest
2021
2122import encryption
2223
24+
2325TEST_ENCRYPTION_KEY = 'brtJUWneL92g5q0N2gyDSnlPSYAiIVZ/cWgjyZNeMy0='
2426TEST_ENCRYPTION_KEY_DECODED = base64 .b64decode (TEST_ENCRYPTION_KEY )
2527
28+ TEST_ENCRYPTION_KEY_2 = 'o4OD7SWCaPjfeEGhAY+YCgMdY9UW+OJ8mvfWD9lNtO4='
29+ TEST_ENCRYPTION_KEY_2_DECODED = base64 .b64decode (TEST_ENCRYPTION_KEY_2 )
30+
2631
2732def test_generate_encryption_key (capsys ):
2833 encryption .generate_encryption_key ()
@@ -47,9 +52,9 @@ def test_upload_encrypted_blob(cloud_config):
4752def test_blob (cloud_config ):
4853 """Provides a pre-existing blob in the test bucket."""
4954 bucket = storage .Client ().bucket (cloud_config .storage_bucket )
50- blob = bucket .blob ('encryption_test_sigil' )
55+ blob = Blob ('encryption_test_sigil' ,
56+ bucket , encryption_key = TEST_ENCRYPTION_KEY_DECODED )
5157 content = 'Hello, is it me you\' re looking for?'
52- blob .encryption_key = TEST_ENCRYPTION_KEY_DECODED
5358 blob .upload_from_string (content )
5459 return blob .name , content
5560
@@ -65,3 +70,22 @@ def test_download_blob(test_blob, cloud_config):
6570
6671 downloaded_content = dest_file .read ().decode ('utf-8' )
6772 assert downloaded_content == test_blob_content
73+
74+
75+ def test_rotate_encryption_key (test_blob , cloud_config ):
76+ test_blob_name , test_blob_content = test_blob
77+ encryption .rotate_encryption_key (
78+ cloud_config .storage_bucket ,
79+ test_blob_name ,
80+ TEST_ENCRYPTION_KEY ,
81+ TEST_ENCRYPTION_KEY_2 )
82+
83+ with tempfile .NamedTemporaryFile () as dest_file :
84+ encryption .download_encrypted_blob (
85+ cloud_config .storage_bucket ,
86+ test_blob_name ,
87+ dest_file .name ,
88+ TEST_ENCRYPTION_KEY_2 )
89+
90+ downloaded_content = dest_file .read ().decode ('utf-8' )
91+ assert downloaded_content == test_blob_content
0 commit comments