Skip to content

Commit 2675b30

Browse files
kagabofKagorora
authored andcommitted
Merge pull request #17 from Stackup-Rwanda/bg-consume-header-user-token-172817077
#172817077 consume token from header
2 parents 01ab8c4 + 5a90572 commit 2675b30

34 files changed

+1084
-837
lines changed

src/controllers/request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default class requestsController {
132132
if (!request) throw 'Request not found!';
133133
const requestStatus = request.status;
134134
if (requestStatus === 'rejected') throw 'The request was rejected before!';
135-
if (requestStatus === 'pending') { return await Request.updateStatus(request, 'rejected'); }
135+
if (requestStatus === 'pending') { await Request.updateStatus(request, 'rejected'); }
136136
if (requestStatus === 'approved') {
137137
const { departureDate } = request;
138138
const todayDate = getTodayDate();

src/helpers/eventConnect.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import localStorage from 'localStorage';
1+
import dotenv from 'dotenv';
22
import models from '../db/models';
33
import decodeToken from './decodeToken';
44
import io from './ioServerHelper';
55

6+
dotenv.config();
7+
68
const { Chats } = models;
79
export default () => {
810
io.on('connect', (socket) => {
911
socket.on('send-chat-message', (message) => {
10-
const token = localStorage.getItem('token');
12+
const { token } = message;
1113
if (token) {
1214
const decoded = decodeToken(token, process.env.SECRETKEY);
1315
const { id, firstName, lastName } = decoded;
1416
const name = `${firstName} ${lastName}`;
15-
const send = { from: name, message };
17+
const send = {
18+
from: name,
19+
message: message.message,
20+
};
1621

1722
Chats.create({
18-
message,
23+
message: message.message,
1924
senderId: id,
2025
senderName: name,
2126
});

src/middlewares/checkAuth.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import localStorage from 'localStorage';
21
import decodeToken from '../helpers/decodeToken';
32

43
const auth = (req, res, next) => {
5-
const token = localStorage.getItem('token');
4+
const token = req.header('user-token');
65
if (!token) {
76
return res.status(401).json({
87
status: 401,

src/middlewares/validateReturnTrip.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default async (req, res, next) => {
5454
if (((request.departureDate <= reqDepartureDate)
5555
&& (reqDepartureDate <= request.returnDate))
5656
|| ((request.departureDate <= reqReturnDate) && (reqReturnDate <= request.returnDate))) {
57-
conflictingRequest = getConflictingRequest(request);
57+
conflictingRequest = getConflictingRequest(request.dataValues);
5858
throw 'conflicting trip';
5959
}
6060
});
@@ -97,8 +97,5 @@ export default async (req, res, next) => {
9797
conflictingRequest,
9898
});
9999
}
100-
return res.status(500).json({
101-
error: error.message,
102-
});
103100
}
104101
};

src/tests/accommodation/accommodationFeedback.test.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import chaiHttp from 'chai-http';
33
import sinonChai from 'sinon-chai';
44
import { mockReq, mockRes } from 'sinon-express-mock';
55
import sinon from 'sinon';
6+
import jwt from 'jsonwebtoken';
67
import app from '../../app';
78
import accommodationFacilities from '../../controllers/accommodation';
89
import { travelAdminInfo } from './accommodationMockData';
@@ -12,18 +13,15 @@ chai.should();
1213
chai.use(sinonChai);
1314
const accommodationFeedBack = () => {
1415
describe('accommodation feedback ', () => {
15-
it('it should return 200 on successful signIn', (done) => {
16-
chai
17-
.request(app)
18-
.post('/api/v1/auth/login')
19-
.send(travelAdminInfo)
20-
.end((err, res) => {
21-
expect(res.statusCode).to.equal(200);
22-
done();
23-
});
16+
const Signed = travelAdminInfo;
17+
const Token = jwt.sign(Signed, process.env.SECRETKEY, {
18+
expiresIn: '24h',
2419
});
2520
it('it should return 200 when accommodation commented successfully ', () => {
26-
const accommodationFeedBackSpy = sinon.spy(accommodationFacilities, 'accommodationFeedBack');
21+
const accommodationFeedBackSpy = sinon.spy(
22+
accommodationFacilities,
23+
'accommodationFeedBack'
24+
);
2725
const request = {
2826
params: {
2927
id: 1,
@@ -45,6 +43,7 @@ const accommodationFeedBack = () => {
4543
chai
4644
.request(app)
4745
.post('/api/v1/accommodation/comment/1')
46+
.set('user-token', Token)
4847
.send({ comment: 'Bad service' })
4948
.end((err, res) => {
5049
expect(res.body).to.have.property('comment');
@@ -58,6 +57,7 @@ const accommodationFeedBack = () => {
5857
chai
5958
.request(app)
6059
.post('/api/v1/accommodation/comment/n')
60+
.set('user-token', Token)
6161
.send({ comment: 'Bad service' })
6262
.end((err, res) => {
6363
expect(res.statusCode).to.equal(400);
@@ -68,4 +68,3 @@ const accommodationFeedBack = () => {
6868
};
6969

7070
export default accommodationFeedBack;
71-

src/tests/accommodation/accommodationMockData.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ export const supplierToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6OCwiZ
33
export const requesterToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NSwiZW1haWwiOiJtdWx0aS1jaXR5QGdtYWlsLmNvbSIsInJvbGUiOiJyZXF1ZXN0ZXIiLCJmaXJzdE5hbWUiOiJkYXZpZCIsImxhc3ROYW1lIjoiSGltYmFyYSIsImlhdCI6MTU4NTAyODE4MH0.padWdwnIymfz3yxtMKqHmrbSEcCs8DkKh5cTuBMophg';
44

55
export const travelAdminInfo = {
6+
id: 6,
7+
firstName: 'traveler',
8+
lastName: 'Himbara',
9+
gender: 'male',
610
7-
password: 'Niyonkuru@1',
11+
passport: 'ws846531',
12+
role: 'traveladmin',
13+
method: 'standard',
14+
clientId: null,
815
};
916

1017
export const supplierInfo = {
@@ -13,8 +20,15 @@ export const supplierInfo = {
1320
};
1421

1522
export const wrongUser = {
23+
id: 5,
24+
firstName: 'david',
25+
lastName: 'Himbara',
26+
gender: 'male',
1627
17-
password: 'Niyonkuru@1',
28+
passport: 'ml846531',
29+
role: 'requester',
30+
method: 'standard',
31+
lineManager: '[email protected]',
1832
};
1933

2034
export const nonExistingUser = {
@@ -31,8 +45,16 @@ export const invalidRate = {
3145
};
3246

3347
export const ExistingUser = {
48+
id: 1,
49+
firstName: 'Alain',
50+
lastName: 'Maxim',
51+
gender: 'male',
3452
35-
password: 'Niyonkuru@1',
53+
passport: '12345ohc',
54+
role: 'requester',
55+
method: 'standard',
56+
clientId: null,
57+
lineManager: '[email protected]',
3658
};
3759

3860
export const facility = {
@@ -47,28 +69,24 @@ export const facility = {
4769
roomType: 'king',
4870
price: '$50/night',
4971
available: 'true',
50-
5172
},
5273
{
5374
roomName: 'Room 2',
5475
roomType: 'qween',
5576
price: '$53/night',
5677
available: 'true',
57-
5878
},
5979
{
6080
roomName: 'Room 3',
6181
roomType: 'qween',
6282
price: '$54/night',
6383
available: 'true',
64-
6584
},
6685
{
6786
roomName: 'Room 4',
6887
roomType: 'qween',
6988
price: '$55/night',
7089
available: 'true',
71-
7290
},
7391
],
7492
};
@@ -85,28 +103,24 @@ export const facility2 = {
85103
roomType: 'king',
86104
price: '$50/night',
87105
available: 'true',
88-
89106
},
90107
{
91108
roomName: 'Room 2',
92109
roomType: 'qween',
93110
price: '$53/night',
94111
available: 'true',
95-
96112
},
97113
{
98114
roomName: 'Room 3',
99115
roomType: 'qween',
100116
price: '$54/night',
101117
available: 'true',
102-
103118
},
104119
{
105120
roomName: 'Room 4',
106121
roomType: 'qween',
107122
price: '$55/night',
108123
available: 'true',
109-
110124
},
111125
],
112126
};
@@ -123,28 +137,24 @@ export const existingFacility = {
123137
roomType: 'king',
124138
price: '$50/night',
125139
available: 'true',
126-
127140
},
128141
{
129142
roomName: 'Room 2',
130143
roomType: 'qween',
131144
price: '$53/night',
132145
available: 'true',
133-
134146
},
135147
{
136148
roomName: 'Room 3',
137149
roomType: 'qween',
138150
price: '$54/night',
139151
available: 'true',
140-
141152
},
142153
{
143154
roomName: 'Room 4',
144155
roomType: 'qween',
145156
price: '$55/night',
146157
available: 'true',
147-
148158
},
149159
],
150160
};
@@ -158,14 +168,12 @@ export const missingInformation = {
158168
roomName: 'double room',
159169
price: '$50/night',
160170
available: 'true',
161-
162171
},
163172
{
164173
roomName: 'Room 1',
165174
roomType: 'qween',
166175
price: '$53/night',
167176
available: 'true',
168-
169177
},
170178
{
171179
roomName: 'Room 2',
@@ -199,7 +207,6 @@ export const missingRoomInfo = {
199207
roomType: 'qween',
200208
price: '$53/night',
201209
available: 'true',
202-
203210
},
204211
{
205212
roomName: 'Room 3',

0 commit comments

Comments
 (0)