| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import { INestApplication } from '@nestjs/common';
- import { JwtService } from '@nestjs/jwt';
- import { Test } from '@nestjs/testing';
- import * as request from 'supertest';
- import { AppModule } from '../src/app.module';
- import { configureApp } from '../src/app.setup';
- describe('Auth (e2e)', () => {
- let app: INestApplication;
- let jwtService: JwtService;
- beforeAll(async () => {
- const moduleRef = await Test.createTestingModule({
- imports: [AppModule],
- }).compile();
- app = moduleRef.createNestApplication();
- configureApp(app);
- await app.init();
- jwtService = app.get(JwtService);
- });
- afterAll(async () => {
- await app.close();
- });
- it('/api/v1/customers (GET) rejects unauthenticated requests', 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/auth/stylist/login (POST) returns a token payload with username', async () => {
- const response = await request(app.getHttpServer())
- .post('/api/v1/auth/stylist/login')
- .send({
- email: 'stylist@example.com',
- password: 'Passw0rd!',
- remember: false,
- })
- .expect(201);
- const payload = await jwtService.verifyAsync<{
- sub: number;
- email: string;
- username: string;
- }>(response.body.data.token);
- expect(payload.sub).toBe(1);
- expect(payload.email).toBe('stylist@example.com');
- expect(payload.username).toBe('Ogawa');
- });
- it('/api/v1/customers (GET) accepts a valid signed token', async () => {
- const token = await jwtService.signAsync({
- sub: 1,
- email: 'stylist@example.com',
- username: 'Ogawa',
- });
- const response = await request(app.getHttpServer())
- .get('/api/v1/customers')
- .set('Authorization', token)
- .expect(200);
- expect(response.body).toEqual({
- ok: true,
- data: {
- items: [],
- total: 0,
- },
- error: null,
- });
- });
- });
|