app.e2e-spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { INestApplication } from '@nestjs/common';
  2. import { Test } from '@nestjs/testing';
  3. import * as request from 'supertest';
  4. import { AppModule } from '../src/app.module';
  5. import { configureApp } from '../src/app.setup';
  6. describe('App (e2e)', () => {
  7. let app: INestApplication;
  8. beforeAll(async () => {
  9. const moduleRef = await Test.createTestingModule({
  10. imports: [AppModule],
  11. }).compile();
  12. app = moduleRef.createNestApplication();
  13. configureApp(app);
  14. await app.init();
  15. });
  16. afterAll(async () => {
  17. await app.close();
  18. });
  19. it('/api/v1/health (GET)', async () => {
  20. const response = await request(app.getHttpServer()).get('/api/v1/health').expect(200);
  21. expect(response.body).toEqual({
  22. ok: true,
  23. data: {
  24. status: 'ok',
  25. timestamp: expect.any(String),
  26. services: {
  27. database: expect.any(String),
  28. redis: expect.any(String),
  29. },
  30. },
  31. error: null,
  32. });
  33. });
  34. it('/api/v1/unknown (GET)', async () => {
  35. const response = await request(app.getHttpServer()).get('/api/v1/unknown').expect(404);
  36. expect(response.body).toEqual({
  37. ok: false,
  38. data: null,
  39. error: {
  40. code: 'HTTP_404',
  41. message: expect.any(String),
  42. },
  43. });
  44. });
  45. it('/api/v1/auth/stylist/login (POST) validates request body', async () => {
  46. const response = await request(app.getHttpServer())
  47. .post('/api/v1/auth/stylist/login')
  48. .send({ email: 'bad-email', password: '' })
  49. .expect(400);
  50. expect(response.body.ok).toBe(false);
  51. expect(response.body.data).toBeNull();
  52. expect(response.body.error.code).toBe('HTTP_400');
  53. });
  54. it('/api/v1/auth/stylist/login (POST) returns invalid credentials when no matching account exists', async () => {
  55. const response = await request(app.getHttpServer())
  56. .post('/api/v1/auth/stylist/login')
  57. .send({ email: 'missing-app-e2e@example.com', password: 'Passw0rd!' })
  58. .expect(401);
  59. expect(response.body).toEqual({
  60. ok: false,
  61. data: null,
  62. error: {
  63. code: 'AUTH_INVALID_CREDENTIALS',
  64. message: '邮箱或密码错误',
  65. },
  66. });
  67. });
  68. it('/api/v1/customers (GET) requires authentication', async () => {
  69. const response = await request(app.getHttpServer())
  70. .get('/api/v1/customers')
  71. .expect(401);
  72. expect(response.body).toEqual({
  73. ok: false,
  74. data: null,
  75. error: {
  76. code: 'AUTH_UNAUTHORIZED',
  77. message: '未登录或登录已失效',
  78. },
  79. });
  80. });
  81. it('/api/v1/templates (GET) returns an empty list envelope', async () => {
  82. const response = await request(app.getHttpServer())
  83. .get('/api/v1/templates')
  84. .expect(200);
  85. expect(response.body).toEqual({
  86. ok: true,
  87. data: {
  88. items: [],
  89. total: 0,
  90. },
  91. error: null,
  92. });
  93. });
  94. it('/api/v1/simulations (POST) validates create payload', async () => {
  95. await request(app.getHttpServer())
  96. .post('/api/v1/simulations')
  97. .send({})
  98. .expect(400);
  99. });
  100. });