Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 735a0dc

Browse files
committed
[finished] debuging gif image post tests
1 parent 72fb737 commit 735a0dc

File tree

6 files changed

+161
-6
lines changed

6 files changed

+161
-6
lines changed

api/v1/controllers/gif.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const createGifController = (req, res, next) => {
2828
const gifPublicId = req.file.public_id;
2929
const createdOn = moment().format('YYYY-MM-DD HH:mm:ss');
3030
const authorId = req.user.sub;
31+
3132
findAllGifs()
3233
.then((rows) => {
3334
const gifId = Math.max(...rows.map(row => row.id + 1), 1);

api/v1/models/gif.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Gif = {
2525

2626
saveGif(values) {
2727
const queryText = `INSERT INTO
28-
gifs (ID, title, gifURL, gifPublicId, createdOn, authorID)
28+
gifs (id, title, gifurl, gifpublicid, createdon, authorid)
2929
VALUES ($1, $2, $3, $4, $5, $6)`;
3030
return query(queryText, values);
3131
},
@@ -39,6 +39,16 @@ const Gif = {
3939
});
4040
return row;
4141
},
42+
43+
deleteAllGifs() {
44+
const queryText = 'DELETE FROM gifs RETURNING *';
45+
const rows = query(queryText, [])
46+
.then((res) => res.rows)
47+
.catch((err) => {
48+
throw err;
49+
});
50+
return rows;
51+
},
4252
};
4353

4454

api/v1/test/211552610004202.gif

513 KB
Loading

api/v1/test/gif.spec.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
});

api/v1/test/user.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import chai, { expect } from 'chai';
22
import chaiHttp from 'chai-http';
33
import 'chai/register-should';
44
import uuidv1 from 'uuid/v1';
5+
import dotenv from 'dotenv';
56
import server from '../../../server';
67
import User from '../models/user';
78
import helper from '../controllers/helper';
89
import pool from '../../../config/dbConfig'
910

11+
dotenv.config();
12+
13+
process.env.NODE_ENV = 'test';
14+
1015

1116
const { deleteAllUsers, save } = User;
1217
const { hashPassword, generateToken } = helper;
@@ -47,7 +52,6 @@ describe('auth', () => {
4752
const res = await chai.request(server)
4853
.post('/api/v1/auth/create-user')
4954
.set('Authorization', tokent)
50-
.set('Accept', 'application/json')
5155
.set('Content-Type', 'application/json')
5256
.send(user1);
5357
expect(res).to.have.status(201);
@@ -122,8 +126,7 @@ describe('auth', () => {
122126

123127
});
124128

125-
126-
129+
127130
describe('POST /api/v1/auth/signin', () => {
128131
const id2 = uuidv1();
129132
before(async () => {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"dev_unix": "NODE_ENV=development npm start",
1010
"prod_win": "set NODE_ENV=production&&npm start",
1111
"prod_unix": "NODE_ENV=production npm start",
12-
"test": "set NODE_ENV=test&&npm run start_test",
13-
"start_test": "mocha --require @babel/register ./api/v1/test",
12+
"test": "mocha --require @babel/register ./api/v1/test --timeout 30000 --exit",
1413
"coverage": "nyc npm run test",
1514
"build": "rm -rf ./build && babel -d ./build ./v1/api -s"
1615
},

0 commit comments

Comments
 (0)