|
|
@@ -3,14 +3,25 @@ import { Test } from '@nestjs/testing';
|
|
|
import * as request from 'supertest';
|
|
|
import { AppModule } from '../src/app.module';
|
|
|
import { configureApp } from '../src/app.setup';
|
|
|
+import { GcsStorageDriver } from '../src/modules/storage/gcs-storage.driver';
|
|
|
+import { STORAGE_DRIVER } from '../src/modules/storage/storage.constants';
|
|
|
|
|
|
describe('App (e2e)', () => {
|
|
|
let app: INestApplication;
|
|
|
+ const upload = jest.fn();
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
+ upload.mockReset();
|
|
|
+ upload.mockResolvedValue(undefined);
|
|
|
+
|
|
|
const moduleRef = await Test.createTestingModule({
|
|
|
imports: [AppModule],
|
|
|
- }).compile();
|
|
|
+ })
|
|
|
+ .overrideProvider(STORAGE_DRIVER)
|
|
|
+ .useValue({ upload })
|
|
|
+ .overrideProvider(GcsStorageDriver)
|
|
|
+ .useValue({ upload })
|
|
|
+ .compile();
|
|
|
|
|
|
app = moduleRef.createNestApplication();
|
|
|
configureApp(app);
|
|
|
@@ -114,4 +125,63 @@ describe('App (e2e)', () => {
|
|
|
.send({})
|
|
|
.expect(400);
|
|
|
});
|
|
|
+
|
|
|
+ it('/api/v1/storage/upload (POST) uploads a file and returns a public url', async () => {
|
|
|
+ const response = await request(app.getHttpServer())
|
|
|
+ .post('/api/v1/storage/upload')
|
|
|
+ .field('type', 'template')
|
|
|
+ .attach('file', Buffer.from('hello'), {
|
|
|
+ filename: 'look.png',
|
|
|
+ contentType: 'image/png',
|
|
|
+ })
|
|
|
+ .expect(201);
|
|
|
+
|
|
|
+ expect(upload).toHaveBeenCalledWith(
|
|
|
+ expect.objectContaining({
|
|
|
+ objectKey: expect.stringMatching(/^stylist\/template\/.+\.png$/),
|
|
|
+ contentType: 'image/png',
|
|
|
+ }),
|
|
|
+ );
|
|
|
+ expect(response.body.ok).toBe(true);
|
|
|
+ expect(response.body.data.objectKey).toMatch(/^stylist\/template\/.+\.png$/);
|
|
|
+ expect(response.body.data.url).toBe(
|
|
|
+ `https://storage.googleapis.com/aitori-public/${response.body.data.objectKey}`,
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ it('/api/v1/storage/upload (POST) rejects requests without a file', async () => {
|
|
|
+ const response = await request(app.getHttpServer())
|
|
|
+ .post('/api/v1/storage/upload')
|
|
|
+ .field('type', 'template')
|
|
|
+ .expect(400);
|
|
|
+
|
|
|
+ expect(response.body).toEqual({
|
|
|
+ ok: false,
|
|
|
+ data: null,
|
|
|
+ error: {
|
|
|
+ code: 'HTTP_400',
|
|
|
+ message: '请选择要上传的文件',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('/api/v1/storage/upload (POST) validates type', async () => {
|
|
|
+ const response = await request(app.getHttpServer())
|
|
|
+ .post('/api/v1/storage/upload')
|
|
|
+ .field('type', '../template')
|
|
|
+ .attach('file', Buffer.from('hello'), {
|
|
|
+ filename: 'look.png',
|
|
|
+ contentType: 'image/png',
|
|
|
+ })
|
|
|
+ .expect(400);
|
|
|
+
|
|
|
+ expect(response.body).toEqual({
|
|
|
+ ok: false,
|
|
|
+ data: null,
|
|
|
+ error: {
|
|
|
+ code: 'HTTP_400',
|
|
|
+ message: '上传类型不合法',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|