From 722633d9349a07ac612e48e6899fd3a57842c8fa Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 14 Nov 2024 16:57:26 +0100 Subject: [PATCH] test: delete filtered assets Signed-off-by: David Dal Busco --- src/tests/satellite.storage.spec.ts | 173 +++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 2 deletions(-) diff --git a/src/tests/satellite.storage.spec.ts b/src/tests/satellite.storage.spec.ts index c175cde9..2568847a 100644 --- a/src/tests/satellite.storage.spec.ts +++ b/src/tests/satellite.storage.spec.ts @@ -651,13 +651,14 @@ describe('Satellite storage', () => { }); const user = Ed25519KeyIdentity.generate(); + const user2 = Ed25519KeyIdentity.generate(); describe('user', () => { beforeAll(async () => { - actor.setIdentity(user); - const { set_doc } = actor; + actor.setIdentity(user); + await set_doc('#user', user.getPrincipal().toText(), { data: await toArray({ provider: 'internet_identity' @@ -665,6 +666,16 @@ describe('Satellite storage', () => { description: toNullable(), version: toNullable() }); + + actor.setIdentity(user2); + + await set_doc('#user', user2.getPrincipal().toText(), { + data: await toArray({ + provider: 'internet_identity' + }), + description: toNullable(), + version: toNullable() + }); }); describe.each([{ memory: { Heap: null } }, { memory: { Stable: null } }])( @@ -951,6 +962,164 @@ describe('Satellite storage', () => { expect(countBetween).toBe(5n); }); }); + + describe('delete filtered', () => { + it('should delete assets based on filter criteria', async () => { + const { del_filtered_assets, list_assets } = actor; + + await uploadCustomAsset('asset1.svg'); + await uploadCustomAsset('asset2.svg'); + await uploadCustomAsset('asset3.svg'); + + const filterParams: ListParams = { + matcher: toNullable({ + key: toNullable('/asset2\\.svg$'), + description: toNullable(), + created_at: toNullable(), + updated_at: toNullable() + }), + order: toNullable({ + desc: true, + field: { Keys: null } + }), + owner: toNullable(), + paginate: toNullable() + }; + + const listParams: ListParams = { + matcher: toNullable({ + key: toNullable('/asset\\d+\\.svg$'), + description: toNullable(), + created_at: toNullable(), + updated_at: toNullable() + }), + order: toNullable({ + desc: true, + field: { Keys: null } + }), + owner: toNullable(), + paginate: toNullable() + }; + + await del_filtered_assets(collection, filterParams); + + const remainingAssets = await list_assets(collection, listParams); + + expect(remainingAssets.items.map((item) => item[1].key.full_path)).toEqual([ + `/${collection}/asset3.svg`, + `/${collection}/asset1.svg` + ]); + }); + + it('should prevent user1 from deleting assets of user2', async () => { + const { del_filtered_assets, list_assets } = actor; + + actor.setIdentity(user2); + + await uploadCustomAsset('user2_asset.svg'); + + actor.setIdentity(user); + + const filterParams: ListParams = { + matcher: toNullable({ + key: toNullable('/asset1\\.svg$'), + description: toNullable(), + created_at: toNullable(), + updated_at: toNullable() + }), + order: toNullable({ + desc: true, + field: { Keys: null } + }), + owner: toNullable(), + paginate: toNullable() + }; + + await del_filtered_assets(collection, filterParams); + + const listParams: ListParams = { + matcher: toNullable({ + key: toNullable('/asset\\d+\\.svg$'), + description: toNullable(), + created_at: toNullable(), + updated_at: toNullable() + }), + order: toNullable({ + desc: true, + field: { Keys: null } + }), + owner: toNullable(), + paginate: toNullable() + }; + + const remainingAssets = await list_assets(collection, listParams); + expect(remainingAssets.items.map((item) => item[1].key.full_path)).toEqual([ + `/${collection}/asset3.svg` + ]); + + const listParamsUser2: ListParams = { + matcher: toNullable({ + key: toNullable('/user2_asset\\.svg$'), + description: toNullable(), + created_at: toNullable(), + updated_at: toNullable() + }), + order: toNullable({ + desc: true, + field: { Keys: null } + }), + owner: toNullable(), + paginate: toNullable() + }; + + actor.setIdentity(user2); + + const user2Assets = await list_assets(collection, listParamsUser2); + expect(user2Assets.items.map((item) => item[1].key.full_path)).toEqual([ + `/${collection}/user2_asset.svg` + ]); + + actor.setIdentity(user); + }); + + it('should delete assets as controller if filtered delete is used', async () => { + const { del_filtered_assets, list_assets } = actor; + + actor.setIdentity(controller); + + const filterParams: ListParams = { + matcher: toNullable({ + key: toNullable('/user2_asset\\.svg$'), + description: toNullable(), + created_at: toNullable(), + updated_at: toNullable() + }), + order: toNullable(), + owner: toNullable(), + paginate: toNullable() + }; + + await del_filtered_assets(collection, filterParams); + + const listParams: ListParams = { + matcher: toNullable(), + order: toNullable(), + owner: toNullable(), + paginate: toNullable() + }; + + actor.setIdentity(user2); + + const user2Assets = await list_assets(collection, listParams); + + expect(user2Assets.items_length).toEqual(0n); + + actor.setIdentity(user); + + const remainingAssets = await list_assets(collection, listParams); + expect(remainingAssets.items_length).toBeGreaterThan(0n); + }); + }); } ); });