| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- import { INestApplication } from '@nestjs/common';
- 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],
- })
- .overrideProvider(STORAGE_DRIVER)
- .useValue({ upload })
- .overrideProvider(GcsStorageDriver)
- .useValue({ upload })
- .compile();
- app = moduleRef.createNestApplication();
- configureApp(app);
- await app.init();
- });
- afterAll(async () => {
- await app.close();
- });
- it('/api/v1/health (GET)', async () => {
- const response = await request(app.getHttpServer()).get('/api/v1/health').expect(200);
- expect(response.body).toEqual({
- ok: true,
- data: {
- status: 'ok',
- timestamp: expect.any(String),
- services: {
- database: expect.any(String),
- redis: expect.any(String),
- },
- },
- error: null,
- });
- });
- it('/api/v1/unknown (GET)', async () => {
- const response = await request(app.getHttpServer()).get('/api/v1/unknown').expect(404);
- expect(response.body).toEqual({
- ok: false,
- data: null,
- error: {
- code: 'HTTP_404',
- message: expect.any(String),
- },
- });
- });
- it('/api/v1/auth/stylist/login (POST) validates request body', async () => {
- const response = await request(app.getHttpServer())
- .post('/api/v1/auth/stylist/login')
- .send({ email: 'bad-email', password: '' })
- .expect(400);
- expect(response.body.ok).toBe(false);
- expect(response.body.data).toBeNull();
- expect(response.body.error.code).toBe('HTTP_400');
- });
- it('/api/v1/auth/stylist/login (POST) returns invalid credentials when no matching account exists', async () => {
- const response = await request(app.getHttpServer())
- .post('/api/v1/auth/stylist/login')
- .send({ email: 'missing-app-e2e@example.com', password: 'Passw0rd!' })
- .expect(401);
- expect(response.body).toEqual({
- ok: false,
- data: null,
- error: {
- code: 'AUTH_INVALID_CREDENTIALS',
- message: '邮箱或密码错误',
- },
- });
- });
- it('/api/v1/customers (GET) requires authentication', async () => {
- const response = await request(app.getHttpServer())
- .get('/api/v1/customers')
- .expect(401);
- expect(response.body).toEqual({
- ok: false,
- data: null,
- error: {
- code: 'AUTH_UNAUTHORIZED',
- message: '未登录或登录已失效',
- },
- });
- });
- it('/api/v1/templates (GET) returns an empty list envelope', async () => {
- const response = await request(app.getHttpServer())
- .get('/api/v1/templates')
- .expect(200);
- expect(response.body).toEqual({
- ok: true,
- data: {
- items: [],
- total: 0,
- },
- error: null,
- });
- });
- it('/api/v1/simulations (POST) validates create payload', async () => {
- await request(app.getHttpServer())
- .post('/api/v1/simulations')
- .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: '上传类型不合法',
- },
- });
- });
- });
|