Skip to content

Commit

Permalink
Merge 2320b86 into 8f739dc
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Mar 22, 2019
2 parents 8f739dc + 2320b86 commit 71c9b60
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const logger = createLogger({
],
});

if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
logger.add(new transports.Console({
format: format.combine(
format.colorize(),
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www",
"test": "./node_modules/.bin/mocha --exit",
"test:cover": "nyc --report-dir ./coverage --reporter=html --reporter=text-summary node_modules/mocha/bin/_mocha --exit",
"test": "./node_modules/.bin/mocha test/**/*.test.js --exit",
"test:cover": "nyc --report-dir ./coverage --reporter=html --reporter=text-summary node_modules/mocha/bin/_mocha test/**/*.test.js --exit",
"test:coveralls": "nyc npm run test:cover && nyc report --reporter=text-lcov | coveralls",
"lint": "./node_modules/.bin/eslint routes/ models/ helpers/ lib/ app.js public/js",
"lint:fix": "npm run lint -- --fix",
Expand Down
42 changes: 42 additions & 0 deletions routes/user/deleteUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable max-len */
/**
*
* @api {DELETE} /api/user/:id DELETE /api/user/:id
* @apiName DELETE /api/user/:id
* @apiGroup User
*
*
* @apiParam {String} userId UUID of the user to get
*
* @apiSuccess {Number} deletedUsers how many user profiles were deleted
*
* @apiParamExample {String} Request-Example:
* {
* "id" : "0a40cf80-4b2a-11e9-a532-07f768aa6c74"
* }
*
*
*/

module.exports = function deleteUser(app) {
app.delete('/api/user/:id', async (req, res) => {
const {
id,
} = req.params;

const deletedUsers = await app.models.User.destroy({
where: {
id,
},
});

if (deletedUsers === 0) {
res.status(404);
return res.end();
}

res.status(200);
res.json(deletedUsers);
return res.end();
});
};
47 changes: 47 additions & 0 deletions routes/user/getUserById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable max-len */
/**
*
* @api {GET} /api/user/:id GET /api/user/:id
* @apiName GET /api/user/:id
* @apiGroup User
*
*
* @apiParam {String} userId UUID of the user to get
*
* @apiSuccess {Object} user
* @apiSuccess {String} user.id UUID
* @apiSuccess {String} user.username Username
* @apiSuccess {String} user.steamId Steam64 ID
* @apiSuccess {Date} user.lastVisited Date the user was last active on the website
*
* @apiParamExample {String} Request-Example:
* {
* "id" : "0a40cf80-4b2a-11e9-a532-07f768aa6c74"
* }
*
*/

module.exports = function getUserById(app) {
app.get('/api/user/:id', (req, res) => {
const {
id,
} = req.params;
return app.models.User.findByPk(id)
.then((user) => {
if (user === null) {
res.status(404);
return res.end();
}
const userData = user.get({
plain: true,
});
const response = {
id: userData.id,
username: userData.username,
steamId: userData.steamId,
lastVisited: userData.lastVisited,
};
return res.json(response);
});
});
};
8 changes: 4 additions & 4 deletions test/routes-bans.test.js → test/bans/routes-bans.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ const faker = require('faker');
const chai = require('chai');
const chaiHttp = require('chai-http');

const mock = require('./mock');
const mock = require('../mock');

// Start the server
const server = require('../bin/www');
const server = require('../../bin/www');

const validSteamId = '76561198028175941';
const should = chai.should(); // eslint-disable-line no-unused-vars

const {
reasons,
games,
} = require('../config/constants');
} = require('../../config/constants');

const {
sequelize,
} = require('../models');
} = require('../../models');

chai.use(chaiHttp);

Expand Down
2 changes: 1 addition & 1 deletion test/mock/mockPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = async function mockPlayer(UserId, steamId, username) {
}

if (_.isUndefined(steamId)) {
steamId = faker.random.number();
steamId = faker.random.number(76561190000000000, 76561200000000000);
}

if (_.isUndefined(UserId)) {
Expand Down
2 changes: 1 addition & 1 deletion test/mock/mockUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function mockServer(username, steamId) {
}

if (_.isUndefined(steamId)) {
steamId = faker.random.number();
steamId = faker.random.number(76561190000000000, 76561200000000000);
}

const user = {
Expand Down
2 changes: 1 addition & 1 deletion test/pages.test.js → test/pages/pages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const chai = require('chai');
const chaiHttp = require('chai-http');

// Start the server
const server = require('../bin/www');
const server = require('../../bin/www');

const should = chai.should(); // eslint-disable-line no-unused-vars

Expand Down
40 changes: 40 additions & 0 deletions test/user/deleteUser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const chai = require('chai');
const chaiHttp = require('chai-http');
const mock = require('../mock');

// Start the server
const server = require('../../bin/www');

const should = chai.should(); // eslint-disable-line no-unused-vars

chai.use(chaiHttp);

describe('DELETE /api/user/:id', () => {
let testUser;
before(async () => {
testUser = await mock.user();
});

it('Returns 200 and expected data', (done) => {
chai.request(server)
.delete(`/api/user/${testUser.id}`)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.eq(1)
done();
});
});

it('Returns 404 for unknown ID', (done) => {
chai.request(server)
.delete('/api/user/unknownId')
.end((err, res) => {
res.should.have.status(404);
done();
});
});
});
40 changes: 40 additions & 0 deletions test/user/getUserById.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const chai = require('chai');
const chaiHttp = require('chai-http');
const mock = require('../mock');

// Start the server
const server = require('../../bin/www');

const should = chai.should(); // eslint-disable-line no-unused-vars

chai.use(chaiHttp);

describe('GET /api/user/:id', () => {
let testUser;
before(async () => {
testUser = await mock.user();
});

it('Returns 200 and expected data', (done) => {
chai.request(server)
.get(`/api/user/${testUser.id}`)
.end((err, res) => {
res.should.have.status(200);
res.body.id.should.be.eq(testUser.id);
done();
});
});

it('Returns 404 for unknown ID', (done) => {
chai.request(server)
.get('/api/user/unknownId')
.end((err, res) => {
res.should.have.status(404);
done();
});
});
});

0 comments on commit 71c9b60

Please sign in to comment.