mirror of
https://github.com/cnodejs/nodeclub.git
synced 2025-12-08 19:55:55 +00:00
修复主题收藏接口部分报500错误的问题,补充主题收藏接口测试用例
This commit is contained in:
parent
51f7bed982
commit
40fb6c8c6b
@ -2,8 +2,8 @@ var eventproxy = require('eventproxy');
|
||||
var TopicProxy = require('../../proxy').Topic;
|
||||
var TopicCollectProxy = require('../../proxy').TopicCollect;
|
||||
var UserProxy = require('../../proxy').User;
|
||||
|
||||
var _ = require('lodash');
|
||||
var validator = require('validator');
|
||||
|
||||
function list(req, res, next) {
|
||||
var loginname = req.params.loginname;
|
||||
@ -51,6 +51,12 @@ exports.list = list;
|
||||
|
||||
function collect(req, res, next) {
|
||||
var topic_id = req.body.topic_id;
|
||||
|
||||
if (!validator.isMongoId(topic_id)) {
|
||||
res.status(422);
|
||||
return res.send({success: false, error_msg: '不是有效的主题id'});
|
||||
}
|
||||
|
||||
TopicProxy.getTopic(topic_id, function (err, topic) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
@ -87,12 +93,18 @@ function collect(req, res, next) {
|
||||
topic.save();
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
exports.collect = collect;
|
||||
|
||||
function de_collect(req, res, next) {
|
||||
var topic_id = req.body.topic_id;
|
||||
|
||||
if (!validator.isMongoId(topic_id)) {
|
||||
res.status(422);
|
||||
return res.send({success: false, error_msg: '不是有效的主题id'});
|
||||
}
|
||||
|
||||
TopicProxy.getTopic(topic_id, function (err, topic) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
@ -119,6 +131,6 @@ function de_collect(req, res, next) {
|
||||
topic.collect_count -= 1;
|
||||
topic.save();
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
exports.de_collect = de_collect;
|
||||
|
||||
@ -4,19 +4,72 @@ var should = require('should');
|
||||
var support = require('../../support/support');
|
||||
|
||||
describe('test/api/v1/topic_collect.test.js', function () {
|
||||
|
||||
var mockUser, mockTopic;
|
||||
|
||||
before(function (done) {
|
||||
support.createUser(function (err, user) {
|
||||
mockUser = user;
|
||||
support.createTopic(user.id, function (err, topic) {
|
||||
mockTopic = topic;
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 主题被收藏之前
|
||||
describe('before collect topic', function () {
|
||||
|
||||
describe('get /topic_collect/:loginname', function () {
|
||||
|
||||
it('should list topic with length = 0', function (done) {
|
||||
request.get('/api/v1/topic_collect/' + mockUser.loginname)
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.success.should.true();
|
||||
res.body.data.length.should.equal(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('get /api/v1/topic/:topicid', function () {
|
||||
|
||||
it('should return topic info with is_collect = false', function (done) {
|
||||
request.get('/api/v1/topic/' + mockTopic.id)
|
||||
.query({
|
||||
accesstoken: mockUser.accessToken
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.success.should.true();
|
||||
res.body.data.is_collect.should.false();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// 收藏主题
|
||||
describe('post /topic_collect/collect', function () {
|
||||
it('should collect topic', function (done) {
|
||||
|
||||
it('should 401 with no accessToken', function (done) {
|
||||
request.post('/api/v1/topic_collect/collect')
|
||||
.send({
|
||||
topic_id: mockTopic.id
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.status.should.equal(401);
|
||||
res.body.success.should.false();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should collect topic with correct accessToken', function (done) {
|
||||
request.post('/api/v1/topic_collect/collect')
|
||||
.send({
|
||||
accesstoken: mockUser.accessToken,
|
||||
@ -24,54 +77,110 @@ describe('test/api/v1/topic_collect.test.js', function () {
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.should.eql({"success": true});
|
||||
res.body.success.should.true();
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it('do nothing when topic is not found', function (done) {
|
||||
it('should fail when topic_id is not valid', function (done) {
|
||||
request.post('/api/v1/topic_collect/collect')
|
||||
.send({
|
||||
accesstoken: support.normalUser.accessToken,
|
||||
topic_id: mockTopic.id + 'not_found'
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.status.should.equal(500);
|
||||
done();
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
describe('get /topic_collect/:loginname', function () {
|
||||
it('should list topic', function (done) {
|
||||
request.get('/api/v1/topic_collect/' + mockUser.loginname)
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
var collectTopicId = res.body.data[0].id;
|
||||
|
||||
collectTopicId.should.equal(mockTopic.id)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('get /api/v1/topic/:topicid', function () {
|
||||
it('should return topic info', function (done) {
|
||||
request.get('/api/v1/topic/' + mockTopic.id)
|
||||
.query({
|
||||
accesstoken: mockUser.accessToken,
|
||||
topic_id: mockTopic.id + "not_valid"
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.data.is_collect.should.true();
|
||||
res.status.should.equal(422);
|
||||
res.body.success.should.false();
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail when topic not found', function (done) {
|
||||
var notFoundTopicId = mockTopic.id.split("").reverse().join("");
|
||||
request.post('/api/v1/topic_collect/collect')
|
||||
.send({
|
||||
accesstoken: mockUser.accessToken,
|
||||
topic_id: notFoundTopicId
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
if (mockTopic.id === notFoundTopicId) { // 小概率事件id反转之后还不变
|
||||
res.body.success.should.true();
|
||||
} else {
|
||||
res.status.should.equal(404);
|
||||
res.body.success.should.false();
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// 主题被收藏之后
|
||||
describe('after collect topic', function () {
|
||||
|
||||
describe('get /topic_collect/:loginname', function () {
|
||||
|
||||
it('should list topic with length = 1', function (done) {
|
||||
request.get('/api/v1/topic_collect/' + mockUser.loginname)
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.success.should.true();
|
||||
res.body.data.length.should.equal(1);
|
||||
res.body.data[0].id.should.equal(mockTopic.id);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail when user not found', function (done) {
|
||||
request.get('/api/v1/topic_collect/' + mockUser.loginname + 'not_found')
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.status.should.equal(404);
|
||||
res.body.success.should.false();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('get /api/v1/topic/:topicid', function () {
|
||||
|
||||
it('should return topic info with is_collect = true', function (done) {
|
||||
request.get('/api/v1/topic/' + mockTopic.id)
|
||||
.query({
|
||||
accesstoken: mockUser.accessToken
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.success.should.true();
|
||||
res.body.data.is_collect.should.true();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// 取消收藏主题
|
||||
describe('post /topic_collect/de_collect', function () {
|
||||
it('should de_collect topic', function (done) {
|
||||
|
||||
it('should 401 with no accessToken', function (done) {
|
||||
request.post('/api/v1/topic_collect/de_collect')
|
||||
.send({
|
||||
topic_id: mockTopic.id
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.status.should.equal(401);
|
||||
res.body.success.should.false();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should decollect topic with correct accessToken', function (done) {
|
||||
request.post('/api/v1/topic_collect/de_collect')
|
||||
.send({
|
||||
accesstoken: mockUser.accessToken,
|
||||
@ -79,22 +188,80 @@ describe('test/api/v1/topic_collect.test.js', function () {
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.should.eql({"success": true});
|
||||
res.body.success.should.true();
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it('do nothing when topic is not found', function (done) {
|
||||
|
||||
it('should fail when topic_id is not valid', function (done) {
|
||||
request.post('/api/v1/topic_collect/de_collect')
|
||||
.send({
|
||||
accesstoken: support.normalUser.accessToken,
|
||||
topic_id: mockTopic.id + 'not_found'
|
||||
accesstoken: mockUser.accessToken,
|
||||
topic_id: mockTopic.id + "not_valid"
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.status.should.equal(500);
|
||||
res.status.should.equal(422);
|
||||
res.body.success.should.false();
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
it('should fail when topic not found', function (done) {
|
||||
var notFoundTopicId = mockTopic.id.split("").reverse().join("");
|
||||
request.post('/api/v1/topic_collect/de_collect')
|
||||
.send({
|
||||
accesstoken: mockUser.accessToken,
|
||||
topic_id: notFoundTopicId
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
if (mockTopic.id === notFoundTopicId) { // 小概率事件id反转之后还不变
|
||||
res.body.success.should.true();
|
||||
} else {
|
||||
res.status.should.equal(404);
|
||||
res.body.success.should.false();
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// 主题被取消收藏之后
|
||||
describe('after decollect topic', function () {
|
||||
|
||||
describe('get /topic_collect/:loginname', function () {
|
||||
|
||||
it('should list topic with length = 0', function (done) {
|
||||
request.get('/api/v1/topic_collect/' + mockUser.loginname)
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.success.should.true();
|
||||
res.body.data.length.should.equal(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('get /api/v1/topic/:topicid', function () {
|
||||
|
||||
it('should return topic info with is_collect = false', function (done) {
|
||||
request.get('/api/v1/topic/' + mockTopic.id)
|
||||
.query({
|
||||
accesstoken: mockUser.accessToken
|
||||
})
|
||||
.end(function (err, res) {
|
||||
should.not.exists(err);
|
||||
res.body.success.should.true();
|
||||
res.body.data.is_collect.should.false();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user