Skip to content

Commit ab426ca

Browse files
committed
test: add test cases for sign controller
1 parent 5417eee commit ab426ca

File tree

4 files changed

+141
-12
lines changed

4 files changed

+141
-12
lines changed

app/controller/sign.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class SignController extends Controller {
2121

2222
async signup() {
2323
const { ctx, service, config } = this;
24-
const loginname = validator.trim(ctx.request.body.loginname).toLowerCase();
25-
const email = validator.trim(ctx.request.body.email).toLowerCase();
26-
const pass = validator.trim(ctx.request.body.pass);
27-
const rePass = validator.trim(ctx.request.body.re_pass);
24+
const loginname = validator.trim(ctx.request.body.loginname || '').toLowerCase();
25+
const email = validator.trim(ctx.request.body.email || '').toLowerCase();
26+
const pass = validator.trim(ctx.request.body.pass || '');
27+
const rePass = validator.trim(ctx.request.body.re_pass || '');
2828

2929
let msg;
3030
// 验证信息的正确性
@@ -90,12 +90,13 @@ class SignController extends Controller {
9090

9191
async activeAccount() {
9292
const { ctx, service, config } = this;
93-
const key = validator.trim(ctx.query.key);
94-
const name = validator.trim(ctx.query.name);
93+
const key = validator.trim(ctx.query.key || '');
94+
const name = validator.trim(ctx.query.name || '');
9595

9696
const user = await service.user.getUserByLoginName(name);
9797
if (!user) {
98-
throw new Error('[ACTIVE_ACCOUNT] no such user: ' + name);
98+
await ctx.render('notify/notify', { error: '用户不存在' });
99+
return;
99100
}
100101

101102
const passhash = user.pass;

app/service/at.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class AtService extends Service {
7272
* @return {String} 替换后的文本内容
7373
*/
7474
linkUsers(text) {
75-
const users = AtService.fetchUsers(text);
75+
const users = this.fetchUsers(text);
7676
for (let i = 0; i < users.length; i++) {
7777
const name = users[i];
7878
text = text.replace(

app/view/sign/search_pass.html

Lines changed: 3 additions & 3 deletions
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'>
@@ -9,7 +9,7 @@
99
</ul>
1010
</div>
1111
<div class='inner'>
12-
<% if (typeof(error) !== 'undefined' && error) { %>
12+
<% if (typeof error !== 'undefined' && error) { %>
1313
<div class="alert alert-error">
1414
<a class="close" data-dismiss="alert" href="#">&times;</a>
1515
<strong><%= error %></strong>
@@ -20,7 +20,7 @@
2020
<label class='control-label' for='email'>电子邮箱</label>
2121

2222
<div class='controls'>
23-
<% if (typeof(email) !== 'undefined') { %>
23+
<% if (typeof email !== 'undefined') { %>
2424
<input class='input-xlarge' id='email' name='email' size='30' type='text' value='<%= email %>'/>
2525
<% } else { %>
2626
<input class='input-xlarge' id='email' name='email' size='30' type='text'/>

test/app/controller/sign.test.js

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22

3-
const { app } = require('egg-mock/bootstrap');
3+
const { app, assert } = require('egg-mock/bootstrap');
44

55
describe('test/app/controller/sign.test.js', () => {
6+
const loginname = 'loginname' + Date.now();
7+
const email = `${loginname}@email.com`;
8+
69
it('should GET /signup', async () => {
710
await app.httpRequest()
811
.get('/signup')
@@ -27,4 +30,129 @@ describe('test/app/controller/sign.test.js', () => {
2730
.expect(302);
2831
});
2932

33+
it('should GET /search_pass', async () => {
34+
const res = await app.httpRequest()
35+
.get('/search_pass');
36+
assert(res.statusCode === 200);
37+
});
38+
39+
it('should GET /reset_pass', async () => {
40+
const res = await app.httpRequest()
41+
.get('/reset_pass');
42+
assert(res.statusCode === 403);
43+
assert(res.text.includes('信息有误,密码无法重置。'));
44+
});
45+
46+
it('should GET /active_account', async () => {
47+
const res = await app.httpRequest()
48+
.get('/active_account');
49+
assert(res.text.includes('用户不存在'));
50+
});
51+
52+
it('should POST /signup', async () => {
53+
app.mockCsrf();
54+
const res = await app.httpRequest()
55+
.post('/signup')
56+
.type('form')
57+
.send({});
58+
59+
assert(res.statusCode === 422);
60+
assert(res.text.includes('信息不完整。'));
61+
});
62+
63+
it('should POST /signup loginname less 5', async () => {
64+
app.mockCsrf();
65+
const res = await app.httpRequest()
66+
.post('/signup')
67+
.type('form')
68+
.send({
69+
loginname: 'logi',
70+
71+
pass: '123456',
72+
re_pass: '123456',
73+
});
74+
75+
assert(res.statusCode === 422);
76+
assert(res.text.includes('用户名至少需要5个字符。'));
77+
});
78+
79+
it('should POST /signup invalid loginname', async () => {
80+
app.mockCsrf();
81+
const res = await app.httpRequest()
82+
.post('/signup')
83+
.type('form')
84+
.send({
85+
loginname: 'login@name',
86+
87+
pass: '123456',
88+
re_pass: '123456',
89+
});
90+
91+
assert(res.statusCode === 422);
92+
assert(res.text.includes('用户名不合法。'));
93+
});
94+
95+
it('should POST /signup invalid email', async () => {
96+
app.mockCsrf();
97+
const res = await app.httpRequest()
98+
.post('/signup')
99+
.type('form')
100+
.send({
101+
loginname: 'loginname',
102+
email: 'invalid_email',
103+
pass: '123456',
104+
re_pass: '123456',
105+
});
106+
107+
assert(res.statusCode === 422);
108+
assert(res.text.includes('邮箱不合法。'));
109+
});
110+
111+
it('should POST /signup unmatch password', async () => {
112+
app.mockCsrf();
113+
const res = await app.httpRequest()
114+
.post('/signup')
115+
.type('form')
116+
.send({
117+
loginname: 'loginname',
118+
119+
pass: '123456',
120+
re_pass: '1234567',
121+
});
122+
123+
assert(res.statusCode === 422);
124+
assert(res.text.includes('两次密码输入不一致。'));
125+
});
126+
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+
143+
it('should POST /signup user or email in use', async () => {
144+
app.mockCsrf();
145+
const res = await app.httpRequest()
146+
.post('/signup')
147+
.type('form')
148+
.send({
149+
loginname,
150+
email,
151+
pass: '123456',
152+
re_pass: '123456',
153+
});
154+
155+
assert(res.statusCode === 422);
156+
assert(res.text.includes('用户名或邮箱已被使用。'));
157+
});
30158
});

0 commit comments

Comments
 (0)