-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
4,554 additions
and
3,439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/public/** | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ dist | |
coverage/ | ||
.nyc_output/ | ||
bower_components | ||
.eslintrc.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @module Services this module is created to wrap the | ||
* core logic of all the controllers so that the logic is | ||
* unit testable | ||
*/ | ||
|
||
import mongoose from 'mongoose'; | ||
|
||
const User = mongoose.model('User'); | ||
const Game = mongoose.model('Game'); | ||
|
||
/** | ||
* @name returnGamesWon | ||
* @type {function} | ||
* @param {array} games The games that the requesting user has played | ||
* @param {object} user The current user | ||
* @description this method sorts through the games provided and | ||
* checks where the current user has won, uses .toString() because direct _id | ||
* comparison seems buggy and anomalous | ||
* @returns {object} The user object and the games played; | ||
*/ | ||
function returnGamesWon(games, user) { | ||
return games.filter(game => game.gameWinner._id.toString() === user._id.toString()); | ||
} | ||
|
||
|
||
/** | ||
* @name handleFetchProfile | ||
* @type {function} | ||
* @param {string} _id The user id, should be a mongoose ObjectID type (usually a string) | ||
* @description this method check for a user and all the games he/she has played, | ||
* populating the players and the winner in the process.; | ||
* @returns {object} The user object and the games played; | ||
*/ | ||
const handleFetchProfile = _id => new Promise((resolve, reject) => { | ||
User.findOne({ _id }, (err, user) => { | ||
if (err) reject(err); | ||
if (user) { | ||
return Game.find({ | ||
players: user._id | ||
}) | ||
.populate('players') | ||
.populate('gameWinner') | ||
.exec((err, games) => { | ||
if (err) return reject(err); | ||
// using toString() because these methods behave anomalously | ||
const gamesWon = returnGamesWon(games, user); | ||
return resolve({ ...user._doc, games, gamesWon }); | ||
}); | ||
} | ||
const error = new Error('There is no user matching the query'); | ||
error.status = 403; | ||
reject(error); | ||
}); | ||
}); | ||
|
||
|
||
export default { | ||
handleFetchProfile | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import 'babel-polyfill'; | ||
import request from 'supertest'; | ||
import faker from 'faker'; | ||
import { expect } from 'chai'; | ||
import mongoose from 'mongoose'; | ||
import app from '../../server'; | ||
import { Tokenizer } from '../../app/helpers/tokenizer'; | ||
|
||
mongoose.Promise = global.Promise; | ||
|
||
const User = mongoose.model('User'); | ||
let token; | ||
|
||
const mock = { | ||
name: 'Hasstrup Ezekiel', | ||
password: '12345', | ||
username: 'hasstrupezekiel', | ||
email: 'hasstrup@email.com' | ||
}; | ||
|
||
describe('User endpoints', () => { | ||
describe('Authentication', () => { | ||
before(() => Promise.resolve(User.create(mock).then((user) => { | ||
token = Tokenizer(user); | ||
}) | ||
.catch((err) => { | ||
throw err; | ||
}))); | ||
|
||
it('POST /api/auth/login should return the user token along with the', (done) => { | ||
try { | ||
request(app) | ||
.post('/api/auth/login') | ||
.send(mock) | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
expect(res.statusCode).to.equal(200); | ||
expect(res.body.token).to.equal(Tokenizer(res.body)); | ||
done(); | ||
}); | ||
} catch (err) { | ||
/* eslint no-unused-expressions: 0 */ | ||
expect(err).to.not.exist; | ||
} | ||
}); | ||
|
||
it('POST /api/auth/signup should return the token of a user on sign up', (done) => { | ||
try { | ||
const fake = { | ||
username: faker.internet.userName(), | ||
name: faker.name.findName(), | ||
email: faker.internet.email(), | ||
password: faker.internet.password(), | ||
}; | ||
|
||
request(app) | ||
.post('/api/auth/signup') | ||
.send(fake) | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
expect(res.statusCode).to.equal(201); | ||
expect(res.body.token).to.equal(Tokenizer(res.body)); | ||
done(); | ||
}); | ||
} catch (err) { | ||
expect(err).to.not.exist; | ||
} | ||
}); | ||
}); | ||
describe('Show user profile', () => { | ||
it(' GET /api/profile Should return the profile of the user showing the games played(if any)', (done) => { | ||
request(app) | ||
.get('/api/profile') | ||
.set('Authorization', token) | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
expect(res.statusCode).to.equal(200); | ||
expect(res.body.data.name).to.equal('Hasstrup Ezekiel'); | ||
expect(res.body.data.games).to.be.an('array'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('GET /api/profile shoulfd fail without a token with a 403 error', (done) => { | ||
request(app) | ||
.get('/api/profile') | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
expect(res.statusCode).to.equal(404); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.