diff --git a/bin/server.js b/bin/server.js index c44e2a1..00d56b2 100644 --- a/bin/server.js +++ b/bin/server.js @@ -11,15 +11,14 @@ const serve = require('koa-static') const config = require('../config') const errorMiddleware = require('../src/middleware') -async function startServer() { - +async function startServer () { // Create a Koa instance. const app = new Koa() app.keys = [config.session] // Connect to the Mongo Database. mongoose.Promise = global.Promise - mongoose.connect(config.database) + await mongoose.connect(config.database) // MIDDLEWARE START @@ -42,12 +41,18 @@ async function startServer() { // MIDDLEWARE END - app.listen(config.port, () => { - console.log(`Server started on ${config.port}`) - }) + // app.listen(config.port, () => { + // console.log(`Server started on ${config.port}`) + // }) + await app.listen(config.port) + console.log(`Server started on ${config.port}`) + + return app } -startServer() +// startServer() // export default app -//module.exports = app -module.exports = startServer +// module.exports = app +module.exports = { + startServer +} diff --git a/index.js b/index.js index ed2fd2b..4d82406 100644 --- a/index.js +++ b/index.js @@ -1 +1,3 @@ -require('./bin/server.js') +const server = require('./bin/server.js') + +server.startServer() diff --git a/package.json b/package.json index b83065a..c6cd8a9 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,9 @@ "koa-router": "^7.0.1", "koa-static": "^4.0.2", "mongoose": "^5.0.8", - "passport-local": "^1.0.0" + "passport-local": "^1.0.0", + "request": "^2.85.0", + "request-promise": "^4.2.2" }, "devDependencies": { "chai": "^4.1.2", diff --git a/test/auth.spec.js b/test/auth.spec.js index 129d704..89ae7f2 100644 --- a/test/auth.spec.js +++ b/test/auth.spec.js @@ -1,19 +1,30 @@ const app = require('../bin/server') -const supertest = require('supertest') -//const { expect, should } = require('chai') -const expect = require('chai').expect; -const should = require('chai').should; -//const { cleanDb, authUser } = require('./utils') -const cleanDb = require('./utils').cleanDb -const authUser = require('./utils').authUser +// const supertest = require('supertest') +// const expect = require('chai').expect +const should = require('chai').should +// const cleanDb = require('./utils').cleanDb +// const authUser = require('./utils').authUser +const utils = require('./utils') + +const rp = require('request-promise') +const assert = require('chai').assert should() -const request = supertest.agent(app.listen()) +// const request = supertest.agent(app.listen()) const context = {} +const LOCALHOST = 'http://localhost:5000' + + + describe('Auth', () => { - before((done) => { - cleanDb() + before(async () => { + + await app.startServer() + + utils.cleanDb() + + /* authUser(request, (err, { user, token }) => { if (err) { return done(err) } @@ -21,34 +32,75 @@ describe('Auth', () => { context.token = token done() }) + */ + + const userObj = { + username: 'test', + password: 'pass' + } + const testUser = await utils.createUser(userObj) + + context.user = testUser.user + context.token = testUser.token }) describe('POST /auth', () => { - it('should throw 401 if credentials are incorrect', (done) => { - request - .post('/auth') - .set('Accept', 'application/json') - .send({ username: 'supercoolname', password: 'wrongpassword' }) - .expect(401, done) + it('should throw 401 if credentials are incorrect', async () => { + try { + const options = { + method: 'POST', + uri: `${LOCALHOST}/auth`, + resolveWithFullResponse: true, + json: true, + body: { + username: 'test', + password: 'wrongpassword' + } + } + + let result = await rp(options) + + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) + assert(false, 'Unexpected result') + } catch (err) { + if (err.statusCode === 422) { + assert(err.statusCode === 422, 'Error code 422 expected.') + } else if (err.statusCode === 401) { + assert(err.statusCode === 401, 'Error code 401 expected.') + } else { + console.error('Error: ', err) + console.log('Error stringified: ' + JSON.stringify(err, null, 2)) + throw err + } + } }) - it('should auth user', (done) => { - request - .post('/auth') - .set('Accept', 'application/json') - .send({ username: 'test', password: 'pass' }) - .expect(200, (err, res) => { - if (err) { return done(err) } + it('should auth user', async () => { + try { + const options = { + method: 'POST', + uri: `${LOCALHOST}/auth`, + resolveWithFullResponse: true, + json: true, + body: { + username: 'test', + password: 'pass' + } + } - res.body.user.should.have.property('username') - res.body.user.username.should.equal('test') - expect(res.body.user.password).to.not.exist + let result = await rp(options) - context.user = res.body.user - context.token = res.body.token + // console.log(`result: ${JSON.stringify(result, null, 2)}`) - done() - }) + assert(result.statusCode === 200, 'Status Code 200 expected.') + assert(result.body.user.username === 'test', 'Username of test expected') + assert(result.body.user.password === undefined, 'Password expected to be omited') + } catch (err) { + console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2)) + throw err + } }) }) }) diff --git a/test/utils.js b/test/utils.js index bc9fe2c..d1ab4a2 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,4 +1,7 @@ const mongoose = require('mongoose') +const rp = require('request-promise') + +const LOCALHOST = 'http://localhost:5000' function cleanDb () { for (const collection in mongoose.connection.collections) { @@ -23,7 +26,42 @@ function authUser (agent, callback) { }) } +// This function is used to create new users. +// userObj = { +// username, +// password +// } +async function createUser (userObj) { + try { + const options = { + method: 'POST', + uri: `${LOCALHOST}/users`, + resolveWithFullResponse: true, + json: true, + body: { + user: { + username: userObj.username, + password: userObj.password + } + } + } + + let result = await rp(options) + + const retObj = { + user: result.body.user, + token: result.body.token + } + + return retObj + } catch (err) { + console.log('Error in utils.js/createUser(): ' + JSON.stringify(err, null, 2)) + throw err + } +} + module.exports = { cleanDb, - authUser + authUser, + createUser }