forked from cnodejs/nodeclub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.test.js
150 lines (133 loc) · 3.92 KB
/
reply.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var app = require('../../app');
var request = require('supertest')(app);
var support = require('../support/support');
var ReplyProxy = require('../../proxy/reply');
describe('test/controllers/reply.test.js', function () {
before(function (done) {
support.ready(done);
});
var reply1Id;
describe('reply1', function () {
it('should add a reply1', function (done) {
var topic = support.testTopic;
request.post('/' + topic._id + '/reply')
.set('Cookie', support.normalUserCookie)
.send({
r_content: 'test reply 1'
})
.expect(302)
.end(function (err, res) {
res.headers['location'].should.match(new RegExp('/topic/' + topic.id + '#\\w+'));
// 记录下这个 reply1 的 id
reply1Id = res.headers['location'].match(/#(\w+)/)[1];
done(err);
});
});
it('should 422 when add a empty reply1', function (done) {
var topic = support.testTopic;
request.post('/' + topic._id + '/reply')
.set('Cookie', support.normalUserCookie)
.send({
r_content: ''
})
.expect(422)
.end(done);
});
it('should not add a reply1 when not login', function (done) {
request.post('/' + support.testTopic._id + '/reply')
.send({
r_content: 'test reply 1'
})
.expect(403)
.end(done);
});
});
describe('edit reply', function () {
it('should not show edit page when not author', function (done) {
request.get('/reply/' + reply1Id + '/edit')
.set('Cookie', support.normalUser2Cookie)
.expect(403)
.end(done);
});
it('should show edit page when is author', function (done) {
request.get('/reply/' + reply1Id + '/edit')
.set('Cookie', support.normalUserCookie)
.expect(200)
.end(function (err, res) {
res.text.should.containEql('test reply 1');
done(err);
});
});
it('should update edit', function (done) {
var topic = support.testTopic;
request.post('/reply/' + reply1Id + '/edit')
.send({
t_content: 'been update',
})
.set('Cookie', support.normalUserCookie)
.end(function (err, res) {
res.status.should.equal(302);
res.headers['location'].should.match(new RegExp('/topic/' + topic.id + '#\\w+'));
done(err);
});
});
});
describe('upvote reply', function () {
var reply1, reply1UpCount;
before(function (done) {
ReplyProxy.getReply(reply1Id, function (err, reply) {
reply1 = reply;
reply1UpCount = reply1.ups.length;
done(err);
});
});
it('should increase', function (done) {
request.post('/reply/' + reply1Id + '/up')
.send({replyId: reply1Id})
.set('Cookie', support.normalUser2Cookie)
.end(function (err, res) {
res.status.should.equal(200);
res.body.should.eql({
success: true,
action: 'up',
});
done(err);
});
});
it('should decrease', function (done) {
request.post('/reply/' + reply1Id + '/up')
.send({replyId: reply1Id})
.set('Cookie', support.normalUser2Cookie)
.end(function (err, res) {
res.status.should.equal(200);
res.body.should.eql({
success: true,
action: 'down',
});
done(err);
});
});
});
describe('delete reply', function () {
it('should should not delete when not author', function (done) {
request.post('/reply/' + reply1Id + '/delete')
.send({
reply_id: reply1Id
})
.expect(403)
.end(done);
});
it('should delete reply when author', function (done) {
request.post('/reply/' + reply1Id + '/delete')
.send({
reply_id: reply1Id
})
.set('Cookie', support.normalUserCookie)
.expect(200)
.end(function (err, res) {
res.body.should.eql({status: 'success'});
done(err);
});
});
});
});