Skip to content

Commit

Permalink
Merge 50587a9 into ccb3a52
Browse files Browse the repository at this point in the history
  • Loading branch information
kenware authored Jul 11, 2018
2 parents ccb3a52 + 50587a9 commit 77adc26
Show file tree
Hide file tree
Showing 24 changed files with 4,442 additions and 3,419 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
]
}
}
}
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/public/**
12 changes: 9 additions & 3 deletions .eslintrc.js → .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
{
"root": true,
"extends": "airbnb-base",
"env": {
Expand All @@ -7,15 +7,21 @@ module.exports = {
"mocha": true,
"jasmine": true
},
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"one-var": 0,
"import/extensions": 0,
"one-var-declaration-per-line": 0,
"new-cap": 0,
"no-underscore-dangle":"off",
"consistent-return": 0,
"no-param-reassign": 0,
"comma-dangle": 0,
"curly": ["error", "multi-line"],
"import/no-unresolved": [2, { commonjs: true }],
"import/no-unresolved": [2, { "commonjs": true }],
"no-shadow": ["error", { "allow": ["req", "res", "err"] }],
"valid-jsdoc": ["error", {
"requireReturn": true,
Expand All @@ -31,4 +37,4 @@ module.exports = {
}
}]
}
};
}
3 changes: 2 additions & 1 deletion .hound.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
eslint:
enabled: true
config_file: .eslintrc.js
config_file: .eslintrc
ignore_file: .eslintignore
4 changes: 0 additions & 4 deletions .nodemonignore

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Cards for Humanity - [http://cfh.io](http://cfh.io)

[![Build Status](https://travis-ci.org/andela/ET-cfh.svg?branch=develop)](https://travis-ci.org/andela/ET-cfh)
[![Continous Integration](https://img.shields.io/badge/Protected_by-Hound-a873d1.svg)](https://www.houndci.com)
{<img src="https://coveralls.io/repos/github/andela/ET-cfh/badge.svg?branch=ch-coveralls-code-coverage-158457032" alt="Coverage Status" />}
<img src="https://coveralls.io/repos/github/andela/ET-cfh/badge.svg?branch=ch-coveralls-code-coverage-158457032" alt="Coverage Status" />

Cards for Humanity is a fast-paced online version of the popular card game, Cards Against Humanity, that gives you the opportunity to donate to children in need - all while remaining as despicable and awkward as you naturally are.

Expand Down
40 changes: 40 additions & 0 deletions app/controllers/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Module dependencies.
*/
import mongoose from 'mongoose';

const Game = mongoose.model('Game');

/**
* @function gameResult
* @description without friends parameter, it returns a status code 401
* with a warning message
* It saves the game result to the database
*
* @param {object} req
* @param {object} res
*
* @returns {object} returns the game result
*/
const gameResult = (req, res) => {
let gameId = req.params.id;
gameId = parseInt(gameId, 10);
const { gameWinner, players } = req.body;
if (!players) {
return res.status(401).json({ message: 'Players have not joined the game' });
}
const payload = {
gameWinner,
players,
gameId
};
const game = new Game(payload);
game.save((err) => {
if (err) {
return res.status(500).json({ message: 'Error saving game' });
}
return res.status(201).json(game);
});
};

export default gameResult;
28 changes: 28 additions & 0 deletions app/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'babel-polyfill';
import jwt from 'jsonwebtoken';

/**
* @function auth
* @param {string} data - A token
* @returns { null } returns Unauthorized Access if token is undfefined
* @returns { expired } returns Please login
* @description used to access authenticated route
* @description if token is valid, decode the payload and pass it controller
*/

const auth = (req, res, next) => {
const token = req.headers.authorization || req.headers['x-access-token'];
if (!token) {
return res.status(401).json({ message: 'Unauthorized Access' });
}

jwt.verify(token, process.env.SECRET_KEY, (err, result) => {
if (err) {
return res.status(401).json({ message: 'Please login!' });
}
req.decoded = result;
next();
});
};

export default auth;
36 changes: 36 additions & 0 deletions app/models/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Module dependencies.
*/
import mongoose from 'mongoose';

const { Schema } = mongoose;

/**
* Game Schema
*/
const GameSchema = new Schema({
id: {
type: Number
},
gameId: {
type: Number
},
gameWinner: {
type: String,
default: '',
trim: true
},
players: {
type: [String]
}
});

GameSchema.statics = {
load(id, cb) {
this.findOne({
id
}).select('-_id').exec(cb);
}
};

mongoose.model('Game', GameSchema);
51 changes: 51 additions & 0 deletions backend-test/startGame/startGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'babel-polyfill';
import request from 'supertest';
import { expect } from 'chai';
import mongoose from 'mongoose';
import app from '../../server';

const User = mongoose.model('User');

const mockPlayers = {
gameWinner: 'kevin',
players: [1, 2, 3]
};

const userMock = {
name: 'kelvin',
password: '12345',
username: 'kelvin',
email: 'kelvin@email.com'
};

let userToken = '';
describe('Player endpoints', () => {
before(() => {
Promise.resolve(User.create(userMock));
});

it('POST /api/auth/endpoint should return the user token along with the user', (done) => {
request(app)
.post('/api/auth/login')
.send(userMock)
.end((err, res) => {
if (err) return done(err);
expect(res.statusCode).to.equal(200);
userToken = res.body.token;
done();
});
});

it('POST /api/game/:id/start endpoint should return the the game players', (done) => {
request(app)
.post('/api/game/3/start')
.set('authorization', userToken)
.send(mockPlayers)
.end((err, res) => {
if (err) return done(err);
expect(res.statusCode).to.equal(201);
expect(res.body.gameWinner).to.equal('kevin');
done();
});
});
});
6 changes: 6 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as answers from '../app/controllers/answers';
import questions from '../app/controllers/questions';
import avatars from '../app/controllers/avatars';
import index from '../app/controllers/index';
import game from '../app/controllers/game';
import auth from '../app/middleware/auth';

export default (router, passport, app) => {
// api name spaced routes;
Expand All @@ -12,6 +14,10 @@ export default (router, passport, app) => {
.post('/auth/login', users.handleLogin)
.post('/auth/signup', users.handleSignUp);

// Setting up the game api
api
.post('/game/:id/start', auth, game);

router.get('/signin', users.signin);
router.get('/signup', users.signup);
router.get('/chooseavatars', users.checkAvatar);
Expand Down
3 changes: 2 additions & 1 deletion config/socket/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Game.prototype.payload = function() {
avatar: player.avatar,
premium: player.premium,
socketID: player.socket.id,
color: player.color
color: player.color,
userId: player.userId
});
});
return {
Expand Down
2 changes: 1 addition & 1 deletion config/socket/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Player(socket) {
this.username = null;
this.premium = 0;
this.avatar = null;
this.userID = null;
this.userId = null;
this.color = null;
}

Expand Down
7 changes: 4 additions & 3 deletions config/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ module.exports = function(io) {
var joinGame = function(socket,data) {
var player = new Player(socket);
data = data || {};
player.userID = data.userID || 'unauthenticated';
if (data.userID !== 'unauthenticated') {
player.userId = data.userId || 'unauthenticated';
if (data.userId !== 'unauthenticated') {
User.findOne({
_id: data.userID
_id: data.userId
}).exec(function(err, user) {
if (err) {
console.log('err',err);
Expand All @@ -92,6 +92,7 @@ module.exports = function(io) {
player.username = 'Guest';
player.avatar = avatars[Math.floor(Math.random()*4)+12];
} else {
player.userId = user.id;
player.username = user.name;
player.premium = user.premium || 0;
player.avatar = user.avatar || avatars[Math.floor(Math.random()*4)+12];
Expand Down
Loading

0 comments on commit 77adc26

Please sign in to comment.