Skip to content

Commit

Permalink
test: delete filtered assets
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Nov 14, 2024
1 parent 25b2594 commit 722633d
Showing 1 changed file with 171 additions and 2 deletions.
173 changes: 171 additions & 2 deletions src/tests/satellite.storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,20 +651,31 @@ 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'
}),
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 } }])(
Expand Down Expand Up @@ -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);
});
});
}
);
});
Expand Down

0 comments on commit 722633d

Please sign in to comment.