2023-07-26 22:31:52 -07:00
|
|
|
/*
|
2024-02-11 18:37:45 -08:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-26 22:31:52 -07:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-22 21:48:14 -07:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import { Test } from '@nestjs/testing';
|
2024-01-08 00:43:52 -08:00
|
|
|
import {
|
|
|
|
CompleteMultipartUploadCommand,
|
|
|
|
CreateMultipartUploadCommand,
|
|
|
|
PutObjectCommand,
|
|
|
|
S3Client,
|
|
|
|
UploadPartCommand,
|
|
|
|
} from '@aws-sdk/client-s3';
|
2023-03-22 21:48:14 -07:00
|
|
|
import { mockClient } from 'aws-sdk-client-mock';
|
|
|
|
import { GlobalModule } from '@/GlobalModule.js';
|
|
|
|
import { CoreModule } from '@/core/CoreModule.js';
|
2023-09-05 01:02:14 -07:00
|
|
|
import { S3Service } from '@/core/S3Service.js';
|
2023-09-14 22:28:29 -07:00
|
|
|
import { MiMeta } from '@/models/_.js';
|
2023-03-22 21:48:14 -07:00
|
|
|
import type { TestingModule } from '@nestjs/testing';
|
|
|
|
|
|
|
|
describe('S3Service', () => {
|
|
|
|
let app: TestingModule;
|
|
|
|
let s3Service: S3Service;
|
|
|
|
const s3Mock = mockClient(S3Client);
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
app = await Test.createTestingModule({
|
|
|
|
imports: [GlobalModule, CoreModule],
|
|
|
|
providers: [S3Service],
|
|
|
|
}).compile();
|
|
|
|
app.enableShutdownHooks();
|
|
|
|
s3Service = app.get<S3Service>(S3Service);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
s3Mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('upload', () => {
|
|
|
|
test('upload a file', async () => {
|
|
|
|
s3Mock.on(PutObjectCommand).resolves({});
|
|
|
|
|
2023-09-05 01:02:14 -07:00
|
|
|
await s3Service.upload({ objectStorageRegion: 'us-east-1' } as MiMeta, {
|
2023-03-22 21:48:14 -07:00
|
|
|
Bucket: 'fake',
|
|
|
|
Key: 'fake',
|
|
|
|
Body: 'x',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('upload a large file', async () => {
|
|
|
|
s3Mock.on(CreateMultipartUploadCommand).resolves({ UploadId: '1' });
|
|
|
|
s3Mock.on(UploadPartCommand).resolves({ ETag: '1' });
|
|
|
|
s3Mock.on(CompleteMultipartUploadCommand).resolves({ Bucket: 'fake', Key: 'fake' });
|
|
|
|
|
2023-09-05 01:02:14 -07:00
|
|
|
await s3Service.upload({} as MiMeta, {
|
2023-03-22 21:48:14 -07:00
|
|
|
Bucket: 'fake',
|
|
|
|
Key: 'fake',
|
|
|
|
Body: 'x'.repeat(8 * 1024 * 1024 + 1), // デフォルトpartSizeにしている 8 * 1024 * 1024 を越えるサイズ
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('upload a file error', async () => {
|
|
|
|
s3Mock.on(PutObjectCommand).rejects({ name: 'Fake Error' });
|
|
|
|
|
2023-09-05 01:02:14 -07:00
|
|
|
await expect(s3Service.upload({ objectStorageRegion: 'us-east-1' } as MiMeta, {
|
2023-03-22 21:48:14 -07:00
|
|
|
Bucket: 'fake',
|
|
|
|
Key: 'fake',
|
|
|
|
Body: 'x',
|
|
|
|
})).rejects.toThrowError(Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('upload a large file error', async () => {
|
|
|
|
s3Mock.on(UploadPartCommand).rejects();
|
|
|
|
|
2023-09-05 01:02:14 -07:00
|
|
|
await expect(s3Service.upload({} as MiMeta, {
|
2023-03-22 21:48:14 -07:00
|
|
|
Bucket: 'fake',
|
|
|
|
Key: 'fake',
|
|
|
|
Body: 'x'.repeat(8 * 1024 * 1024 + 1), // デフォルトpartSizeにしている 8 * 1024 * 1024 を越えるサイズ
|
|
|
|
})).rejects.toThrowError(Error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|