Skip to content

Commit 2f7dd8c

Browse files
authored
make the test case pass (#60)
1 parent 2d30782 commit 2f7dd8c

File tree

6 files changed

+65
-72
lines changed

6 files changed

+65
-72
lines changed

app/controller/topic.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TopicController extends Controller {
2323
}
2424
const { ctx, service } = this;
2525
const topic_id = ctx.params.tid;
26-
const currentUser = ctx.session.user;
26+
const currentUser = ctx.user;
2727

2828
if (topic_id.length !== 24) {
2929
ctx.status = 404;
@@ -139,23 +139,23 @@ class TopicController extends Controller {
139139
title,
140140
content,
141141
tab,
142-
ctx.session.user._id
142+
ctx.user._id
143143
);
144144

145145
// 发帖用户增加积分,增加发表主题数量
146146
const author = await service.user.getUserById(topic.author_id);
147147
author.score += 5;
148148
author.topic_count += 1;
149149
await author.save();
150-
ctx.session.user = author;
150+
ctx.user = author;
151151

152152
ctx.redirect('/topic/' + topic._id);
153153

154154
// 通知被@的用户
155155
await service.at.sendMessageToMentionUsers(
156156
content,
157157
topic._id,
158-
ctx.session.user._id
158+
ctx.user._id
159159
);
160160
}
161161

@@ -174,8 +174,8 @@ class TopicController extends Controller {
174174
}
175175

176176
if (
177-
String(topic.author_id) === String(ctx.session.user._id) ||
178-
ctx.session.user.is_admin
177+
String(topic.author_id) === String(ctx.user._id) ||
178+
ctx.user.is_admin
179179
) {
180180
await ctx.render('topic/edit', {
181181
action: 'edit',
@@ -210,7 +210,7 @@ class TopicController extends Controller {
210210
}
211211

212212
if (
213-
topic.author_id === ctx.session.user._id || ctx.session.user.is_admin
213+
topic.author_id === ctx.user._id || ctx.user.is_admin
214214
) {
215215
title = validator.trim(title);
216216
tab = validator.trim(tab);
@@ -249,7 +249,7 @@ class TopicController extends Controller {
249249
await service.at.sendMessageToMentionUsers(
250250
content,
251251
topic._id,
252-
ctx.session.user._id
252+
ctx.user._id
253253
);
254254

255255
ctx.redirect('/topic/' + topic._id);
@@ -272,8 +272,8 @@ class TopicController extends Controller {
272272
const [ topic, author ] = await service.topic.getFullTopic(topic_id);
273273

274274
if (
275-
!ctx.session.user.is_admin &&
276-
!topic.author_id.equals(ctx.session.user._id)
275+
!ctx.user.is_admin &&
276+
!topic.author_id.equals(ctx.user._id)
277277
) {
278278
ctx.status = 403;
279279
ctx.body = { success: false, message: '无权限' };
@@ -376,8 +376,8 @@ class TopicController extends Controller {
376376
ctx.body = { status: 'failed' };
377377
}
378378

379-
const doc = await service.topic_collect.getTopicCollect(
380-
ctx.session.user._id,
379+
const doc = await service.topicCollect.getTopicCollect(
380+
ctx.user._id,
381381
topic._id
382382
);
383383

@@ -386,14 +386,14 @@ class TopicController extends Controller {
386386
return;
387387
}
388388

389-
service.topic_collect.newAndSave(ctx.session.user._id, topic._id);
389+
await service.topicCollect.newAndSave(ctx.user._id, topic._id);
390390
ctx.body = { status: 'success' };
391391

392-
const user = await service.user.getUserById(ctx.session.user._id);
392+
const user = await service.user.getUserById(ctx.user._id);
393393
user.collect_topic_count += 1;
394394
await user.save();
395395

396-
ctx.session.user.collect_topic_count += 1;
396+
ctx.user.collect_topic_count += 1;
397397
topic.collect_count += 1;
398398
await topic.save();
399399
}
@@ -411,17 +411,17 @@ class TopicController extends Controller {
411411
}
412412

413413
const removeResult = service.topic_collect.remove(
414-
ctx.session.user._id,
414+
ctx.user._id,
415415
topic._id
416416
);
417417
if (removeResult.result.n === 0) {
418418
ctx.body = { status: 'failed' };
419419
}
420420

421-
const user = await service.user.getUserById(ctx.session.user._id);
421+
const user = await service.user.getUserById(ctx.user._id);
422422

423423
user.collect_topic_count -= 1;
424-
ctx.session.user = user;
424+
ctx.user = user;
425425
await user.save();
426426

427427
topic.collect_count -= 1;

app/view/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
<% if (typeof topics !== 'undefined' && topics.length > 0) { %>
1414
<div class="inner no-padding">
1515
<%- include('./topic/list.html', {
16-
topics: topics,
17-
pages: pages,
18-
current_page: current_page,
19-
base: '/'
16+
topics: topics,
17+
pages: pages,
18+
current_page: current_page,
19+
base: '/'
2020
}) %>
2121
</div>
2222
<% } else { %>

app/view/topic/abstract.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class='cell'>
22

33
<a class="user_avatar pull-left" href="/user/<%= topic.author.loginname %>">
4-
<img src="<%= proxy(topic.author.avatar_url) %>"
4+
<img src="<%= helper.proxy(topic.author.avatar_url) %>"
55
title="<%= topic.author.loginname %>"
66
/>
77
</a>
@@ -18,7 +18,7 @@
1818

1919
<% if (topic.reply && topic.reply.author) {%>
2020
<a class='last_time pull-right' href="/topic/<%= topic._id %><%- '#' + topic.reply._id %>">
21-
<img class="user_small_avatar" src="<%= proxy(topic.reply.author.avatar_url) %>">
21+
<img class="user_small_avatar" src="<%= helper.proxy(topic.reply.author.avatar_url) %>">
2222
<span class="last_active_time"><%= topic.reply.create_at_ago() %></span>
2323
</a>
2424
<% } %>

app/view/topic/list.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<div id="topic_list">
2-
<%- include('./abstract.html', {collection:topics, as:'topic'}) %>
2+
<% for (var i = 0; i < topics.length; i++) { %>
3+
<%- include('./abstract.html', { topic: topics[i] }) %>
4+
<% } %>
35
</div>
46
<div class='pagination' current_page='<%= current_page %>'>
57
<ul>

test/app/controller/topic.test.js

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

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

55
function randomInt() {
66
return (Math.random() * 10000).toFixed(0);
@@ -13,7 +13,7 @@ describe('test/app/controller/topic.test.js', () => {
1313
key,
1414
username,
1515
user,
16-
// topic_collect,
16+
admin,
1717
topic;
1818

1919
before(async () => {
@@ -38,13 +38,15 @@ describe('test/app/controller/topic.test.js', () => {
3838
user_id
3939
);
4040

41-
// topic_collect = ctx.service.topic_collect.newAndSave(
42-
// user_id,
43-
// topic_id
44-
// );
41+
await ctx.service.topicCollect.newAndSave(
42+
user_id,
43+
topic_id
44+
);
4545
topic_id = topic._id;
4646

47+
admin = Object.assign(user, { is_admin: true });
4748
});
49+
4850
it('should GET /topic/:tid ok', async () => {
4951
await app.httpRequest().get('/topic/:tid').expect(404);
5052
});
@@ -58,50 +60,44 @@ describe('test/app/controller/topic.test.js', () => {
5860
});
5961

6062
it('should GET /topic/create ok', async () => {
61-
app.mockSession({
62-
user: {
63-
name: username,
64-
_id: user_id,
65-
is_admin: true,
66-
},
63+
app.mockUser({
64+
name: username,
65+
_id: user_id,
66+
is_admin: true,
6767
});
68-
await app.httpRequest().get('/topic/create').expect(200);
68+
const res = await app.httpRequest().get('/topic/create');
69+
assert(res.status === 200);
6970
});
7071

7172
it('should POST /topic/:tid/top ok', async () => {
72-
app.mockSession({
73-
user: {
74-
name: username,
75-
_id: user_id,
76-
is_admin: true,
77-
},
73+
app.mockUser({
74+
name: username,
75+
_id: user_id,
76+
is_admin: true,
7877
});
7978
app.mockCsrf();
8079
await app.httpRequest().post(`/topic/${topic_id}/top`).expect(200);
8180
});
8281

8382
it('should POST /topic/:tid/good ok', async () => {
84-
app.mockSession({
85-
user: {
86-
name: username,
87-
_id: user_id,
88-
is_admin: true,
89-
},
83+
app.mockUser({
84+
name: username,
85+
_id: user_id,
86+
is_admin: true,
9087
});
9188
app.mockCsrf();
9289
await app.httpRequest().post(`/topic/${topic_id}/good`).expect(200);
9390
});
9491

9592
it('should GET /topic/:tid/edit ok', async () => {
96-
app.mockSession({
97-
user: {
98-
name: username,
99-
_id: user_id,
100-
is_admin: true,
101-
},
93+
app.mockUser({
94+
name: username,
95+
_id: user_id,
96+
is_admin: true,
10297
});
10398

104-
await app.httpRequest().get(`/topic/${topic_id}/edit`).expect(200);
99+
const res = await app.httpRequest().get(`/topic/${topic_id}/edit`);
100+
assert(res.status === 200);
105101
});
106102

107103
it('should GET /topic/:tid/edit ok', async () => {
@@ -129,24 +125,20 @@ describe('test/app/controller/topic.test.js', () => {
129125

130126

131127
it('should POST /topic/:tid/delete ok', async () => {
132-
app.mockSession({
133-
user: {
134-
name: username,
135-
_id: user_id,
136-
is_admin: true,
137-
},
128+
app.mockUser({
129+
name: username,
130+
_id: user_id,
131+
is_admin: true,
138132
});
139133
app.mockCsrf();
140134
await app.httpRequest().post(`/topic/${topic_id}/delete`).expect(200);
141135
});
142136

143137
it('should POST /topic/:tid/edit ok', async () => {
144-
app.mockSession({
145-
user: {
146-
name: username,
147-
_id: user_id,
148-
is_admin: true,
149-
},
138+
app.mockUser({
139+
name: username,
140+
_id: user_id,
141+
is_admin: true,
150142
});
151143
app.mockCsrf();
152144
await app

test/app/service/topic.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ describe('test/app/service/topic.test.js', () => {
2121
assert(result.loginname === loginname);
2222
});
2323

24-
2524
it('newAndSave should ok', async () => {
2625
const title = 'first post';
2726
const content = 'hello world';
@@ -73,10 +72,10 @@ describe('test/app/service/topic.test.js', () => {
7372
assert(result1.length >= 1);
7473

7574
const query2 = {
76-
good: 'test',
75+
good: true,
7776
};
7877
const result2 = await topic.getTopicsByQuery(query2, {});
79-
assert(result2.length === 0);
78+
assert(result2.length < result1.length);
8079
});
8180

8281
it('getLimit5w should ok', async () => {

0 commit comments

Comments
 (0)