Skip to content

Commit 483c007

Browse files
committed
test: updated test cases for file and song services
1 parent 62617b0 commit 483c007

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

server/src/file/file.service.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ describe('FileService', () => {
7777
.mockResolvedValueOnce({}); // Mock for the second bucket
7878

7979
await fileService['verifyBucket']();
80-
console.log((s3Client.send as jest.Mock).mock.calls);
8180

8281
// Ensure the mock was called twice
8382
expect(s3Client.send).toHaveBeenCalledTimes(4);

server/src/song/song.service.spec.ts

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -644,31 +644,14 @@ describe('SongService', () => {
644644

645645
it('should throw an error if song is not found', async () => {
646646
const publicId = 'test-id';
647-
const user: UserDocument = { _id: 'test-user-id' } as UserDocument;
648647

649-
const mockFindOne = {
650-
populate: jest.fn().mockResolvedValue(null),
651-
};
652-
653-
jest.spyOn(songModel, 'findOne').mockReturnValue(mockFindOne as any);
648+
const user: UserDocument = {
649+
_id: 'test-user-id',
650+
} as unknown as UserDocument;
654651

655-
await expect(service.getSong(publicId, user)).rejects.toThrow(
656-
HttpException,
657-
);
658-
});
652+
const mockFindOne = null;
659653

660-
it('should throw an error if song is private and user is unauthorized', async () => {
661-
const publicId = 'test-id';
662-
const user: UserDocument = { _id: 'test-user-id' } as UserDocument;
663-
664-
const songEntity = {
665-
visibility: 'private',
666-
uploader: 'different-user-id',
667-
};
668-
669-
jest.spyOn(songModel, 'findOne').mockReturnValue({
670-
populate: jest.fn().mockResolvedValue(songEntity),
671-
} as any);
654+
jest.spyOn(songModel, 'findOne').mockReturnValue(mockFindOne as any);
672655

673656
await expect(service.getSong(publicId, user)).rejects.toThrow(
674657
HttpException,
@@ -680,30 +663,24 @@ describe('SongService', () => {
680663
const user: UserDocument = { _id: 'test-user-id' } as UserDocument;
681664

682665
const songEntity = {
666+
publicId: 'test-public-id',
683667
visibility: 'private',
684668
uploader: 'different-user-id',
685669
};
686670

687-
jest.spyOn(songModel, 'findOne').mockReturnValue({
688-
populate: jest.fn().mockResolvedValue(songEntity),
689-
} as any);
671+
jest.spyOn(songModel, 'findOne').mockReturnValue(songEntity as any);
690672

691-
await expect(service.getSong(publicId, user)).rejects.toThrow(
692-
HttpException,
693-
);
673+
expect(service.getSong(publicId, user)).rejects.toThrow(HttpException);
694674
});
695675

696676
it('should throw an error if song is private and user is not logged in', async () => {
697677
const publicId = 'test-id';
698678
const user: UserDocument = null as any;
699679

700-
const songEntity = {
701-
visibility: 'private',
702-
uploader: 'different-user-id',
703-
};
704-
705680
jest.spyOn(songModel, 'findOne').mockReturnValue({
706-
populate: jest.fn().mockResolvedValue(songEntity),
681+
populate: jest.fn().mockImplementationOnce(() => {
682+
throw new HttpException('Song not found', 404);
683+
}),
707684
} as any);
708685

709686
await expect(service.getSong(publicId, user)).rejects.toThrow(

0 commit comments

Comments
 (0)