Skip to content

Commit 2ad70ee

Browse files
sinchangJacksonTian
authored andcommitted
test: improve controller topic unit test cov (#59)
* test: f * test: f * chore: fix lint * test: f * test: f
1 parent 12df9c6 commit 2ad70ee

File tree

9 files changed

+279
-176
lines changed

9 files changed

+279
-176
lines changed

app/controller/topic.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const Controller = require('egg').Controller;
44
const _ = require('lodash');
5-
const validator = require('validator');
5+
// const validator = require('validator');
66
const path = require('path');
77
const fs = require('fs');
88
const awaitWriteStream = require('await-stream-ready').write;
@@ -27,7 +27,7 @@ class TopicController extends Controller {
2727

2828
if (topic_id.length !== 24) {
2929
ctx.status = 404;
30-
// '此话题不存在或已被删除。'
30+
ctx.message = '此话题不存在或已被删除。';
3131
return;
3232
}
3333

@@ -40,10 +40,9 @@ class TopicController extends Controller {
4040

4141
topic.author = author;
4242
topic.replies = replies;
43-
4443
// 点赞数排名第三的回答,它的点赞数就是阈值
45-
topic.reply_up_threshold = (function() {
46-
let allUpCount = replies.map(function(reply) {
44+
topic.reply_up_threshold = (() => {
45+
let allUpCount = replies.map(reply => {
4746
return (reply.ups && reply.ups.length) || 0;
4847
});
4948
allUpCount = _.sortBy(allUpCount, Number).reverse();
@@ -60,7 +59,7 @@ class TopicController extends Controller {
6059
const other_topics = await service.topic.getTopicsByQuery(query, options);
6160

6261
// get no_reply_topics
63-
let no_reply_topics = service.cache.get('no_reply_topics');
62+
let no_reply_topics = await service.cache.get('no_reply_topics');
6463
if (!no_reply_topics) {
6564
const query = { reply_count: 0, tab: { $nin: [ 'job', 'dev' ] } };
6665
const options = { limit: 5, sort: '-create_at' };
@@ -72,7 +71,7 @@ class TopicController extends Controller {
7271
if (!currentUser) {
7372
is_collect = null;
7473
} else {
75-
is_collect = service.topicCollect.getTopicCollect(
74+
is_collect = await service.topicCollect.getTopicCollect(
7675
currentUser._id,
7776
topic_id
7877
);
@@ -103,9 +102,9 @@ class TopicController extends Controller {
103102
async put() {
104103
const { ctx, service } = this;
105104
const { tabs } = this.config;
106-
const title = validator.trim(ctx.body.title);
107-
const tab = validator.trim(ctx.body.tab);
108-
const content = validator.trim(ctx.body.t_content);
105+
const title = ctx.request.body.title.trim();
106+
const tab = ctx.request.body.tab.trim();
107+
const content = ctx.request.body.t_content.trim();
109108

110109
// 得到所有的 tab, e.g. ['ask', 'share', ..]
111110
const allTabs = tabs.map(function(tPair) {
@@ -121,7 +120,7 @@ class TopicController extends Controller {
121120
} else if (!tab || allTabs.indexOf(tab) === -1) {
122121
editError = '必须选择一个版块。';
123122
} else if (content === '') {
124-
editError = '内容不可为空';
123+
editError = '内容不可为空';
125124
}
126125
// END 验证
127126

@@ -155,6 +154,8 @@ class TopicController extends Controller {
155154
topic._id,
156155
ctx.user._id
157156
);
157+
158+
await ctx.redirect('/topic/' + topic._id);
158159
}
159160

160161
/**
@@ -165,6 +166,7 @@ class TopicController extends Controller {
165166
const topic_id = ctx.params.tid;
166167

167168
const { topic } = await service.topic.getTopicById(topic_id);
169+
168170
if (!topic) {
169171
ctx.status = 404;
170172
ctx.message = '此话题不存在或已被删除。';
@@ -185,7 +187,7 @@ class TopicController extends Controller {
185187
});
186188
} else {
187189
ctx.status = 403;
188-
// ctx.message = '对不起,你不能编辑此话题';
190+
ctx.message = '对不起,你不能编辑此话题';
189191
}
190192
}
191193

@@ -208,11 +210,11 @@ class TopicController extends Controller {
208210
}
209211

210212
if (
211-
topic.author_id === ctx.user._id || ctx.user.is_admin
213+
topic.author_id.toString() === ctx.user._id.toString() || ctx.user.is_admin
212214
) {
213-
title = validator.trim(title);
214-
tab = validator.trim(tab);
215-
content = validator.trim(content);
215+
title = title.trim();
216+
tab = tab.trim();
217+
content = content.trim();
216218

217219
// 验证
218220
let editError;
@@ -222,6 +224,8 @@ class TopicController extends Controller {
222224
editError = '标题字数太多或太少。';
223225
} else if (!tab) {
224226
editError = '必须选择一个版块。';
227+
} else if (content === '') {
228+
editError = '内容不可为空。';
225229
}
226230
// END 验证
227231

@@ -247,7 +251,8 @@ class TopicController extends Controller {
247251
await service.at.sendMessageToMentionUsers(
248252
content,
249253
topic._id,
250-
ctx.user._id
254+
ctx.user._id,
255+
'at'
251256
);
252257

253258
ctx.redirect('/topic/' + topic._id);
@@ -269,18 +274,18 @@ class TopicController extends Controller {
269274

270275
const [ topic, author ] = await service.topic.getFullTopic(topic_id);
271276

277+
if (!topic) {
278+
ctx.status = 422;
279+
ctx.body = { message: '此话题不存在或已被删除。', success: false };
280+
return;
281+
}
282+
272283
if (
273284
!ctx.user.is_admin &&
274285
!topic.author_id.equals(ctx.user._id)
275286
) {
276287
ctx.status = 403;
277-
ctx.body = { success: false, message: '无权限' };
278-
return;
279-
}
280-
281-
if (!topic) {
282-
ctx.status = 422;
283-
ctx.body = { success: false, message: '此话题不存在或已被删除。' };
288+
ctx.body = { message: '无权限', success: false };
284289
return;
285290
}
286291

@@ -291,7 +296,8 @@ class TopicController extends Controller {
291296
topic.deleted = true;
292297

293298
await topic.save();
294-
ctx.body = { success: true, message: '话题已被删除。' };
299+
300+
ctx.body = { message: '话题已被删除。', success: true };
295301
}
296302

297303
/**
@@ -302,12 +308,6 @@ class TopicController extends Controller {
302308
const topic_id = ctx.params.tid;
303309
const referer = ctx.get('referer');
304310

305-
if (topic_id.length !== 24) {
306-
ctx.status = 404;
307-
ctx.message = '此话题不存在或已被删除。';
308-
return;
309-
}
310-
311311
const topic = await service.topic.getTopic(topic_id);
312312

313313
if (!topic) {
@@ -372,6 +372,7 @@ class TopicController extends Controller {
372372

373373
if (!topic) {
374374
ctx.body = { status: 'failed' };
375+
return;
375376
}
376377

377378
const doc = await service.topicCollect.getTopicCollect(
@@ -403,20 +404,23 @@ class TopicController extends Controller {
403404

404405
if (!topic) {
405406
ctx.body = { status: 'failed' };
407+
return;
406408
}
407409

408-
const removeResult = service.topic_collect.remove(
410+
const removeResult = await service.topicCollect.remove(
409411
ctx.user._id,
410412
topic._id
411413
);
414+
412415
if (removeResult.result.n === 0) {
413416
ctx.body = { status: 'failed' };
417+
return;
414418
}
415419

416420
const user = await service.user.getUserById(ctx.user._id);
417421

418422
user.collect_topic_count -= 1;
419-
ctx.user = user;
423+
// ctx.user = user;
420424
await user.save();
421425

422426
topic.collect_count -= 1;

app/middleware/error_page.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
'use strict';
22

3+
const status = [ 404, 403 ];
4+
35
module.exports = () => {
46
return async function errorPage(ctx, next) {
57
await next();
6-
if (ctx.status === 404 && !ctx.body) {
8+
if ((status.indexOf(ctx.status) > -1) && !ctx.body) {
79
const { message } = ctx;
10+
ctx.status = ctx.status;
811
if (ctx.acceptJSON) {
912
ctx.body = { error: 'Not Found' };
1013
} else {
11-
ctx.status = 404;
1214
await ctx.render('notify/notify', { error: message });
1315
}
1416
}

app/router.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ module.exports = app => {
8080
// // 保存新建的文章
8181
// router.post('/topic/create', userRequired, limit.peruserperday('create_topic', config.create_post_per_day, { showJson: false }), topic.put);
8282

83+
router.post('/topic/create', userRequired, topic.put);
84+
8385
router.post('/topic/:tid/edit', userRequired, topic.update);
8486
router.post('/topic/collect', userRequired, topic.collect); // 关注某话题
8587
router.post('/topic/de_collect', userRequired, topic.de_collect); // 取消关注某话题

app/service/topic.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ class TopicService extends Service {
9393
const topic = await this.ctx.model.Topic.findOne(query);
9494

9595
if (!topic) {
96-
throw new Error('此话题不存在或已被删除。');
96+
// throw new Error('此话题不存在或已被删除。');
97+
return [];
9798
}
9899

99100
topic.linkedContent = this.service.at.linkUsers(topic.content);
100101

101102
const author = await this.service.user.getUserById(topic.author_id);
102103
if (!author) {
103-
throw new Error('话题的作者丢了。');
104+
// throw new Error('话题的作者丢了。');
105+
return [];
104106
}
105107

106108
const replies = await this.service.reply.getRepliesByTopicId(topic._id);

app/view/reply/reply.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
reply_id="<%= reply._id %>" reply_to_id="<%= reply.reply_id || '' %>" id="<%= reply._id %>">
44
<div class='author_content'>
55
<a href="/user/<%= reply.author.loginname %>" class="user_avatar">
6-
<img src="<%= proxy(reply.author.avatar_url) %>" title="<%= reply.author.loginname %>"/></a>
6+
<img src="<%= helper.proxy(reply.author.avatar_url) %>" title="<%= reply.author.loginname %>"/></a>
77

88
<div class='user_info'>
99
<a class='dark reply_author' href="/user/<%= reply.author.loginname %>"><%= reply.author.loginname %></a>
10-
<a class="reply_time" href="#<%= reply._id %>"><%= indexInCollection + 1 %>楼•<%= helper.ago(reply.create_at)
10+
<a class="reply_time" href="#<%= reply._id %>"><%= index + 1 %>楼•<%= helper.ago(reply.create_at)
1111
%></a>
1212
<% if (reply.author.loginname == topic.author.loginname) { %>
1313
<span class="reply_by_author">作者</span>
@@ -40,7 +40,7 @@
4040
</div>
4141
</div>
4242
<div class='reply_content from-<%= reply.author.loginname %>'>
43-
<%- markdown(reply.content) %>
43+
<%- helper.markdown(reply.content) %>
4444
</div>
4545
<div class='clearfix'>
4646
<div class='reply2_area'>

app/view/topic/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<span class='col_fade'>作者</span>
55
</div>
66
<div class='inner'>
7-
<%- include('../user/card.html', { object: topic.author, as: 'user' }) %>
7+
<%- include('../user/card.html', { user: topic.author }) %>
88
</div>
99
</div>
1010

@@ -129,7 +129,7 @@
129129
<div class='inner topic'>
130130

131131
<div class='topic_content'>
132-
<%- markdown(topic.linkedContent) %>
132+
<%- helper.markdown(topic.linkedContent) %>
133133
</div>
134134
</div>
135135
</div>
@@ -138,7 +138,9 @@
138138
<div class='header'>
139139
<span class='col_fade'><%= topic.replies.length %> 回复</span>
140140
</div>
141-
<%- include('../reply/reply.html', topic.replies) %>
141+
<% topic.replies.map((reply, index) => { %>
142+
<%- include('../reply/reply.html', { reply, index }) %>
143+
<%}) %>
142144
</div>
143145
<% } %>
144146
<% if (current_user && typeof topic !== 'undefined') { %>

app/view/user/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@
9090
<span class="col_fade">最近创建的话题</span>
9191
</div>
9292
<% if (typeof recent_topics !== 'undefined' && recent_topics.length > 0) { %>
93-
<%- include('../topic/abstract', { topic: recent_topics }) %>
93+
<% recent_topics.map((topic, index) => { %>
94+
<%- include('../topic/abstract.html', { topic }) %>
95+
<%}) %>
9496
<div class='cell more'>
9597
<a class='dark' href="/user/<%= user.loginname %>/topics">查看更多»</a>
9698
</div>
@@ -105,7 +107,9 @@
105107
<span class="col_fade">最近参与的话题</span>
106108
</div>
107109
<% if (typeof recent_replies !== 'undefined' && recent_replies.length > 0) { %>
108-
<%- include('../topic/abstract', { topic: recent_replies }) %>
110+
<% recent_replies.map((topic, index) => { %>
111+
<%- include('../topic/abstract.html', { topic }) %>
112+
<%}) %>
109113
<div class='cell more'>
110114
<a class='dark' href="/user/<%= user.loginname %>/replies">查看更多»</a>
111115
</div>

0 commit comments

Comments
 (0)