2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-11 13:35:51 +09:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import { Test } from '@nestjs/testing';
|
2024-01-08 17:43:52 +09:00
|
|
|
import {
|
|
|
|
DeleteObjectCommand,
|
|
|
|
DeleteObjectCommandOutput,
|
|
|
|
InvalidObjectState,
|
|
|
|
NoSuchKey,
|
|
|
|
S3Client,
|
|
|
|
} from '@aws-sdk/client-s3';
|
2023-03-23 13:48:14 +09:00
|
|
|
import { mockClient } from 'aws-sdk-client-mock';
|
2023-03-11 13:35:51 +09:00
|
|
|
import { GlobalModule } from '@/GlobalModule.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
import { CoreModule } from '@/core/CoreModule.js';
|
|
|
|
import type { TestingModule } from '@nestjs/testing';
|
|
|
|
|
|
|
|
describe('DriveService', () => {
|
|
|
|
let app: TestingModule;
|
|
|
|
let driveService: DriveService;
|
2023-03-23 13:48:14 +09:00
|
|
|
const s3Mock = mockClient(S3Client);
|
2023-03-11 13:35:51 +09:00
|
|
|
|
2023-03-23 13:48:14 +09:00
|
|
|
beforeAll(async () => {
|
2023-03-11 13:35:51 +09:00
|
|
|
app = await Test.createTestingModule({
|
|
|
|
imports: [GlobalModule, CoreModule],
|
2023-03-23 13:48:14 +09:00
|
|
|
providers: [DriveService],
|
2023-03-11 13:35:51 +09:00
|
|
|
}).compile();
|
|
|
|
app.enableShutdownHooks();
|
|
|
|
driveService = app.get<DriveService>(DriveService);
|
2023-03-23 13:48:14 +09:00
|
|
|
});
|
2023-03-11 13:35:51 +09:00
|
|
|
|
2023-03-23 13:48:14 +09:00
|
|
|
beforeEach(async () => {
|
|
|
|
s3Mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
2023-03-11 13:35:51 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Object storage', () => {
|
2023-03-23 13:48:14 +09:00
|
|
|
test('delete a file', async () => {
|
|
|
|
s3Mock.on(DeleteObjectCommand)
|
|
|
|
.resolves({} as DeleteObjectCommandOutput);
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-03-23 13:48:14 +09:00
|
|
|
await driveService.deleteObjectStorageFile('peace of the world');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('delete a file then unexpected error', async () => {
|
|
|
|
s3Mock.on(DeleteObjectCommand)
|
|
|
|
.rejects(new InvalidObjectState({ $metadata: {}, message: '' }));
|
|
|
|
|
|
|
|
await expect(driveService.deleteObjectStorageFile('unexpected')).rejects.toThrowError(Error);
|
|
|
|
});
|
|
|
|
|
2023-03-11 13:35:51 +09:00
|
|
|
test('delete a file with no valid key', async () => {
|
2023-03-23 13:48:14 +09:00
|
|
|
// Some S3 implementations returns 404 Not Found on deleting with a non-existent key
|
|
|
|
s3Mock.on(DeleteObjectCommand)
|
|
|
|
.rejects(new NoSuchKey({ $metadata: {}, message: 'allowed error.' }));
|
|
|
|
|
|
|
|
await driveService.deleteObjectStorageFile('lol no way');
|
2023-03-11 13:35:51 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|