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