@@ -874,7 +874,7 @@ def test_list_buckets_all_arguments(self):
874
874
uri_parts = urlparse (requested_url )
875
875
self .assertEqual (parse_qs (uri_parts .query ), expected_query )
876
876
877
- def test_page_empty_response (self ):
877
+ def test_list_buckets_page_empty_response (self ):
878
878
from google .api_core import page_iterator
879
879
880
880
project = "PROJECT"
@@ -885,7 +885,7 @@ def test_page_empty_response(self):
885
885
iterator ._page = page
886
886
self .assertEqual (list (page ), [])
887
887
888
- def test_page_non_empty_response (self ):
888
+ def test_list_buckets_page_non_empty_response (self ):
889
889
import six
890
890
from google .cloud .storage .bucket import Bucket
891
891
@@ -908,3 +908,138 @@ def dummy_response():
908
908
self .assertEqual (page .remaining , 0 )
909
909
self .assertIsInstance (bucket , Bucket )
910
910
self .assertEqual (bucket .name , blob_name )
911
+
912
+ def test_create_hmac_key (self ):
913
+ import datetime
914
+ from pytz import UTC
915
+ from six .moves .urllib .parse import urlencode
916
+ from google .cloud .storage .hmac_key import HMACKeyMetadata
917
+
918
+ PROJECT = "PROJECT"
919
+ ACCESS_ID = "ACCESS-ID"
920
+ CREDENTIALS = _make_credentials ()
921
+
922
+ SECRET = "a" * 40
923
+ now = datetime .datetime .utcnow ().replace (tzinfo = UTC )
924
+ now_stamp = "{}Z" .format (now .isoformat ())
925
+ RESOURCE = {
926
+ "kind" : "storage#hmacKey" ,
927
+ "metadata" : {
928
+ "accessId" : ACCESS_ID ,
929
+ "etag" : "ETAG" ,
930
+ "id" : "projects/{}/hmacKeys/{}" .format (PROJECT , ACCESS_ID ),
931
+ "project" : PROJECT ,
932
+ "state" : "ACTIVE" ,
933
+ "serviceAccountEmail" : EMAIL ,
934
+ "timeCreated" : now_stamp ,
935
+ "updated" : now_stamp ,
936
+ },
937
+ "secret" : SECRET ,
938
+ }
939
+
940
+ client = self ._make_one (project = PROJECT , credentials = CREDENTIALS )
941
+ http = _make_requests_session ([_make_json_response (RESOURCE )])
942
+ client ._http_internal = http
943
+
944
+ metadata , secret = client .create_hmac_key (service_account_email = EMAIL )
945
+
946
+ self .assertIsInstance (metadata , HMACKeyMetadata )
947
+ self .assertIs (metadata ._client , client )
948
+ self .assertEqual (metadata ._properties , RESOURCE ["metadata" ])
949
+ self .assertEqual (secret , RESOURCE ["secret" ])
950
+
951
+ URI = "/" .join (
952
+ [
953
+ client ._connection .API_BASE_URL ,
954
+ "storage" ,
955
+ client ._connection .API_VERSION ,
956
+ "projects/%s/hmacKeys" ,
957
+ ]
958
+ )
959
+ QS_PARAMS = {"serviceAccountEmail" : EMAIL }
960
+ FULL_URI = "{}?{}" .format (URI , urlencode (QS_PARAMS ))
961
+ http .request .assert_called_once_with (
962
+ method = "POST" , url = FULL_URI , data = None , headers = mock .ANY
963
+ )
964
+
965
+ def test_list_hmac_keys_defaults_empty (self ):
966
+ PROJECT = "PROJECT"
967
+ CREDENTIALS = _make_credentials ()
968
+ client = self ._make_one (project = PROJECT , credentials = CREDENTIALS )
969
+
970
+ http = _make_requests_session ([_make_json_response ({})])
971
+ client ._http_internal = http
972
+
973
+ metadatas = list (client .list_hmac_keys ())
974
+
975
+ self .assertEqual (len (metadatas ), 0 )
976
+
977
+ URI = "/" .join (
978
+ [
979
+ client ._connection .API_BASE_URL ,
980
+ "storage" ,
981
+ client ._connection .API_VERSION ,
982
+ "projects/%s/hmacKeys" ,
983
+ ]
984
+ )
985
+ http .request .assert_called_once_with (
986
+ method = "GET" , url = URI , data = None , headers = mock .ANY
987
+ )
988
+
989
+ def test_list_hmac_keys_explicit_non_empty (self ):
990
+ from six .moves .urllib .parse import urlencode
991
+ from google .cloud .storage .hmac_key import HMACKeyMetadata
992
+
993
+ PROJECT = "PROJECT"
994
+ MAX_RESULTS = 3
995
+
996
+ ACCESS_ID = "ACCESS-ID"
997
+ CREDENTIALS = _make_credentials ()
998
+ client = self ._make_one (project = PROJECT , credentials = CREDENTIALS )
999
+
1000
+ response = {
1001
+ "kind" : "storage#hmacKeysMetadata" ,
1002
+ "items" : [
1003
+ {
1004
+ "kind" : "storage#hmacKeyMetadata" ,
1005
+ "accessId" : ACCESS_ID ,
1006
+ "serviceAccountEmail" : EMAIL ,
1007
+ }
1008
+ ],
1009
+ }
1010
+
1011
+ http = _make_requests_session ([_make_json_response (response )])
1012
+ client ._http_internal = http
1013
+
1014
+ metadatas = list (
1015
+ client .list_hmac_keys (
1016
+ max_results = MAX_RESULTS ,
1017
+ service_account_email = EMAIL ,
1018
+ show_deleted_keys = True ,
1019
+ )
1020
+ )
1021
+
1022
+ self .assertEqual (len (metadatas ), len (response ["items" ]))
1023
+
1024
+ for metadata , resource in zip (metadatas , response ["items" ]):
1025
+ self .assertIsInstance (metadata , HMACKeyMetadata )
1026
+ self .assertIs (metadata ._client , client )
1027
+ self .assertEqual (metadata ._properties , resource )
1028
+
1029
+ URI = "/" .join (
1030
+ [
1031
+ client ._connection .API_BASE_URL ,
1032
+ "storage" ,
1033
+ client ._connection .API_VERSION ,
1034
+ "projects/%s/hmacKeys" ,
1035
+ ]
1036
+ )
1037
+ QS_PARAMS = {
1038
+ "maxResults" : MAX_RESULTS ,
1039
+ "serviceAccountEmail" : EMAIL ,
1040
+ "showDeletedKeys" : True ,
1041
+ }
1042
+ FULL_URI = "{}?{}" .format (URI , urlencode (QS_PARAMS ))
1043
+ http .request .assert_called_once_with (
1044
+ method = "GET" , url = FULL_URI , data = None , headers = mock .ANY
1045
+ )
0 commit comments