Skip to content

Commit bf13548

Browse files
lqs469JacksonTian
authored andcommitted
Test: Improve controller/user ut coverage & bug fix (#75)
* test: improve contorller user ut coverage * fix: use arrow func
1 parent b6cf1c5 commit bf13548

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

app/controller/user.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class UserController extends Controller {
3232
query = { _id: { $in: topic_ids } };
3333
let recent_replies = await service.topic.getTopicsByQuery(query, {});
3434

35-
recent_replies = _.sortBy(recent_replies, function(topic) {
35+
recent_replies = _.sortBy(recent_replies, topic => {
3636
return topic_ids.indexOf(topic._id.toString());
3737
});
3838

39-
user.url = (function() {
39+
user.url = (() => {
4040
if (user.url && user.url.indexOf('http') !== 0) {
4141
return 'http://' + user.url;
4242
}
@@ -92,13 +92,14 @@ class UserController extends Controller {
9292
const opt = { skip: (page - 1) * limit, limit };
9393

9494
const collects = await service.topicCollect.getTopicCollectsByUserId(user._id, opt);
95-
const ids = collects.map(function(doc) {
96-
return String(doc.topic_id);
95+
const ids = collects.map(doc => {
96+
return doc.topic_id.toString();
9797
});
98+
9899
const query = { _id: { $in: ids } };
99100
let topics = await service.topic.getTopicsByQuery(query, {});
100-
topics = _.sortBy(topics, function(topic) {
101-
return ids.indexOf(String(topic._id));
101+
topics = _.sortBy(topics, topic => {
102+
return ids.indexOf(topic._id.toString());
102103
});
103104

104105
await ctx.render('user/collect_topics', {
@@ -154,13 +155,13 @@ class UserController extends Controller {
154155

155156
const opt = { skip: (page - 1) * limit, limit, sort: '-create_at' };
156157
const replies = await service.reply.getRepliesByAuthorId(user._id, opt);
157-
const topic_ids = [ ...new Set([ ...replies.map(function(reply) {
158+
const topic_ids = [ ...new Set(replies.map(reply => {
158159
return reply.topic_id.toString();
159-
}) ]) ];
160+
})) ];
160161
// 获取所有有评论的主题
161162
const query = { _id: { $in: topic_ids } };
162163
let topics = await service.topic.getTopicsByQuery(query, {});
163-
topics = _.sortBy(topics, function(topic) {
164+
topics = _.sortBy(topics, topic => {
164165
return topic_ids.indexOf(topic._id.toString());
165166
});
166167
const count = await service.reply.getCountByAuthorId(user._id);

config/config.default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,7 @@ module.exports = appInfo => {
141141
secret: process.env.EGG_ALINODE_SECRET || '',
142142
};
143143

144+
config.list_topic_count = 20;
145+
144146
return config;
145147
};

test/app/controller/user.test.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ describe('test/app/controller/user.test.js', () => {
1818

1919
describe('- Index', () => {
2020
it('should GET /user ok', async () => {
21+
app.mockContext({ user: { is_admin: true } });
2122
user.url = 'test_url';
2223
await user.save();
24+
const topic = await ctx.service.topic.newAndSave('title', 'content', 'share', user._id);
25+
await ctx.service.reply.newAndSave('content', topic._id, user._id);
2326

2427
const r1 = await app.httpRequest().get('/user');
2528
assert(r1.status === 404);
@@ -35,6 +38,22 @@ describe('test/app/controller/user.test.js', () => {
3538
assert(/<a class="dark">([^"]+)<\/a>/g.exec(res.text)[1] === user.loginname);
3639
assert(/<span class="big">([^"]+)<\/span> /g.exec(res.text)[1] === user.score.toString());
3740
assert(/([\S\s]+)/g.exec(res.text)[1].replace(/[\t\s]+/g, '') === '这家伙很懒,什么个性签名都没有留下。');
41+
42+
user.url = 'http://test_url.com';
43+
await user.save();
44+
const res1 = await app.httpRequest().get(`/user/${loginname}`);
45+
assert(res1.status === 200);
46+
47+
user.active = false;
48+
await user.save();
49+
const res2 = await app.httpRequest().get(`/user/${loginname}`);
50+
assert(res2.status === 200);
51+
const utility = require('utility');
52+
const token = utility.md5(user.email + user.pass + app.config.session_secret);
53+
assert(/href="\/active_account\?key=([^&]+)&/g.exec(res2.text)[1] === token);
54+
55+
user.active = true;
56+
await user.save();
3857
});
3958
});
4059

@@ -155,6 +174,34 @@ describe('test/app/controller/user.test.js', () => {
155174
const authUser = cookies.find(c => c.indexOf('$$$$') > -1);
156175
assert(/=([^$]+)\$/g.exec(authUser)[1] === user._id.toString());
157176
assert(login.headers.location === '/');
177+
178+
const login1 = await app.httpRequest().post('/passport/local').send({ name: user.loginname, pass: 'pass' });
179+
assert(login1.status === 302);
180+
assert(login1.headers.location === '/signin');
181+
182+
const login2 = await app.httpRequest().post('/passport/local').send({ name: user.email, pass: 'newpass' });
183+
assert(login2.status === 302);
184+
assert(login2.headers.location === '/');
185+
186+
const login3 = await app.httpRequest().post('/passport/local').send({ name: 'noExistedUser', pass: 'pass' });
187+
assert(login3.status === 302);
188+
assert(login3.headers.location === '/signin');
189+
190+
user.active = false;
191+
await user.save();
192+
const login4 = await app.httpRequest().post('/passport/local').send({ name: user.loginname, pass: 'newpass' });
193+
assert(login4.status === 302);
194+
assert(login4.headers.location === '/signin');
195+
});
196+
197+
it('should GET /passport/github redirect to auth url', async () => {
198+
const res = await ctx.app.httpRequest().get('/passport/github');
199+
assert(/^https:\/\/github.com\/login\/oauth\/authorize\?response_type=code&redirect_uri=http/.test(res.headers.location));
200+
assert(res.status === 302);
201+
202+
const res1 = await ctx.app.httpRequest().get('/passport/github/callback');
203+
assert(/^https:\/\/github.com\/login\/oauth\/authorize\?response_type=code&redirect_uri=http/.test(res1.headers.location));
204+
assert(res1.status === 302);
158205
});
159206

160207
it('should POST /user/set_star no_login reject', async () => {
@@ -226,7 +273,11 @@ describe('test/app/controller/user.test.js', () => {
226273
const noExistedUser = await app.httpRequest().get('/user/no_user/collections');
227274
assert(noExistedUser.status === 404);
228275
assert(/<strong>([\S\s]+)<\/strong>/g.exec(noExistedUser.text)[1] === '这个用户不存在。');
229-
await app.httpRequest().get(`/user/${user.loginname}/collections`).expect(200);
276+
277+
const topic = await ctx.service.topic.newAndSave('title', 'content', 'share', user._id);
278+
await ctx.service.topicCollect.newAndSave(user._id, topic._id);
279+
const res = await app.httpRequest().get(`/user/${user.loginname}/collections`);
280+
assert(res.status = 200);
230281
});
231282

232283
it('should GET /user/:name/topics ok', async () => {
@@ -240,7 +291,11 @@ describe('test/app/controller/user.test.js', () => {
240291
const noExistedUser = await app.httpRequest().get('/user/no_user/replies');
241292
assert(noExistedUser.status === 404);
242293
assert(/<strong>([\S\s]+)<\/strong>/g.exec(noExistedUser.text)[1] === '这个用户不存在。');
243-
await app.httpRequest().get(`/user/${user.loginname}/replies`).expect(200);
294+
295+
const topic = await ctx.service.topic.newAndSave('title', 'content', 'share', user._id);
296+
await ctx.service.reply.newAndSave('content', topic._id, user._id);
297+
const res = await app.httpRequest().get(`/user/${user.loginname}/replies`);
298+
assert(res.status === 200);
244299
});
245300
});
246301
});

0 commit comments

Comments
 (0)