@@ -2414,6 +2414,114 @@ def test_generate_upload_policy_bad_credentials(self):
24142414 with self .assertRaises (AttributeError ):
24152415 bucket .generate_upload_policy ([])
24162416
2417+ def test_lock_retention_policy_no_policy_set (self ):
2418+ credentials = object ()
2419+ connection = _Connection ()
2420+ connection .credentials = credentials
2421+ client = _Client (connection )
2422+ name = 'name'
2423+ bucket = self ._make_one (client = client , name = name )
2424+ bucket ._properties ['metageneration' ] = 1234
2425+
2426+ with self .assertRaises (ValueError ):
2427+ bucket .lock_retention_policy ()
2428+
2429+ def test_lock_retention_policy_no_metageneration (self ):
2430+ credentials = object ()
2431+ connection = _Connection ()
2432+ connection .credentials = credentials
2433+ client = _Client (connection )
2434+ name = 'name'
2435+ bucket = self ._make_one (client = client , name = name )
2436+ bucket ._properties ['retentionPolicy' ] = {
2437+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2438+ 'retentionPeriod' : 86400 * 100 , # 100 days
2439+ }
2440+
2441+ with self .assertRaises (ValueError ):
2442+ bucket .lock_retention_policy ()
2443+
2444+ def test_lock_retention_policy_already_locked (self ):
2445+ credentials = object ()
2446+ connection = _Connection ()
2447+ connection .credentials = credentials
2448+ client = _Client (connection )
2449+ name = 'name'
2450+ bucket = self ._make_one (client = client , name = name )
2451+ bucket ._properties ['metageneration' ] = 1234
2452+ bucket ._properties ['retentionPolicy' ] = {
2453+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2454+ 'isLocked' : True ,
2455+ 'retentionPeriod' : 86400 * 100 , # 100 days
2456+ }
2457+
2458+ with self .assertRaises (ValueError ):
2459+ bucket .lock_retention_policy ()
2460+
2461+ def test_lock_retention_policy_ok (self ):
2462+ name = 'name'
2463+ response = {
2464+ 'name' : name ,
2465+ 'metageneration' : 1235 ,
2466+ 'retentionPolicy' : {
2467+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2468+ 'isLocked' : True ,
2469+ 'retentionPeriod' : 86400 * 100 , # 100 days
2470+ },
2471+ }
2472+ credentials = object ()
2473+ connection = _Connection (response )
2474+ connection .credentials = credentials
2475+ client = _Client (connection )
2476+ bucket = self ._make_one (client = client , name = name )
2477+ bucket ._properties ['metageneration' ] = 1234
2478+ bucket ._properties ['retentionPolicy' ] = {
2479+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2480+ 'retentionPeriod' : 86400 * 100 , # 100 days
2481+ }
2482+
2483+ bucket .lock_retention_policy ()
2484+
2485+ kw , = connection ._requested
2486+ self .assertEqual (kw ['method' ], 'POST' )
2487+ self .assertEqual (kw ['path' ], '/b/{}/lockRetentionPolicy' .format (name ))
2488+ self .assertEqual (kw ['query_params' ], {'metageneration' : 1234 })
2489+
2490+ def test_lock_retention_policy_w_user_project (self ):
2491+ name = 'name'
2492+ user_project = 'user-project-123'
2493+ response = {
2494+ 'name' : name ,
2495+ 'metageneration' : 1235 ,
2496+ 'retentionPolicy' : {
2497+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2498+ 'isLocked' : True ,
2499+ 'retentionPeriod' : 86400 * 100 , # 100 days
2500+ },
2501+ }
2502+ credentials = object ()
2503+ connection = _Connection (response )
2504+ connection .credentials = credentials
2505+ client = _Client (connection )
2506+ bucket = self ._make_one (
2507+ client = client , name = name , user_project = user_project )
2508+ bucket ._properties ['metageneration' ] = 1234
2509+ bucket ._properties ['retentionPolicy' ] = {
2510+ 'effectiveTime' : '2018-03-01T16:46:27.123456Z' ,
2511+ 'retentionPeriod' : 86400 * 100 , # 100 days
2512+ }
2513+
2514+ bucket .lock_retention_policy ()
2515+
2516+ kw , = connection ._requested
2517+ self .assertEqual (kw ['method' ], 'POST' )
2518+ self .assertEqual (kw ['path' ], '/b/{}/lockRetentionPolicy' .format (name ))
2519+ self .assertEqual (
2520+ kw ['query_params' ], {
2521+ 'metageneration' : 1234 ,
2522+ 'userProject' : user_project ,
2523+ })
2524+
24172525
24182526class _Connection (object ):
24192527 _delete_bucket = False
0 commit comments