2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-23 06:48:14 +02:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import { Test } from '@nestjs/testing';
|
2024-01-08 10:43:52 +02:00
|
|
|
import {
|
|
|
|
CompleteMultipartUploadCommand,
|
|
|
|
CreateMultipartUploadCommand,
|
|
|
|
PutObjectCommand,
|
|
|
|
S3Client,
|
|
|
|
UploadPartCommand,
|
|
|
|
} from '@aws-sdk/client-s3';
|
2023-03-23 06:48:14 +02:00
|
|
|
import { mockClient } from 'aws-sdk-client-mock';
|
|
|
|
import { GlobalModule } from '@/GlobalModule.js';
|
|
|
|
import { CoreModule } from '@/core/CoreModule.js';
|
2023-09-05 11:02:14 +03:00
|
|
|
import { S3Service } from '@/core/S3Service.js';
|
2023-09-15 08:28:29 +03:00
|
|
|
import { MiMeta } from '@/models/_.js';
|
2023-03-23 06:48:14 +02: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 11:02:14 +03:00
|
|
|
await s3Service.upload({ objectStorageRegion: 'us-east-1' } as MiMeta, {
|
2023-03-23 06:48:14 +02: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 11:02:14 +03:00
|
|
|
await s3Service.upload({} as MiMeta, {
|
2023-03-23 06:48:14 +02: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 11:02:14 +03:00
|
|
|
await expect(s3Service.upload({ objectStorageRegion: 'us-east-1' } as MiMeta, {
|
2023-03-23 06:48:14 +02:00
|
|
|
Bucket: 'fake',
|
|
|
|
Key: 'fake',
|
|
|
|
Body: 'x',
|
|
|
|
})).rejects.toThrowError(Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('upload a large file error', async () => {
|
|
|
|
s3Mock.on(UploadPartCommand).rejects();
|
|
|
|
|
2023-09-05 11:02:14 +03:00
|
|
|
await expect(s3Service.upload({} as MiMeta, {
|
2023-03-23 06:48:14 +02:00
|
|
|
Bucket: 'fake',
|
|
|
|
Key: 'fake',
|
|
|
|
Body: 'x'.repeat(8 * 1024 * 1024 + 1), // デフォルトpartSizeにしている 8 * 1024 * 1024 を越えるサイズ
|
|
|
|
})).rejects.toThrowError(Error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|