|
| 1 | +/* Copyright (C) 2026 NooBaa */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const { VERSIONING } = require('../../../common/constants').S3; |
| 6 | +const SensitiveString = require('../../../util/sensitive_string'); |
| 7 | +const system_utils = require('../../../server/utils/system_utils'); |
| 8 | +const { MDStore } = require('../../../server/object_services/md_store'); |
| 9 | +const object_server = require('../../../server/object_services/object_server'); |
| 10 | + |
| 11 | +describe('object_lock - put_object_retention', () => { |
| 12 | + const system_store = require('../../../server/system_services/system_store').get_instance(); |
| 13 | + |
| 14 | + let mdstore_instance_stub; |
| 15 | + let update_object_by_id_stub; |
| 16 | + const bucket_id = 'bucket_id_retention'; |
| 17 | + const system_id = 'system_id_retention'; |
| 18 | + |
| 19 | + function make_obj({ mode, retain_until_date }) { |
| 20 | + const retain = retain_until_date || new Date(Date.now() + 7 * 24 * 3600 * 1000); |
| 21 | + return { |
| 22 | + _id: { |
| 23 | + toHexString: () => 'objidhex', |
| 24 | + getTimestamp: () => new Date(), |
| 25 | + }, |
| 26 | + system: system_id, |
| 27 | + bucket: bucket_id, |
| 28 | + key: 'locked-key', |
| 29 | + size: 1, |
| 30 | + lock_settings: { |
| 31 | + retention: { |
| 32 | + mode, |
| 33 | + retain_until_date: retain, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + function make_req({ mode, retain_until_date, bypass_governance }) { |
| 40 | + return { |
| 41 | + role: 'admin', |
| 42 | + system: { |
| 43 | + _id: system_id, |
| 44 | + buckets_by_name: { |
| 45 | + 'test-bucket': { |
| 46 | + _id: bucket_id, |
| 47 | + name: 'test-bucket', |
| 48 | + versioning: VERSIONING.ENABLED, |
| 49 | + object_lock_configuration: { object_lock_enabled: 'Enabled' }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + rpc_params: { |
| 54 | + bucket: new SensitiveString('test-bucket'), |
| 55 | + key: 'locked-key', |
| 56 | + bypass_governance, |
| 57 | + retention: { |
| 58 | + mode, |
| 59 | + retain_until_date, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + mdstore_instance_stub = { |
| 67 | + find_object_latest: jest.fn(), |
| 68 | + update_object_by_id: jest.fn().mockResolvedValue(undefined), |
| 69 | + get_object_version_id: jest.fn().mockReturnValue('v1'), |
| 70 | + make_md_id: jest.fn(), |
| 71 | + }; |
| 72 | + update_object_by_id_stub = mdstore_instance_stub.update_object_by_id; |
| 73 | + jest.spyOn(MDStore, 'instance').mockReturnValue(mdstore_instance_stub); |
| 74 | + jest.spyOn(system_utils, 'system_in_maintenance').mockReturnValue(false); |
| 75 | + if (!system_store.data) { |
| 76 | + system_store.data = {}; |
| 77 | + } |
| 78 | + system_store.data.get_by_id = jest.fn().mockReturnValue({ |
| 79 | + name: new SensitiveString('test-bucket'), |
| 80 | + versioning: VERSIONING.ENABLED, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + afterEach(() => { |
| 85 | + jest.restoreAllMocks(); |
| 86 | + }); |
| 87 | + |
| 88 | + test('rejects COMPLIANCE to GOVERNANCE downgrade with same retain date', async () => { |
| 89 | + const retain_until = new Date(Date.now() + 15 * 24 * 3600 * 1000); |
| 90 | + mdstore_instance_stub.find_object_latest.mockResolvedValue( |
| 91 | + make_obj({ mode: 'COMPLIANCE', retain_until_date: retain_until }) |
| 92 | + ); |
| 93 | + const req = make_req({ |
| 94 | + mode: 'GOVERNANCE', |
| 95 | + retain_until_date: retain_until, |
| 96 | + bypass_governance: true, |
| 97 | + }); |
| 98 | + |
| 99 | + await expect(object_server.put_object_retention(req)) |
| 100 | + .rejects.toMatchObject({ rpc_code: 'OBJECT_LOCKED' }); |
| 101 | + expect(update_object_by_id_stub).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + test('rejects COMPLIANCE to GOVERNANCE downgrade with longer retain date', async () => { |
| 105 | + const current_until = new Date(Date.now() + 15 * 24 * 3600 * 1000); |
| 106 | + const longer_until = new Date(Date.now() + 90 * 24 * 3600 * 1000); |
| 107 | + mdstore_instance_stub.find_object_latest.mockResolvedValue( |
| 108 | + make_obj({ mode: 'COMPLIANCE', retain_until_date: current_until }) |
| 109 | + ); |
| 110 | + const req = make_req({ |
| 111 | + mode: 'GOVERNANCE', |
| 112 | + retain_until_date: longer_until, |
| 113 | + bypass_governance: true, |
| 114 | + }); |
| 115 | + |
| 116 | + await expect(object_server.put_object_retention(req)) |
| 117 | + .rejects.toMatchObject({ rpc_code: 'OBJECT_LOCKED' }); |
| 118 | + expect(update_object_by_id_stub).not.toHaveBeenCalled(); |
| 119 | + }); |
| 120 | + |
| 121 | + test('allows GOVERNANCE to COMPLIANCE upgrade with same retain date', async () => { |
| 122 | + const retain_until = new Date(Date.now() + 15 * 24 * 3600 * 1000); |
| 123 | + mdstore_instance_stub.find_object_latest.mockResolvedValue( |
| 124 | + make_obj({ mode: 'GOVERNANCE', retain_until_date: retain_until }) |
| 125 | + ); |
| 126 | + const req = make_req({ |
| 127 | + mode: 'COMPLIANCE', |
| 128 | + retain_until_date: retain_until, |
| 129 | + }); |
| 130 | + |
| 131 | + await expect(object_server.put_object_retention(req)).resolves.toBeUndefined(); |
| 132 | + expect(update_object_by_id_stub).toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + test('allows extending COMPLIANCE retention while keeping COMPLIANCE mode', async () => { |
| 136 | + const current_until = new Date(Date.now() + 15 * 24 * 3600 * 1000); |
| 137 | + const longer_until = new Date(Date.now() + 60 * 24 * 3600 * 1000); |
| 138 | + mdstore_instance_stub.find_object_latest.mockResolvedValue( |
| 139 | + make_obj({ mode: 'COMPLIANCE', retain_until_date: current_until }) |
| 140 | + ); |
| 141 | + const req = make_req({ |
| 142 | + mode: 'COMPLIANCE', |
| 143 | + retain_until_date: longer_until, |
| 144 | + }); |
| 145 | + |
| 146 | + await expect(object_server.put_object_retention(req)).resolves.toBeUndefined(); |
| 147 | + expect(update_object_by_id_stub).toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + |
| 150 | + test('allows setting GOVERNANCE after COMPLIANCE retention has expired', async () => { |
| 151 | + const expired_until = new Date(Date.now() - 24 * 3600 * 1000); |
| 152 | + const new_until = new Date(Date.now() + 7 * 24 * 3600 * 1000); |
| 153 | + mdstore_instance_stub.find_object_latest.mockResolvedValue( |
| 154 | + make_obj({ mode: 'COMPLIANCE', retain_until_date: expired_until }) |
| 155 | + ); |
| 156 | + const req = make_req({ |
| 157 | + mode: 'GOVERNANCE', |
| 158 | + retain_until_date: new_until, |
| 159 | + }); |
| 160 | + |
| 161 | + await expect(object_server.put_object_retention(req)).resolves.toBeUndefined(); |
| 162 | + expect(update_object_by_id_stub).toHaveBeenCalled(); |
| 163 | + }); |
| 164 | + |
| 165 | + test('rejects shortening active COMPLIANCE retention', async () => { |
| 166 | + const current_until = new Date(Date.now() + 15 * 24 * 3600 * 1000); |
| 167 | + const shorter_until = new Date(Date.now() + 1 * 24 * 3600 * 1000); |
| 168 | + mdstore_instance_stub.find_object_latest.mockResolvedValue( |
| 169 | + make_obj({ mode: 'COMPLIANCE', retain_until_date: current_until }) |
| 170 | + ); |
| 171 | + const req = make_req({ |
| 172 | + mode: 'COMPLIANCE', |
| 173 | + retain_until_date: shorter_until, |
| 174 | + bypass_governance: true, |
| 175 | + }); |
| 176 | + |
| 177 | + await expect(object_server.put_object_retention(req)) |
| 178 | + .rejects.toMatchObject({ rpc_code: 'OBJECT_LOCKED' }); |
| 179 | + expect(update_object_by_id_stub).not.toHaveBeenCalled(); |
| 180 | + }); |
| 181 | +}); |
0 commit comments