From 1c27f615fd57124fafc5a105ad9f82f892e87352 Mon Sep 17 00:00:00 2001 From: Test Engineer Date: Tue, 10 Mar 2026 08:46:59 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86API=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/backend/user.test.js | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/backend/user.test.js diff --git a/tests/backend/user.test.js b/tests/backend/user.test.js new file mode 100644 index 0000000..4caaf46 --- /dev/null +++ b/tests/backend/user.test.js @@ -0,0 +1,47 @@ +// 用户管理 API 测试 +const request = require('supertest'); +const app = require('../../backend/src/index'); + +describe('User API', () => { + let token; + + beforeAll(async () => { + // Login first + const loginRes = await request(app) + .post('/api/auth/login') + .send({ username: 'testuser', password: 'test123' }); + token = loginRes.body.token; + }); + + describe('GET /api/user/profile', () => { + it('should get user profile', async () => { + const response = await request(app) + .get('/api/user/profile') + .set('Authorization', `Bearer ${token}`); + + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('user'); + }); + + it('should reject without token', async () => { + const response = await request(app) + .get('/api/user/profile'); + + expect(response.status).toBe(401); + }); + }); + + describe('PUT /api/user/password', () => { + it('should update password', async () => { + const response = await request(app) + .put('/api/user/password') + .set('Authorization', `Bearer ${token}`) + .send({ + oldPassword: 'test123', + newPassword: 'newpass123' + }); + + expect(response.status).toBe(200); + }); + }); +});