Skip to content

Commit 23ee4f7

Browse files
committed
test: add HeadBucketCommand mock and verifyBucket tests in FileService
1 parent 9a546c4 commit 23ee4f7

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
GetObjectCommand,
3+
HeadBucketCommand,
34
PutObjectCommand,
45
S3Client,
56
} from '@aws-sdk/client-s3';
@@ -17,6 +18,7 @@ jest.mock('@aws-sdk/client-s3', () => {
1718
S3Client: jest.fn(() => mS3Client),
1819
GetObjectCommand: jest.fn(),
1920
PutObjectCommand: jest.fn(),
21+
HeadBucketCommand: jest.fn(),
2022
ObjectCannedACL: {
2123
private: 'private',
2224
public_read: 'public-read',
@@ -72,17 +74,23 @@ describe('FileService', () => {
7274
expect(fileService).toBeDefined();
7375
});
7476

75-
it('should throw an error if S3 configuration is missing', () => {
76-
expect(() => {
77-
new FileService(
78-
'',
79-
'test-bucket-thumbs',
80-
'test-key',
81-
'test-secret',
82-
'test-endpoint',
83-
'test-region',
84-
);
85-
}).toThrow('Missing S3 bucket configuration');
77+
describe('verifyBucket', () => {
78+
it('should verify the buckets successfully', async () => {
79+
(s3Client.send as jest.Mock).mockResolvedValueOnce({});
80+
(s3Client.send as jest.Mock).mockResolvedValueOnce({});
81+
82+
await fileService['verifyBucket']();
83+
84+
expect(s3Client.send).toHaveBeenCalledWith(expect.any(HeadBucketCommand));
85+
expect(s3Client.send).toHaveBeenCalledWith(expect.any(HeadBucketCommand));
86+
});
87+
88+
it('should log an error if bucket verification fails', async () => {
89+
const error = new Error('Bucket not found');
90+
(s3Client.send as jest.Mock).mockRejectedValueOnce(error);
91+
92+
await expect(fileService['verifyBucket']()).rejects.toThrow(error);
93+
});
8694
});
8795

8896
it('should upload a song', async () => {

0 commit comments

Comments
 (0)