@@ -2048,6 +2048,114 @@ def test_generate_upload_policy_bad_credentials(self):
20482048 with self .assertRaises (AttributeError ):
20492049 bucket .generate_upload_policy ([])
20502050
2051+ def test_lock_retention_policy_no_policy_set (self ):
2052+ credentials = object ()
2053+ connection = _Connection ()
2054+ connection .credentials = credentials
2055+ client = _Client (connection )
2056+ name = 'name'
2057+ bucket = self ._make_one (client = client , name = name )
2058+ bucket ._properties ['metageneration' ] = 1234
2059+
2060+ with self .assertRaises (ValueError ):
2061+ bucket .lock_retention_policy ()
2062+
2063+ def test_lock_retention_policy_no_metageneration (self ):
2064+ credentials = object ()
2065+ connection = _Connection ()
2066+ connection .credentials = credentials
2067+ client = _Client (connection )
2068+ name = 'name'
2069+ bucket = self ._make_one (client = client , name = name )
2070+ bucket ._properties ['retentionPolicy' ] = {
2071+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2072+ 'retentionPeriod' : 86400 * 100 , # 100 days
2073+ }
2074+
2075+ with self .assertRaises (ValueError ):
2076+ bucket .lock_retention_policy ()
2077+
2078+ def test_lock_retention_policy_already_locked (self ):
2079+ credentials = object ()
2080+ connection = _Connection ()
2081+ connection .credentials = credentials
2082+ client = _Client (connection )
2083+ name = 'name'
2084+ bucket = self ._make_one (client = client , name = name )
2085+ bucket ._properties ['metageneration' ] = 1234
2086+ bucket ._properties ['retentionPolicy' ] = {
2087+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2088+ 'isLocked' : True ,
2089+ 'retentionPeriod' : 86400 * 100 , # 100 days
2090+ }
2091+
2092+ with self .assertRaises (ValueError ):
2093+ bucket .lock_retention_policy ()
2094+
2095+ def test_lock_retention_policy_ok (self ):
2096+ name = 'name'
2097+ response = {
2098+ 'name' : name ,
2099+ 'metageneration' : 1235 ,
2100+ 'retentionPolicy' : {
2101+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2102+ 'isLocked' : True ,
2103+ 'retentionPeriod' : 86400 * 100 , # 100 days
2104+ },
2105+ }
2106+ credentials = object ()
2107+ connection = _Connection (response )
2108+ connection .credentials = credentials
2109+ client = _Client (connection )
2110+ bucket = self ._make_one (client = client , name = name )
2111+ bucket ._properties ['metageneration' ] = 1234
2112+ bucket ._properties ['retentionPolicy' ] = {
2113+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2114+ 'retentionPeriod' : 86400 * 100 , # 100 days
2115+ }
2116+
2117+ bucket .lock_retention_policy ()
2118+
2119+ kw , = connection ._requested
2120+ self .assertEqual (kw ['method' ], 'POST' )
2121+ self .assertEqual (kw ['path' ], '/b/{}/lockRetentionPolicy' .format (name ))
2122+ self .assertEqual (kw ['query_params' ], {'metageneration' : 1234 })
2123+
2124+ def test_lock_retention_policy_w_user_project (self ):
2125+ name = 'name'
2126+ user_project = 'user-project-123'
2127+ response = {
2128+ 'name' : name ,
2129+ 'metageneration' : 1235 ,
2130+ 'retentionPolicy' : {
2131+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2132+ 'isLocked' : True ,
2133+ 'retentionPeriod' : 86400 * 100 , # 100 days
2134+ },
2135+ }
2136+ credentials = object ()
2137+ connection = _Connection (response )
2138+ connection .credentials = credentials
2139+ client = _Client (connection )
2140+ bucket = self ._make_one (
2141+ client = client , name = name , user_project = user_project )
2142+ bucket ._properties ['metageneration' ] = 1234
2143+ bucket ._properties ['retentionPolicy' ] = {
2144+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2145+ 'retentionPeriod' : 86400 * 100 , # 100 days
2146+ }
2147+
2148+ bucket .lock_retention_policy ()
2149+
2150+ kw , = connection ._requested
2151+ self .assertEqual (kw ['method' ], 'POST' )
2152+ self .assertEqual (kw ['path' ], '/b/{}/lockRetentionPolicy' .format (name ))
2153+ self .assertEqual (
2154+ kw ['query_params' ], {
2155+ 'metageneration' : 1234 ,
2156+ 'userProject' : user_project ,
2157+ })
2158+
20512159
20522160class _Connection (object ):
20532161 _delete_bucket = False
0 commit comments