|
@@ -14,6 +14,7 @@ chai.use(require('chai-things'))
|
|
|
describe('Like', () => {
|
|
|
let admin = chai.request.agent(server)
|
|
|
let user = chai.request.agent(server)
|
|
|
+ let user2 = chai.request.agent(server)
|
|
|
|
|
|
//Wait for app to start before commencing
|
|
|
before((done) => {
|
|
@@ -35,6 +36,15 @@ describe('Like', () => {
|
|
|
password: 'password',
|
|
|
})
|
|
|
})
|
|
|
+ .then(() => {
|
|
|
+ return user2
|
|
|
+ .post('/api/v1/user')
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({
|
|
|
+ username: 'useraccount2',
|
|
|
+ password: 'password',
|
|
|
+ })
|
|
|
+ })
|
|
|
.then(() => {
|
|
|
return admin
|
|
|
.post('/api/v1/category')
|
|
@@ -65,7 +75,9 @@ describe('Like', () => {
|
|
|
.set('content-type', 'application/json')
|
|
|
.send({ threadId: 1, content: 'POST 3' })
|
|
|
})
|
|
|
- .then(done)
|
|
|
+ .then(_ => {
|
|
|
+ done()
|
|
|
+ })
|
|
|
.catch(done)
|
|
|
}
|
|
|
|
|
@@ -77,4 +89,64 @@ describe('Like', () => {
|
|
|
})
|
|
|
|
|
|
after(() => sequelize.sync({ force: true }))
|
|
|
+
|
|
|
+ it('should allow a user to like a post', async () => {
|
|
|
+ let likeRes = await user.put('/api/v1/post/1/like')
|
|
|
+
|
|
|
+ //Should be idempotent, hence put twice
|
|
|
+ await user.put('/api/v1/post/1/like')
|
|
|
+
|
|
|
+ likeRes.should.have.status(200)
|
|
|
+ likeRes.should.be.json
|
|
|
+ likeRes.body.should.have.property('success', true)
|
|
|
+
|
|
|
+ let postRes = await user.get('/api/v1/post/1')
|
|
|
+
|
|
|
+ postRes.should.have.status(200)
|
|
|
+ postRes.should.be.json
|
|
|
+ postRes.body.should.have.property('Likes')
|
|
|
+ postRes.body.Likes.should.have.property('length', 1)
|
|
|
+ postRes.body.Likes[0].should.have.deep.property('username', 'useraccount')
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should return an error if not logged in', done => {
|
|
|
+ chai.request(server)
|
|
|
+ .put('/api/v1/post/3/like')
|
|
|
+ .end((err, res) => {
|
|
|
+ res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
|
|
|
+
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should return an error if liking own post', done => {
|
|
|
+ admin
|
|
|
+ .put('/api/v1/post/1/like')
|
|
|
+ .end((err, res) => {
|
|
|
+ res.body.errors.should.contain.something.that.deep.equals(Errors.cannotLikeOwnPost)
|
|
|
+
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should return all likes on a post', async () => {
|
|
|
+ await admin.put('/api/v1/post/3/like')
|
|
|
+ await user2.put('/api/v1/post/3/like')
|
|
|
+
|
|
|
+ let postRes = await admin.get('/api/v1/post/3')
|
|
|
+
|
|
|
+ postRes.should.have.status(200)
|
|
|
+ postRes.should.be.json
|
|
|
+ postRes.body.should.have.property('Likes')
|
|
|
+ postRes.body.Likes.should.have.property('length', 2)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should return all likes on a thread', async () => {
|
|
|
+ let thread = await admin.get('/api/v1/thread/1')
|
|
|
+
|
|
|
+ thread.should.have.status(200)
|
|
|
+ thread.should.be.json
|
|
|
+ thread.body.Posts[0].Likes.should.have.property('length', 1)
|
|
|
+ thread.body.Posts[2].Likes.should.have.property('length', 2)
|
|
|
+ })
|
|
|
})
|