|
| 1 | +import chai, { expect } from 'chai'; |
| 2 | +import chaiHttp from 'chai-http'; |
| 3 | +import 'chai/register-should'; |
| 4 | +import { after, before } from 'mocha'; |
| 5 | +import dotenv from 'dotenv'; |
| 6 | +import moment from 'moment'; |
| 7 | +import uuidv1 from 'uuid/v1'; |
| 8 | +import server from '../../../server'; |
| 9 | +import Gif from '../models/gif'; |
| 10 | +import User from '../models/user'; |
| 11 | +import helper from '../controllers/helper'; |
| 12 | + |
| 13 | +dotenv.config(); |
| 14 | + |
| 15 | +process.env.NODE_ENV = 'test'; |
| 16 | + |
| 17 | +const { hashPassword, generateToken } = helper; |
| 18 | +const { save, deleteAllUsers } = User; |
| 19 | +const { saveGif, deleteAllGifs } = Gif; |
| 20 | + |
| 21 | +chai.use(chaiHttp); |
| 22 | + |
| 23 | +describe('Gifs', () => { |
| 24 | + const id1 = uuidv1(); |
| 25 | + const obj = generateToken({ sub: id1, role: 'ADMIN' }); |
| 26 | + const token = `Bearer ${obj}`; |
| 27 | + before(async () => { |
| 28 | + const gifTitle = 'First gif test value'; |
| 29 | + const gUrl = 'http://res.cloudinary.com/mukuna/image/upload/v1574479438/teamwork-api-gifs/utvsco0pt6suogvzfbu9.gif'; |
| 30 | + const p_id = 'teamwork-api-gifs/utvsco0pt6suogvzfbu9'; |
| 31 | + const date = moment().format('YYYY-MM-DD HH:mm:ss'); |
| 32 | + const userValues = [id1, 'Daniel', 'Mukuna', '[email protected]', hashPassword('DANIEL12345'), 'male', 'admin', 'I.T', 'thika']; |
| 33 | + const gifValues = [1, gifTitle, gUrl, p_id, date, id1]; |
| 34 | + await save(userValues); |
| 35 | + await saveGif(gifValues); |
| 36 | + }); |
| 37 | + |
| 38 | + after(async () => { |
| 39 | + await deleteAllGifs(); |
| 40 | + await deleteAllUsers(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('POST /api/v1/gifs', () => { |
| 44 | + it('should create a new gifs', async () => { |
| 45 | + const gif1 = { |
| 46 | + title: 'test title' |
| 47 | + }; |
| 48 | + const gifUrl = './api/v1/test/211552610004202.gif'; |
| 49 | + const res = await chai.request(server) |
| 50 | + .post('/api/v1/gifs') |
| 51 | + .set('Authorization', token) |
| 52 | + .set('Content-Type', 'application/x-www-form-urlencoded') |
| 53 | + .attach('image', gifUrl) |
| 54 | + .field(gif1); |
| 55 | + expect(res).to.have.status(201); |
| 56 | + expect(res.body.data).to.be.an('Object'); |
| 57 | + expect(res.body.status).to.be.equals('success'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should fail if the gifs url is blank', async () => { |
| 61 | + const gif2 = { |
| 62 | + title: 'test title' |
| 63 | + }; |
| 64 | + const gifUrl2 = ''; |
| 65 | + const res = await chai.request(server) |
| 66 | + .post('/api/v1/gifs') |
| 67 | + .set('Authorization', token) |
| 68 | + .set('Content-Type', 'application/x-www-form-urlencoded') |
| 69 | + .attach('image', gifUrl2) |
| 70 | + .field(gif2); |
| 71 | + expect(res).to.have.status(400); |
| 72 | + expect(res.body.status).to.be.equals('error'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should fail if the gifs title is blank', async () => { |
| 76 | + const gif3 = { |
| 77 | + title: '' |
| 78 | + }; |
| 79 | + const gifUrl3 = './api/v1/test/211552610004202.gif'; |
| 80 | + const res = await chai.request(server) |
| 81 | + .post('/api/v1/gifs') |
| 82 | + .set('Authorization', token) |
| 83 | + .set('Content-Type', 'application/x-www-form-urlencoded') |
| 84 | + .attach('image', gifUrl3) |
| 85 | + .field(gif3); |
| 86 | + expect(res).to.have.status(400); |
| 87 | + expect(res.body.status).to.be.equals('error'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('GET /api/v1/gifs', () => { |
| 92 | + it('should fetch all gifs', async () => { |
| 93 | + const res = await chai.request(server) |
| 94 | + .get('/api/v1/gifs') |
| 95 | + .set('Authorization', token); |
| 96 | + expect(res).to.have.status(200); |
| 97 | + expect(res.body.data).to.be.an('array'); |
| 98 | + expect(res.body.status).to.be.equals('success'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('GET /api/v1/gifs/:gifId', () => { |
| 103 | + it('should fetch get one gif', async () => { |
| 104 | + const res = await chai.request(server) |
| 105 | + .get('/api/v1/gifs/1') |
| 106 | + .set('Authorization', token); |
| 107 | + expect(res).to.have.status(200); |
| 108 | + expect(res.body).to.be.an('object'); |
| 109 | + expect(res.body.status).to.be.equals('success'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should fail if gif does not exist', async () => { |
| 113 | + const res = await chai.request(server) |
| 114 | + .get('/api/v1/gifs/5') |
| 115 | + .set('Authorization', token); |
| 116 | + expect(res).to.have.status(500); |
| 117 | + expect(res.body).to.be.an('Object'); |
| 118 | + expect(res.body.status).to.be.equals('error'); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('DELETE /api/v1/gifs/:gifId', () => { |
| 123 | + it('should delete one gif', async () => { |
| 124 | + const res = await chai.request(server) |
| 125 | + .delete('/api/v1/gifs/1') |
| 126 | + .set('Authorization', token) |
| 127 | + .set('Content-Type', 'application/json'); |
| 128 | + expect(res).to.have.status(200); |
| 129 | + expect(res.body).to.be.an('Object'); |
| 130 | + expect(res.body.status).to.be.equals('success'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should fail if gif does not exist', async () => { |
| 134 | + const res = await chai.request(server) |
| 135 | + .delete('/api/v1/gifs/5') |
| 136 | + .set('Authorization', token); |
| 137 | + expect(res).to.have.status(500); |
| 138 | + expect(res.body).to.be.an('Object'); |
| 139 | + expect(res.body.status).to.be.equals('error'); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments