'use strict'; const { app, assert } = require('egg-mock/bootstrap'); const { generateSalt, hashPassword } = require('../../app/lib/password'); const { codeKey, dailyKey, intervalKey } = require('../../app/lib/emailCode'); const USERNAME = 'auth_test_user'; const PASSWORD = 'test123456'; const REGISTER_USERNAME = 'auth_register_user'; const REGISTER_EMAIL = 'auth_register_user@oms.test'; describe('test/app/auth.test.js', () => { let userId; before(async () => { // 清理注册用例相关的 Redis 状态,避免限流影响重复执行 await app.redis.del(codeKey(REGISTER_EMAIL)); await app.redis.del(intervalKey(REGISTER_EMAIL)); await app.redis.del(dailyKey(REGISTER_EMAIL)); const salt = generateSalt(); const user = await app.model.User.create({ username: USERNAME, password: hashPassword(PASSWORD, salt), salt, status: 1, }); userId = user.id; }); after(async () => { await app.model.User.destroy({ where: { username: USERNAME } }); await app.model.User.destroy({ where: { username: REGISTER_USERNAME } }); }); it('should 401 when request without token', async () => { await app.httpRequest() .get('/api/users') .expect(401); }); it('should 401 when password is wrong', async () => { const res = await app.httpRequest() .post('/api/auth/login') .send({ account: USERNAME, password: 'wrong-password' }) .expect(401); assert(res.body.code === 401); }); it('should login success and token only contains user_id', async () => { const res = await app.httpRequest() .post('/api/auth/login') .send({ account: USERNAME, password: PASSWORD }); assert(res.status === 200, `login failed: ${res.status} ${JSON.stringify(res.body)}`); const { token, user_id: tokenUserId } = res.body.data; assert(token); assert(tokenUserId === userId); // 校验载荷:仅包含 user_id(jwt 自带 iat/exp),不含任何敏感信息 const decoded = app.jwt.verify(token, app.config.jwt.secret); assert(decoded.user_id === userId); assert(!decoded.password); assert(!decoded.salt); assert(!decoded.username); // 携带 token 可正常访问受保护接口 await app.httpRequest() .get('/api/users') .set('Authorization', `Bearer ${token}`) .expect(200); await app.httpRequest() .get('/api/auth/profile') .set('Authorization', `Bearer ${token}`) .expect(200); }); it('should 400 when register without email code', async () => { await app.httpRequest() .post('/api/auth/register') .send({ username: 'no_code_user', password: PASSWORD, email: 'no_code@oms.test' }) .expect(400); }); it('should send email code and register success', async () => { // 1. 发送邮箱验证码 const sendRes = await app.httpRequest() .post('/api/auth/email-code') .send({ email: REGISTER_EMAIL }) .expect(200); assert(sendRes.body.code === 0); // 2. 从 Redis 取出验证码(SMTP 未配置时不会真实发信) const code = await app.redis.get(codeKey(REGISTER_EMAIL)); assert(code && code.length === 6); // 3. 携带验证码注册 const res = await app.httpRequest() .post('/api/auth/register') .send({ username: REGISTER_USERNAME, password: PASSWORD, email: REGISTER_EMAIL, code, nickname: '注册测试' }); assert(res.status === 200, `register failed: ${res.status} ${JSON.stringify(res.body)}`); const { token, user_id: newUserId } = res.body.data; assert(token); assert(newUserId > 0); // 注册返回的 token 可直接访问受保护接口 await app.httpRequest() .get('/api/auth/profile') .set('Authorization', `Bearer ${token}`) .expect(200); // 密码应为加盐哈希,不可明文反解 const created = await app.model.User.findOne({ where: { username: REGISTER_USERNAME } }); assert(created.password !== PASSWORD); assert(created.salt); }); it('should 400 when register email already exists', async () => { // 邮箱已被注册:发送验证码阶段即拦截 const sendRes = await app.httpRequest() .post('/api/auth/email-code') .send({ email: REGISTER_EMAIL }) .expect(400); assert(sendRes.body.code === 400); // 注册阶段同样拦截 const res = await app.httpRequest() .post('/api/auth/register') .send({ username: 'another_user', password: PASSWORD, email: REGISTER_EMAIL, code: '000000' }) .expect(400); assert(res.body.code === 400); }); it('should 400 when email code is wrong', async () => { await app.httpRequest() .post('/api/auth/email-code') .send({ email: 'wrong_code@oms.test' }) .expect(200); const res = await app.httpRequest() .post('/api/auth/register') .send({ username: 'wrong_code_user', password: PASSWORD, email: 'wrong_code@oms.test', code: '000000' }) .expect(400); assert(res.body.code === 400); }); it('should 400 when register params invalid', async () => { const res = await app.httpRequest() .post('/api/auth/register') .send({ username: 'short_pwd_user', password: '123', email: 'bad-email', code: '123456' }) .expect(400); assert(res.body.code === 400); }); it('should 429 when send code too frequently', async () => { const email = 'freq_limit@oms.test'; await app.redis.del(intervalKey(email), dailyKey(email), codeKey(email)); await app.httpRequest() .post('/api/auth/email-code') .send({ email }) .expect(200); const res = await app.httpRequest() .post('/api/auth/email-code') .send({ email }) .expect(429); assert(res.body.code === 429); await app.redis.del(intervalKey(email), dailyKey(email), codeKey(email)); }); it('should 429 when daily limit reached', async () => { const email = 'daily_limit@oms.test'; await app.redis.del(intervalKey(email), codeKey(email)); // 直接把当日计数置为上限 await app.redis.set(dailyKey(email), app.config.emailCode.dailyLimit); const res = await app.httpRequest() .post('/api/auth/email-code') .send({ email }) .expect(429); assert(res.body.code === 429); await app.redis.del(dailyKey(email)); }); it('should 401 when token is invalid', async () => { await app.httpRequest() .get('/api/users') .set('Authorization', 'Bearer invalid.token.value') .expect(401); }); });