Skip to content

Commit a06f038

Browse files
sinchangJacksonTian
authored andcommitted
test: improve controller sign unit test coverage (#80)
1 parent dd2c5df commit a06f038

File tree

3 files changed

+115
-39
lines changed

3 files changed

+115
-39
lines changed

app/controller/sign.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class SignController extends Controller {
7373

7474
await service.user.newAndSave(loginname, loginname, passhash, email, avatarUrl, false);
7575
// 发送激活邮件
76-
// await mail.sendActiveMail(email, utility.md5(email + passhash + config.session_secret), loginname);
76+
// await service.mail.sendActiveMail(email, utility.md5(email + passhash + config.session_secret), loginname);
7777
await ctx.render('sign/signup', {
7878
success: '欢迎加入 ' + config.name + '!我们已给您的注册邮箱发送了一封邮件,请点击里面的链接来激活您的帐号。',
7979
});
@@ -180,10 +180,10 @@ class SignController extends Controller {
180180

181181
async updatePass() {
182182
const { ctx, service } = this;
183-
const psw = validator.trim(ctx.body.psw) || '';
184-
const repsw = validator.trim(ctx.body.repsw) || '';
185-
const key = validator.trim(ctx.body.key) || '';
186-
const name = validator.trim(ctx.body.name) || '';
183+
const psw = validator.trim(ctx.request.body.psw) || '';
184+
const repsw = validator.trim(ctx.request.body.repsw) || '';
185+
const key = validator.trim(ctx.request.body.key) || '';
186+
const name = validator.trim(ctx.request.body.name) || '';
187187

188188
if (psw !== repsw) {
189189
await this.ctx.render('sign/reset', {

app/view/sign/reset.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- partial('../sign/sidebar') %>
1+
<%- include('./sidebar.html') %>
22

33
<div id='content'>
44
<div class='panel'>

test/app/controller/sign.test.js

Lines changed: 109 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
'use strict';
22

33
const { app, assert } = require('egg-mock/bootstrap');
4+
const utility = require('utility');
45

56
describe('test/app/controller/sign.test.js', () => {
67
const loginname = 'loginname' + Date.now();
78
const email = `${loginname}@email.com`;
9+
const pass = 'pass';
10+
let ctx,
11+
key;
12+
13+
before(async () => {
14+
ctx = app.mockContext();
15+
});
816

917
it('should GET /signup', async () => {
1018
await app.httpRequest()
@@ -18,6 +26,41 @@ describe('test/app/controller/sign.test.js', () => {
1826
.expect(403);
1927
});
2028

29+
it('should POST /signup ok', async () => {
30+
app.mockCsrf();
31+
const res = await app.httpRequest()
32+
.post('/signup')
33+
.send({
34+
loginname,
35+
email,
36+
pass,
37+
re_pass: pass,
38+
});
39+
40+
assert(res.statusCode === 200);
41+
assert(res.text.includes('欢迎加入 CNode技术社区!我们已给您的注册邮箱发送了一封邮件,请点击里面的链接来激活您的帐号'));
42+
});
43+
44+
it('should GET /active_account', async () => {
45+
let res = await app.httpRequest()
46+
.get('/active_account');
47+
assert(res.text.includes('用户不存在'));
48+
49+
res = await app.httpRequest()
50+
.get(`/active_account?name=${loginname}`);
51+
assert(res.text.includes('信息有误,帐号无法被激活。'));
52+
53+
const user = await ctx.service.user.getUserByLoginName(loginname);
54+
const activeKey = utility.md5(email + user.pass + app.config.session_secret);
55+
res = await app.httpRequest()
56+
.get(`/active_account?name=${loginname}&key=${activeKey}`);
57+
assert(res.text.includes('帐号已被激活,请登录'));
58+
59+
res = await app.httpRequest()
60+
.get(`/active_account?name=${loginname}&key=${activeKey}`);
61+
assert(res.text.includes('帐号已经是激活状态。'));
62+
});
63+
2164
it('should GET /signin', async () => {
2265
await app.httpRequest()
2366
.get('/signin')
@@ -31,22 +74,71 @@ describe('test/app/controller/sign.test.js', () => {
3174
});
3275

3376
it('should GET /search_pass', async () => {
34-
const res = await app.httpRequest()
35-
.get('/search_pass');
36-
assert(res.statusCode === 200);
77+
await app.httpRequest()
78+
.get('/search_pass').expect(200);
79+
});
80+
81+
it('should POST /search_pass', async () => {
82+
app.mockCsrf();
83+
let res = await app
84+
.httpRequest()
85+
.post('/search_pass')
86+
.send({ email: 'test' });
87+
88+
assert(res.text.includes('邮箱不合法'));
89+
90+
res = await app
91+
.httpRequest()
92+
.post('/search_pass')
93+
.send({ email: '[email protected]' });
94+
95+
assert(res.text.includes('没有这个电子邮箱。'));
96+
97+
res = await app
98+
.httpRequest()
99+
.post('/search_pass')
100+
.send({ email });
101+
102+
assert(res.text.includes('我们已给您填写的电子邮箱发送了一封邮件,请在24小时内点击里面的链接来重置密码。'));
37103
});
38104

39105
it('should GET /reset_pass', async () => {
40-
const res = await app.httpRequest()
106+
let res = await app.httpRequest()
41107
.get('/reset_pass');
42108
assert(res.statusCode === 403);
43109
assert(res.text.includes('信息有误,密码无法重置。'));
110+
111+
const user = await ctx.service.user.getUserByLoginName(loginname);
112+
key = user.retrieve_key;
113+
res = await app.httpRequest()
114+
.get(`/reset_pass?name=${loginname}&key=${key}`);
115+
assert(res.statusCode === 200);
116+
117+
await ctx.model.User.findOneAndUpdate({ loginname }, { retrieve_time: '' }).exec();
118+
res = await app.httpRequest()
119+
.get(`/reset_pass?name=${loginname}&key=${key}`);
120+
assert(res.text.includes('该链接已过期,请重新申请。'));
44121
});
45122

46-
it('should GET /active_account', async () => {
47-
const res = await app.httpRequest()
48-
.get('/active_account');
49-
assert(res.text.includes('用户不存在'));
123+
it('should POST /reset_pass', async () => {
124+
app.mockCsrf();
125+
let res = await app
126+
.httpRequest()
127+
.post('/reset_pass')
128+
.send({ psw: 'hi', repsw: 'hihi', name: loginname, key: 'key' });
129+
assert(res.text.includes('两次密码输入不一致。'));
130+
131+
res = await app
132+
.httpRequest()
133+
.post('/reset_pass')
134+
.send({ psw: 'hihi', repsw: 'hihi', name: loginname, key: 'key' });
135+
assert(res.text.includes('错误的激活链接'));
136+
137+
res = await app
138+
.httpRequest()
139+
.post('/reset_pass')
140+
.send({ psw: 'hihi', repsw: 'hihi', name: loginname, key });
141+
assert(res.text.includes('你的密码已重置。'));
50142
});
51143

52144
it('should POST /signup', async () => {
@@ -68,8 +160,8 @@ describe('test/app/controller/sign.test.js', () => {
68160
.send({
69161
loginname: 'logi',
70162
71-
pass: '123456',
72-
re_pass: '123456',
163+
pass,
164+
re_pass: pass,
73165
});
74166

75167
assert(res.statusCode === 422);
@@ -84,8 +176,8 @@ describe('test/app/controller/sign.test.js', () => {
84176
.send({
85177
loginname: 'login@name',
86178
87-
pass: '123456',
88-
re_pass: '123456',
179+
pass,
180+
re_pass: pass,
89181
});
90182

91183
assert(res.statusCode === 422);
@@ -100,8 +192,8 @@ describe('test/app/controller/sign.test.js', () => {
100192
.send({
101193
loginname: 'loginname',
102194
email: 'invalid_email',
103-
pass: '123456',
104-
re_pass: '123456',
195+
pass,
196+
re_pass: pass,
105197
});
106198

107199
assert(res.statusCode === 422);
@@ -116,30 +208,14 @@ describe('test/app/controller/sign.test.js', () => {
116208
.send({
117209
loginname: 'loginname',
118210
119-
pass: '123456',
211+
pass,
120212
re_pass: '1234567',
121213
});
122214

123215
assert(res.statusCode === 422);
124216
assert(res.text.includes('两次密码输入不一致。'));
125217
});
126218

127-
it('should POST /signup ok', async () => {
128-
app.mockCsrf();
129-
const res = await app.httpRequest()
130-
.post('/signup')
131-
.type('form')
132-
.send({
133-
loginname,
134-
email,
135-
pass: '123456',
136-
re_pass: '123456',
137-
});
138-
139-
assert(res.statusCode === 200);
140-
assert(res.text.includes('欢迎加入 CNode技术社区!我们已给您的注册邮箱发送了一封邮件,请点击里面的链接来激活您的帐号'));
141-
});
142-
143219
it('should POST /signup user or email in use', async () => {
144220
app.mockCsrf();
145221
const res = await app.httpRequest()
@@ -148,8 +224,8 @@ describe('test/app/controller/sign.test.js', () => {
148224
.send({
149225
loginname,
150226
email,
151-
pass: '123456',
152-
re_pass: '123456',
227+
pass,
228+
re_pass: pass,
153229
});
154230

155231
assert(res.statusCode === 422);

0 commit comments

Comments
 (0)