Skip to content

Commit

Permalink
#30 - Fixed merge conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascvas committed Sep 4, 2018
1 parent 9e1b17e commit d3ac535
Show file tree
Hide file tree
Showing 11 changed files with 2,266 additions and 49 deletions.
5 changes: 3 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
services-deploy:
docker:
- image: circleci/node:7.10
working_directory: ~/carpal/infrastructure/services
working_directory: ~/carpal/backend/src
steps:
- checkout:
path: ~/carpal
Expand All @@ -67,7 +67,8 @@ jobs:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: ./writeSecrets.sh
- run: npm run deploy-sls
- run: npm test
- run: npm run deploy-sls
workflows:
version: 2
ui-build-deploy:
Expand Down
2,241 changes: 2,219 additions & 22 deletions backend/package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"test": "mocha './src/test/**/*.test.js'",
"start": "node src/test/expressApis.js",
"refresh-db": "node ./src/main/database/index.js",
"refresh-test-db": "node ./src/test/database/refreshDatabase.js"
"refresh-test-db": "node ./src/test/database/refreshDatabase.js",
"deploy-sls": "sls deploy"
},
"dependencies": {
"body-parser": "^1.18.3",
Expand All @@ -22,6 +23,7 @@
"aws-sdk": "^2.250.1",
"chai": "^4.1.2",
"chai-exclude": "^1.0.9",
"mocha": "^5.2.0"
"mocha": "^5.2.0",
"serverless": "^1.30.1"
}
}
42 changes: 30 additions & 12 deletions backend/src/main/rides/ListRidesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,48 @@ class ListRidesService {
this._rideRepository = new RideRepository(databaseManager);
}

listRides(queryParams, loginData) {
listRides(query, loginData) {
const connection = this._databaseManager.createConnection();

return this._listRides(queryParams, loginData, connection)
return this._listRides(query, loginData, connection)
.then(rides => rides.map(RidesMapper.entityToDto))
.finally(() => this._databaseManager.closeConnection(connection));
}

_listRides(queryParams, loginData, connection) {
let jsonQuery = this._parseParams(queryParams, loginData);
_listRides(query, loginData, connection) {
let jsonQuery = this._parseParams(query, loginData);
if (!jsonQuery) {
return Promise.resolve([]);
}
return this._rideRepository.list(jsonQuery, connection);
}

_parseParams(queryParams, loginData) {
_parseParams(query, loginData) {
const listType = query.listType || 'driver';
const isAdmin = this._hasRole('admin', loginData);
const isDriver = this._hasRole('driver', loginData);
const isFacilitator = this._hasRole('facilitator', loginData);
const notAdminAndListTypeDoesNotMatchRole = !this._hasRole(listType, loginData) && !isAdmin;
if (notAdminAndListTypeDoesNotMatchRole) {
console.log("WARNING: unauthorised attempt to query data", loginData);
return null;
}

return {
toLongitude: queryParams.toLongitude,
toLatitude: queryParams.toLatitude,
fromLongitude: queryParams.fromLongitude,
fromLatitude: queryParams.fromLatitude,
driverGenders: loginData.role === 'driver' ? ['any', loginData.driverGender] : undefined,
includePickupTimeInPast: loginData.role !== 'driver',
facilitatorId: loginData.role === 'facilitator' ? loginData.email : undefined,
toLongitude: query.toLongitude,
toLatitude: query.toLatitude,
fromLongitude: query.fromLongitude,
fromLatitude: query.fromLatitude,
driverGenders: isDriver ? ['any', loginData.driverGender] : undefined,
includePickupTimeInPast: !isDriver,
facilitatorId: isFacilitator ? loginData.email : undefined,
};
}

_hasRole(role, loginData) {
return loginData.roles.indexOf(role) >= 0;
}
}


module.exports = ListRidesService;
4 changes: 2 additions & 2 deletions backend/src/main/utils/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports.decodeJwt = (event) => {
const decodedToken = jsonwebtoken.decode(tokenValue);
const claims = {};
claims.email = decodedToken[`https://${domain}/email`];
claims.role = decodedToken[`https://${domain}/role`];
if (claims.role === 'driver') {
claims.roles = decodedToken[`https://${domain}/roles`];
if (claims.roles.indexOf('driver') >= 0) {
claims.driverGender = decodedToken[`https://${domain}/gender`];
claims.car = decodedToken[`https://${domain}/car`];
}
Expand Down
3 changes: 1 addition & 2 deletions backend/src/test/auth/ExpressAuthApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ExpressAuthApis {
}

wellKnown(req, res) {
let expiry = moment().add(100, 'd');
console.log(req.query);
let jwks = fs.readFileSync(path.resolve(__dirname, '../config/express/certs/jwks.json'));
res.status(200).send(JSON.parse(jwks));
Expand Down Expand Up @@ -61,7 +60,7 @@ class ExpressAuthApis {
_authAs(queryParams, host, userInfo, expiry) {
let payload = this._completeJWT(userInfo, host, expiry.toDate(), queryParams.nonce);

let accessToken = userInfo.role;
let accessToken = userInfo.roles[0];
let cert = fs.readFileSync(path.resolve(__dirname, '../config/express/certs/private.key'));
let jwtToken = jwt.sign(payload, cert, {algorithm: 'RS256'});
return {accessToken, jwtToken};
Expand Down
8 changes: 4 additions & 4 deletions backend/src/test/rides/ListRides.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ afterEach(async () => {
describe('When listing rides', async () => {
it('should show rides for facilitator', async function () {
// given
const loginData = {email: RandomUtils.randomEmail(), role: 'facilitator'};
const loginData = {email: RandomUtils.randomEmail(), roles: ['facilitator']};
const email = loginData.email;
const ride1 = randomRideWithFacilitator(email);
const ride2 = randomRideWithFacilitator(email);
await databaseContainsRides(ride1, ride2);

// when
const rides = await listRideController.listRides({}, loginData);
const rides = await listRideController.listRides({listType: 'facilitator'}, loginData);

assert.deepEqualExcluding(rides, [ride1, ride2], 'id');
});

it('should show all rides for admin', async function () {
// given
const loginData = {email: RandomUtils.randomEmail(), role: 'admin'};
const loginData = {email: RandomUtils.randomEmail(), roles: ['admin']};
const ride1 = RideEntityBuilder.randomRideEntity();
const ride2 = RideEntityBuilder.randomRideEntity();
await databaseContainsRides(ride1, ride2);
Expand All @@ -72,7 +72,7 @@ describe('When listing rides', async () => {

it('should show all rides for driver', async function () {
// given
const loginData = {email: RandomUtils.randomEmail(), role: 'driver', driverGender: 'male'};
const loginData = {email: RandomUtils.randomEmail(), roles: ['driver'], driverGender: 'male'};
const ride1 = randomRideWithGender('male');
const ride2 = randomRideWithGender('female');
const ride3 = randomRideWithGender('any');
Expand Down
2 changes: 1 addition & 1 deletion backend/src/test/users/admin.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"email": "[email protected]",
"gender": "male",
"role": "admin"
"roles": ["admin"]
}
2 changes: 1 addition & 1 deletion backend/src/test/users/driver.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"email": "[email protected]",
"gender": "male",
"car": "suv",
"role": "driver"
"roles": ["driver"]
}
2 changes: 1 addition & 1 deletion backend/src/test/users/facilitator.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"email": "[email protected]",
"gender": "female",
"role": "facilitator"
"roles": ["facilitator"]
}
File renamed without changes.

0 comments on commit d3ac535

Please sign in to comment.