Skip to content

Commit

Permalink
#28 - Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikpyt committed Oct 16, 2023
1 parent 3a96fe8 commit 858785b
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 33 deletions.
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ module.exports = {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
setupFilesAfterEnv: ['./src/test/setupTest.ts'],
collectCoverageFrom: ['./src/**', '!./src/plugins/prisma.ts', '!./src/server.ts'],
collectCoverageFrom: [
'./src/**',
'!./src/server.ts',
'!./src/plugins/prisma.ts',
'!./src/plugins/pusher.ts',
],
coverageReporters: ['json-summary', 'text', 'html', 'json'],
coveragePathIgnorePatterns: ['node_modules'],
};
2 changes: 1 addition & 1 deletion src/modules/item/blob/blob.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { UploadInput, ReadInput, EditInput, DeleteInput } from './blob.schema';
import BlobService from './blob.service';
import AccessService from '../sharing/access.service';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';
import { ItemEventType, triggerItemEvent } from '../item.event';

export default class BlobController {
private blobService: BlobService;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/item/docs/docs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { ReadInput, EditInput, AddInput, DeleteInput } from './docs.schema';
import DocsService from './docs.service';
import AccessService from '../sharing/access.service';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';
import { ItemEventType, triggerItemEvent } from '../item.event';

export default class DocsController {
private docsService: DocsService;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/item/folder/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { ReadInput, EditInput, AddInput, DeleteInput } from './folder.schema';
import FolderService from './folder.service';
import AccessService from '../sharing/access.service';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';
import { ItemEventType, triggerItemEvent } from '../item.event';

export default class FolderController {
private folderService: FolderService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum ItemEventType {
DELETE = 'delete',
}

/* istanbul ignore next */
const getItemParentChannel = (item: Item): string => {
if (!item.parentId) {
return `browser-root-${item.ownerId}`;
Expand All @@ -14,8 +15,15 @@ const getItemParentChannel = (item: Item): string => {
return `browser-folder-${item.parentId}`;
};

/* istanbul ignore next */
export const triggerItemEvent = async (item: Item, type: ItemEventType): Promise<void> => {
if (fastify.config.NODE_ENV === 'test') {
return;
}

/* istanbul ignore next */
const channelName = getItemParentChannel(item);

/* istanbul ignore next */
await pusher.trigger(channelName, type, item);
};
82 changes: 82 additions & 0 deletions src/modules/item/sharing/__test__/sharing.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { User } from '@prisma/client';
import UserService from '../../../auth/user.service';
import FolderService from '../../folder/folder.service';
import SharingService from '../sharing.service';
import BlobService from '../../blob/blob.service';
import { UserServiceFactory } from '../../../auth/auth.factory';
import { FolderServiceFactory } from '../../folder/folder.factory';
import { SharingServiceFactory } from '../sharing.factory';
import { BlobServiceFactory } from '../../blob/blob.factory';

describe('SharingService', () => {
let userService: UserService;
let folderService: FolderService;
let sharingService: SharingService;
let blobService: BlobService;

let user: User;
let otherUser: User;

beforeAll(async () => {
userService = UserServiceFactory.make();
folderService = FolderServiceFactory.make();
sharingService = SharingServiceFactory.make();
blobService = BlobServiceFactory.make();

user = await userService.createUser({
name: 'Joe Biden the 1st',
email: '[email protected]',
password: '1234',
});
otherUser = await userService.createUser({
name: 'Joe Biden the 2nd',
email: '[email protected]',
password: '4321',
});
});

describe('deleteSharing()', () => {
it('should delete sharing and all child sharings', async () => {
const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: user.id,
parentId: null,
});
await sharingService.createSharing({ itemId: folder.id, userId: otherUser.id }, user.id);

const subFolder = await folderService.createFolder({
name: 'Sub Folder',
color: '#7890123',
ownerId: user.id,
parentId: folder.id,
});

const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
parentId: subFolder.id,
blobUrl: 'https://example.com/test1.txt',
});

await Promise.all([
expect(sharingService.getByItemIdAndUserId(folder.id, otherUser.id)).resolves.toBeDefined(),
expect(
sharingService.getByItemIdAndUserId(subFolder.id, otherUser.id),
).resolves.toBeDefined(),
expect(sharingService.getByItemIdAndUserId(blob.id, otherUser.id)).resolves.toBeDefined(),
]);

await sharingService.deleteSharing({ itemId: subFolder.id, userId: otherUser.id }, user.id);

await Promise.all([
expect(sharingService.getByItemIdAndUserId(folder.id, otherUser.id)).resolves.toBeDefined(),
expect(
sharingService.getByItemIdAndUserId(subFolder.id, otherUser.id),
).rejects.toThrowError(),
expect(sharingService.getByItemIdAndUserId(blob.id, otherUser.id)).rejects.toThrowError(),
]);
});
});
});
46 changes: 23 additions & 23 deletions src/modules/item/sharing/sharing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,18 @@ export default class SharingService {

await prisma.itemSharing.deleteMany({
where: {
itemId: {
in: accessableItems.map((item) => item.id),
},
userId: itemSharing.userId,
},
});

await prisma.itemSharing.delete({
where: {
itemId_userId: {
itemId: input.itemId,
userId: input.userId,
},
OR: [
{
itemId: input.itemId,
userId: input.userId,
},
{
itemId: {
in: accessableItems.map((item) => item.id),
},
userId: itemSharing.userId,
},
],
},
});
} catch (e) {
Expand Down Expand Up @@ -148,16 +147,17 @@ export default class SharingService {

await prisma.itemSharing.deleteMany({
where: {
itemId: {
in: accessableItems.map((item) => item.id),
},
userId: itemSharing.userId,
},
});

await prisma.itemSharing.delete({
where: {
id: id,
OR: [
{
itemId: {
in: accessableItems.map((item) => item.id),
},
userId: itemSharing.userId,
},
{
id: id,
},
],
},
});
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/item/shortcut/shortcut.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { ReadInput, EditInput, AddInput, DeleteInput } from './shortcut.schema';
import ShortcutService from './shortcut.service';
import AccessService from '../sharing/access.service';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';
import { ItemEventType, triggerItemEvent } from '../item.event';

export default class ShortcutController {
private shortcutService: ShortcutService;
Expand Down
4 changes: 0 additions & 4 deletions src/plugins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export default fastifyPlugin(
'DATABASE_URL_NON_POOLING',
'REDIS_URL',
'BLOB_READ_WRITE_TOKEN',
'PUSHER_APP_ID',
'PUSHER_APP_KEY',
'PUSHER_APP_SECRET',
'PUSHER_APP_CLUSTER',
],
properties: {
SECRET: {
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ export default fastifyPlugin(async (fastify: FastifyInstance) => {

await Promise.all([
fastify.register(prisma),
fastify.register(pusher),
fastify.register(redis),
fastify.register(cookie),
fastify.register(cors),
fastify.config.NODE_ENV === 'local'
? /* istanbul ignore next */ fastify.register(swagger)
: /* istanbul ignore next */ null,
fastify.config.NODE_ENV !== 'test'
? /* istanbul ignore next */ fastify.register(pusher)
: /* istanbul ignore next */ null,
]);

await Promise.all([fastify.register(jwt)]);
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ export let pusher: Pusher;

export default fastifyPlugin(
async (fastify: FastifyInstance) => {
if (
!fastify.config.PUSHER_APP_SECRET &&
!fastify.config.PUSHER_APP_CLUSTER &&
!fastify.config.PUSHER_APP_KEY &&
!fastify.config.PUSHER_APP_ID
) {
fastify.log.fatal('Missing PUSHER ENV variables');
throw new Error('Missing PUSHER ENV variables');
}

pusher = new Pusher({
appId: fastify.config.PUSHER_APP_ID,
key: fastify.config.PUSHER_APP_KEY,
Expand Down

0 comments on commit 858785b

Please sign in to comment.