app.e2e-spec.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { INestApplication } from '@nestjs/common';
  2. import { JwtService } from '@nestjs/jwt';
  3. import { Test } from '@nestjs/testing';
  4. import * as request from 'supertest';
  5. import { AppModule } from '../src/app.module';
  6. import { configureApp } from '../src/app.setup';
  7. import { PrismaService } from '../src/prisma/prisma.service';
  8. import { GcsStorageDriver } from '../src/modules/storage/gcs-storage.driver';
  9. import { STORAGE_DRIVER } from '../src/modules/storage/storage.constants';
  10. describe('App (e2e)', () => {
  11. let app: INestApplication;
  12. let jwtService: JwtService;
  13. let prisma: PrismaService;
  14. const upload = jest.fn();
  15. async function signToken() {
  16. return jwtService.signAsync({
  17. sub: 1,
  18. email: 'stylist@example.com',
  19. username: 'Ogawa',
  20. });
  21. }
  22. beforeAll(async () => {
  23. upload.mockReset();
  24. upload.mockResolvedValue(undefined);
  25. const moduleRef = await Test.createTestingModule({
  26. imports: [AppModule],
  27. })
  28. .overrideProvider(STORAGE_DRIVER)
  29. .useValue({ upload })
  30. .overrideProvider(GcsStorageDriver)
  31. .useValue({ upload })
  32. .compile();
  33. app = moduleRef.createNestApplication();
  34. configureApp(app);
  35. await app.init();
  36. jwtService = app.get(JwtService);
  37. prisma = app.get(PrismaService);
  38. });
  39. afterAll(async () => {
  40. await prisma.aiHairSimulation.deleteMany({
  41. where: {
  42. simulateRemark: {
  43. startsWith: 'app-e2e-customer-summary-',
  44. },
  45. },
  46. });
  47. await prisma.customer.deleteMany({
  48. where: {
  49. name: {
  50. startsWith: 'app-e2e-customer-',
  51. },
  52. },
  53. });
  54. await app.close();
  55. });
  56. it('/api/v1/health (GET)', async () => {
  57. const response = await request(app.getHttpServer()).get('/api/v1/health').expect(200);
  58. expect(response.body).toEqual({
  59. ok: true,
  60. data: {
  61. status: 'ok',
  62. timestamp: expect.any(String),
  63. services: {
  64. database: expect.any(String),
  65. redis: expect.any(String),
  66. },
  67. },
  68. error: null,
  69. });
  70. });
  71. it('/api/v1/unknown (GET)', async () => {
  72. const response = await request(app.getHttpServer()).get('/api/v1/unknown').expect(404);
  73. expect(response.body).toEqual({
  74. ok: false,
  75. data: null,
  76. error: {
  77. code: 'HTTP_404',
  78. message: expect.any(String),
  79. },
  80. });
  81. });
  82. it('/api/v1/auth/stylist/login (POST) validates request body', async () => {
  83. const response = await request(app.getHttpServer())
  84. .post('/api/v1/auth/stylist/login')
  85. .send({ email: 'bad-email', password: '' })
  86. .expect(400);
  87. expect(response.body.ok).toBe(false);
  88. expect(response.body.data).toBeNull();
  89. expect(response.body.error.code).toBe('HTTP_400');
  90. });
  91. it('/api/v1/auth/stylist/login (POST) returns invalid credentials when no matching account exists', async () => {
  92. const response = await request(app.getHttpServer())
  93. .post('/api/v1/auth/stylist/login')
  94. .send({ email: 'missing-app-e2e@example.com', password: 'Passw0rd!' })
  95. .expect(401);
  96. expect(response.body).toEqual({
  97. ok: false,
  98. data: null,
  99. error: {
  100. code: 'AUTH_INVALID_CREDENTIALS',
  101. message: '邮箱或密码错误',
  102. },
  103. });
  104. });
  105. it('/api/v1/customers (GET) requires authentication', async () => {
  106. const response = await request(app.getHttpServer())
  107. .get('/api/v1/customers')
  108. .expect(401);
  109. expect(response.body).toEqual({
  110. ok: false,
  111. data: null,
  112. error: {
  113. code: 'AUTH_UNAUTHORIZED',
  114. message: '未登录或登录已失效',
  115. },
  116. });
  117. });
  118. it('/api/v1/customers (POST) creates a customer with default avatar', async () => {
  119. const token = await signToken();
  120. const uniqueSuffix = Date.now();
  121. const response = await request(app.getHttpServer())
  122. .post('/api/v1/customers')
  123. .set('Authorization', token)
  124. .send({
  125. name: `app-e2e-customer-${uniqueSuffix}`,
  126. phone: `090-${String(uniqueSuffix).slice(-4)}-0000`,
  127. hairLength: 'Medium',
  128. followStatus: 'NEED_FOLLOW_UP',
  129. nextVisit: '2026-06-28',
  130. })
  131. .expect(201);
  132. expect(response.body.ok).toBe(true);
  133. expect(response.body.data.name).toBe(`app-e2e-customer-${uniqueSuffix}`);
  134. expect(response.body.data.image).toBe(
  135. 'https://storage.googleapis.com/aitori-public/style/sample/066c7ba22ea149268444bc2d919d5b97.png',
  136. );
  137. });
  138. it('/api/v1/customers (GET) returns paginated customers', async () => {
  139. const token = await signToken();
  140. const uniqueSuffix = `${Date.now()}-list`;
  141. await prisma.customer.create({
  142. data: {
  143. hairstylistId: BigInt(1),
  144. name: `app-e2e-customer-${uniqueSuffix}`,
  145. kana: '',
  146. phone: `090-${String(Date.now()).slice(-4)}-1111`,
  147. email: '',
  148. avatarUrl:
  149. 'https://storage.googleapis.com/aitori-public/style/sample/066c7ba22ea149268444bc2d919d5b97.png',
  150. contactType: 'LINE',
  151. hairLength: 'MEDIUM',
  152. followStatus: 'NEED_FOLLOW_UP',
  153. },
  154. });
  155. const response = await request(app.getHttpServer())
  156. .get('/api/v1/customers?page=1&pageSize=20&q=app-e2e-customer-&hairLength=MEDIUM&followStatus=NEED_FOLLOW_UP')
  157. .set('Authorization', token)
  158. .expect(200);
  159. expect(response.body.ok).toBe(true);
  160. expect(response.body.data).toEqual({
  161. items: expect.any(Array),
  162. total: expect.any(Number),
  163. page: 1,
  164. pageSize: 20,
  165. });
  166. });
  167. it('/api/v1/customers/summary (GET) returns customer summary', async () => {
  168. const token = await signToken();
  169. const uniqueSuffix = `${Date.now()}-summary`;
  170. const customer = await prisma.customer.create({
  171. data: {
  172. hairstylistId: BigInt(1),
  173. name: `app-e2e-customer-${uniqueSuffix}`,
  174. kana: '',
  175. phone: `090-${String(Date.now()).slice(-4)}-2222`,
  176. email: '',
  177. avatarUrl:
  178. 'https://storage.googleapis.com/aitori-public/style/sample/066c7ba22ea149268444bc2d919d5b97.png',
  179. contactType: 'LINE',
  180. nextVisitDate: new Date(),
  181. followStatus: 'NEED_FOLLOW_UP',
  182. },
  183. });
  184. await prisma.aiHairSimulation.create({
  185. data: {
  186. customerId: customer.id,
  187. hairstylistId: BigInt(1),
  188. originImage: customer.avatarUrl,
  189. simulateRemark: `app-e2e-customer-summary-${uniqueSuffix}`,
  190. },
  191. });
  192. const response = await request(app.getHttpServer())
  193. .get('/api/v1/customers/summary')
  194. .set('Authorization', token)
  195. .expect(200);
  196. expect(response.body.ok).toBe(true);
  197. expect(response.body.data).toEqual({
  198. total: expect.any(Number),
  199. today: expect.any(Number),
  200. followUp: expect.any(Number),
  201. simulated: expect.any(Number),
  202. });
  203. });
  204. it('/api/v1/templates (GET) returns an empty list envelope', async () => {
  205. const response = await request(app.getHttpServer())
  206. .get('/api/v1/templates')
  207. .expect(200);
  208. expect(response.body).toEqual({
  209. ok: true,
  210. data: {
  211. items: [],
  212. total: 0,
  213. },
  214. error: null,
  215. });
  216. });
  217. it('/api/v1/simulations (POST) validates create payload', async () => {
  218. await request(app.getHttpServer())
  219. .post('/api/v1/simulations')
  220. .send({})
  221. .expect(400);
  222. });
  223. it('/api/v1/storage/upload (POST) uploads a file and returns a public url', async () => {
  224. const response = await request(app.getHttpServer())
  225. .post('/api/v1/storage/upload')
  226. .field('type', 'template')
  227. .attach('file', Buffer.from('hello'), {
  228. filename: 'look.png',
  229. contentType: 'image/png',
  230. })
  231. .expect(201);
  232. expect(upload).toHaveBeenCalledWith(
  233. expect.objectContaining({
  234. objectKey: expect.stringMatching(/^stylist\/template\/.+\.png$/),
  235. contentType: 'image/png',
  236. }),
  237. );
  238. expect(response.body.ok).toBe(true);
  239. expect(response.body.data.objectKey).toMatch(/^stylist\/template\/.+\.png$/);
  240. expect(response.body.data.url).toBe(
  241. `https://storage.googleapis.com/aitori-public/${response.body.data.objectKey}`,
  242. );
  243. });
  244. it('/api/v1/storage/upload (POST) rejects requests without a file', async () => {
  245. const response = await request(app.getHttpServer())
  246. .post('/api/v1/storage/upload')
  247. .field('type', 'template')
  248. .expect(400);
  249. expect(response.body).toEqual({
  250. ok: false,
  251. data: null,
  252. error: {
  253. code: 'HTTP_400',
  254. message: '请选择要上传的文件',
  255. },
  256. });
  257. });
  258. it('/api/v1/storage/upload (POST) validates type', async () => {
  259. const response = await request(app.getHttpServer())
  260. .post('/api/v1/storage/upload')
  261. .field('type', '../template')
  262. .attach('file', Buffer.from('hello'), {
  263. filename: 'look.png',
  264. contentType: 'image/png',
  265. })
  266. .expect(400);
  267. expect(response.body).toEqual({
  268. ok: false,
  269. data: null,
  270. error: {
  271. code: 'HTTP_400',
  272. message: '上传类型不合法',
  273. },
  274. });
  275. });
  276. });