refactor: memory e2e (#30906)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { DateTime } from 'luxon';
|
||||
import { BulkIdErrorReason } from 'src/dtos/asset-ids.response.dto';
|
||||
import { AssetFileType, MemoryType } from 'src/enum';
|
||||
import { AccessRepository } from 'src/repositories/access.repository';
|
||||
import { AssetRepository } from 'src/repositories/asset.repository';
|
||||
@@ -34,30 +35,51 @@ const setup = (db?: Kysely<DB>) => {
|
||||
});
|
||||
};
|
||||
|
||||
/** A memory owned by one user, plus another user's auth to attempt access with */
|
||||
const newMemoryOfAnotherUser = async (ctx: ReturnType<typeof setup>['ctx']) => {
|
||||
const create = async (ctx: ReturnType<typeof setup>['ctx']) => {
|
||||
const { user } = await ctx.newUser();
|
||||
const { user: otherUser } = await ctx.newUser();
|
||||
const { memory } = await ctx.newMemory({ ownerId: user.id });
|
||||
const { asset } = await ctx.newAsset({ ownerId: user.id });
|
||||
|
||||
return { memory, asset, auth: factory.auth({ user }), otherAuth: factory.auth({ user: otherUser }) };
|
||||
return { memory, asset, user };
|
||||
};
|
||||
|
||||
describe(MemoryService.name, () => {
|
||||
describe('get', () => {
|
||||
it('should return the memory', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
|
||||
await expect(sut.get(auth, memory.id)).resolves.toEqual(expect.objectContaining({ id: memory.id }));
|
||||
});
|
||||
|
||||
it('should not return a memory of another user', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, otherAuth } = await newMemoryOfAnotherUser(ctx);
|
||||
const { memory } = await create(ctx);
|
||||
const { user: otherUser } = await ctx.newUser();
|
||||
const otherAuth = factory.auth({ user: otherUser });
|
||||
|
||||
await expect(sut.get(otherAuth, memory.id)).rejects.toThrow('Not found or no memory.read access');
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update the memory', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
|
||||
await expect(sut.get(auth, memory.id)).resolves.toEqual(expect.objectContaining({ isSaved: false }));
|
||||
await expect(sut.update(auth, memory.id, { isSaved: true })).resolves.toEqual(
|
||||
expect.objectContaining({ id: memory.id, isSaved: true }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should not update a memory of another user', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, otherAuth } = await newMemoryOfAnotherUser(ctx);
|
||||
const { memory } = await create(ctx);
|
||||
const { user: otherUser } = await ctx.newUser();
|
||||
const otherAuth = factory.auth({ user: otherUser });
|
||||
|
||||
await expect(sut.update(otherAuth, memory.id, { isSaved: true })).rejects.toThrow(
|
||||
'Not found or no memory.update access',
|
||||
@@ -66,9 +88,21 @@ describe(MemoryService.name, () => {
|
||||
});
|
||||
|
||||
describe('remove', () => {
|
||||
it('should remove the memory', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
|
||||
await expect(sut.remove(auth, memory.id)).resolves.toBeUndefined();
|
||||
await expect(sut.get(auth, memory.id)).rejects.toThrow('Not found or no memory.read access');
|
||||
});
|
||||
|
||||
it('should not remove a memory of another user', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, auth, otherAuth } = await newMemoryOfAnotherUser(ctx);
|
||||
const { memory, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
const { user: otherUser } = await ctx.newUser();
|
||||
const otherAuth = factory.auth({ user: otherUser });
|
||||
|
||||
await expect(sut.remove(otherAuth, memory.id)).rejects.toThrow('Not found or no memory.delete access');
|
||||
await expect(sut.get(auth, memory.id)).resolves.toEqual(expect.objectContaining({ id: memory.id }));
|
||||
@@ -76,9 +110,33 @@ describe(MemoryService.name, () => {
|
||||
});
|
||||
|
||||
describe('addAssets', () => {
|
||||
it('should add assets to the memory', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, asset, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
|
||||
await expect(sut.addAssets(auth, memory.id, { ids: [asset.id] })).resolves.toEqual([
|
||||
{ id: asset.id, success: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should require access to the asset', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
const { user: other } = await ctx.newUser();
|
||||
const { asset: otherAsset } = await ctx.newAsset({ ownerId: other.id });
|
||||
|
||||
await expect(sut.addAssets(auth, memory.id, { ids: [otherAsset.id] })).resolves.toEqual([
|
||||
{ id: otherAsset.id, success: false, error: BulkIdErrorReason.NO_PERMISSION },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should not add assets to a memory of another user', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, asset, otherAuth } = await newMemoryOfAnotherUser(ctx);
|
||||
const { memory, asset } = await create(ctx);
|
||||
const { user: otherUser } = await ctx.newUser();
|
||||
const otherAuth = factory.auth({ user: otherUser });
|
||||
|
||||
await expect(sut.addAssets(otherAuth, memory.id, { ids: [asset.id] })).rejects.toThrow(
|
||||
'Not found or no memory.read access',
|
||||
@@ -87,9 +145,32 @@ describe(MemoryService.name, () => {
|
||||
});
|
||||
|
||||
describe('removeAssets', () => {
|
||||
it('should remove assets from the memory', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, asset, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
await ctx.newMemoryAsset({ memoryId: memory.id, assetId: asset.id });
|
||||
|
||||
await expect(sut.removeAssets(auth, memory.id, { ids: [asset.id] })).resolves.toEqual([
|
||||
{ id: asset.id, success: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should only remove assets that are in the memory', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, asset, user } = await create(ctx);
|
||||
const auth = factory.auth({ user });
|
||||
|
||||
await expect(sut.removeAssets(auth, memory.id, { ids: [asset.id] })).resolves.toEqual([
|
||||
{ id: asset.id, success: false, error: BulkIdErrorReason.NOT_FOUND },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should not remove assets from a memory of another user', async () => {
|
||||
const { sut, ctx } = setup();
|
||||
const { memory, asset, otherAuth } = await newMemoryOfAnotherUser(ctx);
|
||||
const { memory, asset } = await create(ctx);
|
||||
const { user: otherUser } = await ctx.newUser();
|
||||
const otherAuth = factory.auth({ user: otherUser });
|
||||
await ctx.newMemoryAsset({ memoryId: memory.id, assetId: asset.id });
|
||||
|
||||
await expect(sut.removeAssets(otherAuth, memory.id, { ids: [asset.id] })).rejects.toThrow(
|
||||
|
||||
Reference in New Issue
Block a user