|
| 1 | +import chai, { expect } from 'chai'; |
| 2 | +import chaiHttp from 'chai-http'; |
| 3 | +import 'chai/register-should'; |
| 4 | +import dotenv from 'dotenv'; |
| 5 | +import moment from 'moment'; |
| 6 | +import uuidv1 from 'uuid/v1'; |
| 7 | +import server from '../../../server'; |
| 8 | +import Gif from '../models/gif'; |
| 9 | +import User from '../models/user'; |
| 10 | +import Article from '../models/article'; |
| 11 | +import Comment from '../models/comment'; |
| 12 | +import helper from '../controllers/helper'; |
| 13 | + |
| 14 | +dotenv.config(); |
| 15 | + |
| 16 | +process.env.NODE_ENV = 'test'; |
| 17 | + |
| 18 | +const { save, deleteAllUsers } = User; |
| 19 | +const { saveArticle, deleteAllArticles } = Article; |
| 20 | +const { saveGif, deleteAllGifs } = Gif; |
| 21 | +const { deleteAllComments } = Comment; |
| 22 | + |
| 23 | +const { hashPassword, generateToken} = helper; |
| 24 | + |
| 25 | +chai.use(chaiHttp); |
| 26 | + |
| 27 | +describe('comments', () => { |
| 28 | + const id1 = uuidv1(); |
| 29 | + const obj = generateToken({ sub: id1, role: 'ADMIN' }); |
| 30 | + const token = `Bearer ${obj}`; |
| 31 | + before(async () => { |
| 32 | + const articleTitle = 'First test title'; |
| 33 | + const articleText = 'First test article test'; |
| 34 | + const date = moment().format('YYYY-MM-DD HH:mm:ss'); |
| 35 | + |
| 36 | + const userValues = [id1, 'Daniel', 'Mukuna', '[email protected]', hashPassword('DANIEL12345'), 'male', 'admin', 'I.T', 'thika']; |
| 37 | + const articleValues = [1, articleTitle, articleText, date, id1]; |
| 38 | + |
| 39 | + const gifTitle = 'First gif test value'; |
| 40 | + const gUrl = 'http://res.cloudinary.com/mukuna/image/upload/v1574479438/teamwork-api-gifs/utvsco0pt6suogvzfbu9.gif'; |
| 41 | + const p_id = 'teamwork-api-gifs/utvsco0pt6suogvzfbu9'; |
| 42 | + |
| 43 | + const gifValues = [1, gifTitle, gUrl, p_id, date, id1]; |
| 44 | + await save(userValues); |
| 45 | + await saveGif(gifValues); |
| 46 | + await saveArticle(articleValues); |
| 47 | + }); |
| 48 | + |
| 49 | + after(async () => { |
| 50 | + await deleteAllComments(); |
| 51 | + await deleteAllGifs(); |
| 52 | + await deleteAllArticles(); |
| 53 | + await deleteAllUsers(); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('POST /api/v1/articles/:articleID/comments', () => { |
| 57 | + it('should create a new comment on the article', async () => { |
| 58 | + const comment1 = { |
| 59 | + comment: 'first article test comment' |
| 60 | + }; |
| 61 | + const res = await chai.request(server) |
| 62 | + .post('/api/v1/articles/1/comments') |
| 63 | + .set('Authorization', token) |
| 64 | + .set('Content-Type', 'application/json') |
| 65 | + .send(comment1); |
| 66 | + expect(res).to.have.status(201); |
| 67 | + expect(res.body).to.be.an('Object'); |
| 68 | + expect(res.body.status).to.be.equals('success'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should fail if the comment field is empty', async () => { |
| 72 | + const comment2 = { |
| 73 | + comment: '' |
| 74 | + }; |
| 75 | + const res = await chai.request(server) |
| 76 | + .post('/api/v1/articles/1/comments') |
| 77 | + .set('Authorization', token) |
| 78 | + .set('Content-Type', 'application/json') |
| 79 | + .send(comment2); |
| 80 | + expect(res).to.have.status(400); |
| 81 | + expect(res.body).to.be.an('Object'); |
| 82 | + expect(res.body.status).to.be.equals('error'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('POST /api/v1/gifs/:gifID/comments', () => { |
| 87 | + it('should create a new comment on the gif', async () => { |
| 88 | + const comment3 = { |
| 89 | + comment: 'first gif image test comment' |
| 90 | + }; |
| 91 | + const res = await chai.request(server) |
| 92 | + .post('/api/v1/gifs/1/comments') |
| 93 | + .set('Authorization', token) |
| 94 | + .set('Content-Type', 'application/json') |
| 95 | + .send(comment3); |
| 96 | + expect(res).to.have.status(201); |
| 97 | + expect(res.body).to.be.an('Object'); |
| 98 | + expect(res.body.status).to.be.equals('success'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should fail if the comment field is empty', async () => { |
| 102 | + const comment4 = { |
| 103 | + comment: '' |
| 104 | + }; |
| 105 | + const res = await chai.request(server) |
| 106 | + .post('/api/v1/gifs/1/comments') |
| 107 | + .set('Authorization', token) |
| 108 | + .set('Content-Type', 'application/json') |
| 109 | + .send(comment4); |
| 110 | + expect(res).to.have.status(400); |
| 111 | + expect(res.body).to.be.an('Object'); |
| 112 | + expect(res.body.status).to.be.equals('error'); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments