-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove creatorId from fileRecord entity
- Loading branch information
1 parent
399244f
commit ffb4603
Showing
9 changed files
with
248 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
apps/server/src/modules/files-storage/service/files-storage-remove-creator.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AntivirusService } from '@infra/antivirus'; | ||
import { S3ClientAdapter } from '@infra/s3-client'; | ||
import { fileRecordFactory, setupEntities } from '@shared/testing'; | ||
import { LegacyLogger } from '@src/core/logger'; | ||
import { FileRecordParams } from '../controller/dto'; | ||
import { FileRecord, FileRecordParentType } from '../entity'; | ||
import { FILES_STORAGE_S3_CONNECTION } from '../files-storage.config'; | ||
import { FileRecordRepo } from '../repo'; | ||
import { FilesStorageService } from './files-storage.service'; | ||
|
||
const buildFileRecordsWithParams = () => { | ||
const parentId = new ObjectId().toHexString(); | ||
const parentSchoolId = new ObjectId().toHexString(); | ||
const creatorId = new ObjectId().toHexString(); | ||
|
||
const fileRecords = [ | ||
fileRecordFactory.buildWithId({ parentId, schoolId: parentSchoolId, name: 'text.txt', creatorId }), | ||
fileRecordFactory.buildWithId({ parentId, schoolId: parentSchoolId, name: 'text-two.txt', creatorId }), | ||
fileRecordFactory.buildWithId({ parentId, schoolId: parentSchoolId, name: 'text-tree.txt', creatorId }), | ||
]; | ||
|
||
const params: FileRecordParams = { | ||
schoolId: parentSchoolId, | ||
parentId, | ||
parentType: FileRecordParentType.User, | ||
}; | ||
|
||
return { params, fileRecords, parentId, creatorId }; | ||
}; | ||
|
||
describe('FilesStorageService delete methods', () => { | ||
let module: TestingModule; | ||
let service: FilesStorageService; | ||
let fileRecordRepo: DeepMocked<FileRecordRepo>; | ||
let storageClient: DeepMocked<S3ClientAdapter>; | ||
|
||
beforeAll(async () => { | ||
await setupEntities([FileRecord]); | ||
|
||
module = await Test.createTestingModule({ | ||
providers: [ | ||
FilesStorageService, | ||
{ | ||
provide: FILES_STORAGE_S3_CONNECTION, | ||
useValue: createMock<S3ClientAdapter>(), | ||
}, | ||
{ | ||
provide: FileRecordRepo, | ||
useValue: createMock<FileRecordRepo>(), | ||
}, | ||
{ | ||
provide: LegacyLogger, | ||
useValue: createMock<LegacyLogger>(), | ||
}, | ||
{ | ||
provide: AntivirusService, | ||
useValue: createMock<AntivirusService>(), | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: createMock<ConfigService>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get(FilesStorageService); | ||
storageClient = module.get(FILES_STORAGE_S3_CONNECTION); | ||
fileRecordRepo = module.get(FileRecordRepo); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
it('service should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('removeCreatorIdFromFileRecord is called', () => { | ||
describe('WHEN valid files does not exist', () => { | ||
const setup = () => { | ||
const userId = new ObjectId().toHexString(); | ||
fileRecordRepo.findByCreatorId.mockResolvedValueOnce([[], 0]); | ||
|
||
return { userId }; | ||
}; | ||
|
||
it('should not modify any filescall repo save with undefined creatorId', async () => { | ||
const { userId } = setup(); | ||
|
||
const result = await service.removeCreatorIdFromFileRecord(userId); | ||
|
||
expect(result).toEqual(0); | ||
|
||
expect(fileRecordRepo.findByCreatorId).toBeCalledWith(userId); | ||
expect(fileRecordRepo.save).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('WHEN valid files exists', () => { | ||
const setup = () => { | ||
const { fileRecords, creatorId } = buildFileRecordsWithParams(); | ||
|
||
fileRecordRepo.findByCreatorId.mockResolvedValueOnce([fileRecords, fileRecords.length]); | ||
|
||
return { fileRecords, creatorId }; | ||
}; | ||
|
||
it('should call repo save with undefined creatorId', async () => { | ||
const { fileRecords, creatorId } = setup(); | ||
|
||
await service.removeCreatorIdFromFileRecord(creatorId); | ||
|
||
expect(fileRecordRepo.save).toHaveBeenCalledWith( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ ...fileRecords[0], _creatorId: undefined }), | ||
expect.objectContaining({ ...fileRecords[1], _creatorId: undefined }), | ||
expect.objectContaining({ ...fileRecords[2], _creatorId: undefined }), | ||
]) | ||
); | ||
}); | ||
|
||
it('should getnumber of updated fileRecords', async () => { | ||
const { creatorId } = setup(); | ||
|
||
const result = await service.removeCreatorIdFromFileRecord(creatorId); | ||
|
||
expect(result).toEqual(3); | ||
}); | ||
}); | ||
|
||
describe('WHEN repository throw an error', () => { | ||
const setup = () => { | ||
const { fileRecords } = buildFileRecordsWithParams(); | ||
|
||
fileRecordRepo.save.mockRejectedValueOnce(new Error('bla')); | ||
|
||
return { fileRecords }; | ||
}; | ||
|
||
it('should pass the error', async () => { | ||
const { fileRecords } = setup(); | ||
|
||
await expect(service.delete(fileRecords)).rejects.toThrow(new Error('bla')); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters