Skip to content

Commit

Permalink
Merge pull request #77 from andela/bg-improve-test-coverage-app-17191…
Browse files Browse the repository at this point in the history
…1664

#171911664: Improve test coverage on app.js
  • Loading branch information
kagabof authored Mar 25, 2020
2 parents 1e449b5 + 915cce9 commit fe1a621
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 20 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"expect.js": "^0.3.1",
"express": "^4.17.1",
"express-session": "^1.17.0",
"faker": "^4.1.0",
Expand All @@ -62,6 +63,7 @@
"sequelize": "^5.21.3",
"sequelize-cli": "^5.5.1",
"socket.io": "^2.3.0",
"socket.io-client": "^2.3.0",
"swagger-jsdoc": "^3.5.0",
"swagger-ui-express": "^4.1.3"
},
Expand Down
15 changes: 8 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ dotenv.config();
const connectedClients = {};
io.use(async (socket, next) => {
const { token } = socket.handshake.query;
if (token) {
const decoded = decodeToken(token, process.env.SECRETKEY);
const userData = decoded;
if (!userData.error) {
try {
if (token) {
const decoded = decodeToken(token, process.env.SECRETKEY);
const userData = decoded;
const clientKey = Number.parseInt(userData.id, 10);
connectedClients[clientKey] = connectedClients[clientKey] || [];
connectedClients[clientKey].push(socket.id);
}
} next();
} catch (error) {
console.log('Invalid token provided');
}
next();
});
app.use((req, res, next) => {
req.io = io;
req.connectedClients = connectedClients;
next();
});

io.on('connection', (socket) => {
io.sockets.on('connect', (socket) => {
global.connect = socket;
console.log('a user connected');
socket.on('disconnect', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export default class requestsController {
where: { managerId: req.user.id, status: 'pending' },
include: [{ model: models.Comment }],
});
if (pendingRequests) { return res.status(200).json({ message: 'Pending requests', pendingRequests }); }
const pendingRequestLength = pendingRequests.length;
if (pendingRequestLength > 0) { return res.status(200).json({ message: 'Pending requests', pendingRequests }); }
return res.status(404).json({ message: 'No Pending request available' });
} catch (error) {
return res.status(500).json({ status: 500, error });
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/eventConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io from './ioServerHelper';

const { Chats } = models;
export default () => {
io.on('connection', (socket) => {
io.on('connect', (socket) => {
socket.on('send-chat-message', (message) => {
const token = localStorage.getItem('token');
if (token) {
Expand Down
59 changes: 51 additions & 8 deletions src/tests/app.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import io from 'socket.io-client';
import dotenv from 'dotenv';
import app from '../app';

chai.use(chaiHttp);
chai.should();
dotenv.config();

const clientToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTAsImVtYWlsIjoid2lsbGlhbS5pc2hpbXdlQGFuZGVsYS5jb20iLCJyb2xlIjoibWFuYWdlciIsImZpcnN0TmFtZSI6IldpbGxpYW0iLCJsYXN0TmFtZSI6IklzaGltd2UiLCJpYXQiOjE1ODQ4MDkwMjJ9.FEnICPezHKpjKGLkoN7J942KrwWMoas1cToIjbBC93w';
const clientToken2 = ' ';

const port = process.env.PORT || 4000;
const BASE_URL = `http://localhost:${port}`;
const checkRoute = () => {
describe('Non existing route.(POST) ', () => {
it('it should return 404 on non existing route', (done) => {
chai
.request(app)
.post('/api/v1/auth/route')
.end((err, res) => {
expect(res.statusCode).to.equal(404);
expect(res.body).to.have.property('error').equals('Not Found!');
describe('test app.js', () => {
describe('Non existing route.(POST) ', () => {
let socket;
beforeEach((done) => {
socket = io.connect(BASE_URL, { query: { token: clientToken } }, {
'reconnection delay': 0,
'reopen delay': 0,
'force new connection': true,
transports: ['websocket'],
});
socket.on('connect', () => {
done();
});
});
it('it should welcome if url is valid', (done) => {
chai
.request(app)
.get('/')
.end((err, res) => {
expect(res.body).to.equals('Welcome to Barefoot Nomad');
done();
});
});
it('should fail to connect if no token provided', (done) => {
socket = io.connect('http://localhost:4000', { query: { token: clientToken2 } });
socket.on('disconnect', () => {
});
done();
});
it('it should display 404 for non existing endpoint', (done) => {
chai
.request(app)
.post('/api/v1/auth/route')
.end((err, res) => {
expect(res.statusCode).to.equal(404);
expect(res.body).to.have.property('error').equals('Not Found!');
done();
});
});
afterEach((done) => {
if (socket.connected) {
socket.disconnect();
}
done();
});
});
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/tests/comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const testRejectRequest = () => {
.request(app)
.delete('/api/v1/trips/comment?commentId=1')
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.status).to.equal(200);
expect(res.body).to.have.property('message').that.equals('Comment deleted successfully!');
done();
});
Expand Down
4 changes: 2 additions & 2 deletions src/tests/mainTest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import {
} from './accommodation/createAccommodationFacility.test';
import testUserRoles from './userRoles.test';

routeExistance();
signUpTest();
userLoginTest();
testUserRoles();
twoWayTrip();
muliltiRequest();
allRequestTest();
Expand Down Expand Up @@ -74,6 +74,6 @@ userProfileInformation();
accommodationFeedBack();
notificationFormat();
likeUnlikeAccommodation();
testUserRoles();
socialLogin();
routeExistance();
events();
22 changes: 22 additions & 0 deletions src/tests/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,28 @@ const userSignUp = () => {
done();
});
});
it('it should return 200 on successful signIn', (done) => {
chai
.request(app)
.post('/api/v1/auth/login')
.send(mockData.managerLogin3)
.end((err, res) => {
expect(res.statusCode).to.equal(200);
expect(res.body.message).to.be.equal('Successfully login');
done();
});
});
it('it should return 200 when Pending Request available', (done) => {
chai
.request(app)
.get('/api/v1/trips/pendingApproval')
.send()
.end((err, res) => {
expect(res.statusCode).to.equal(200);
expect(res.body.message).to.be.equal('Pending requests');
done();
});
});
});
};

Expand Down

0 comments on commit fe1a621

Please sign in to comment.