From 3063d7abdc066a7ef0fca2be043e994761a9ca3f Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 18:33:27 -0800 Subject: [PATCH] getting data from postgres successfully --- bin/server.js | 18 +++++++------ package.json | 2 +- src/modules/iredmail/controller.js | 13 +++++++-- test/a03-iredmail.spec.js | 43 +++++++++++++++--------------- 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/bin/server.js b/bin/server.js index 1b47ef2..b7e73df 100644 --- a/bin/server.js +++ b/bin/server.js @@ -54,15 +54,17 @@ async function startServer () { await app.listen(config.port) console.log(`Server started on ${config.port}`) - try { - const iRedMail = new IRedMail() - await iRedMail.sequelize.authenticate() - console.log('Connection to the iRedMail postgres database has been established successfully.') + if (process.env.TEST_ENV !== 'unit') { + try { + const iRedMail = new IRedMail() + await iRedMail.sequelize.authenticate() + console.log('Connection to the iRedMail postgres database has been established successfully.') - // Attach to iRedMail instantiation to the context object. - app.context.iRedMail = iRedMail - } catch (err) { - console.log(`Could not connect to iRedMail instance.`) + // Attach to iRedMail instantiation to the context object. + app.context.iRedMail = iRedMail + } catch (err) { + console.log(`Could not connect to iRedMail instance.`) + } } return app diff --git a/package.json b/package.json index 7f72bed..b4fed41 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node index.js", "test": "APP_ENV=test TEST_ENV=unit nyc --reporter=text ./node_modules/.bin/mocha --exit", - "test:integration": "APP_ENV=test TEST_ENV=integration nyc --reporter=text ./node_modules/.bin/mocha --exit", + "test:integration": "APP_ENV=test TEST_ENV=integration nyc --reporter=text ./node_modules/.bin/mocha --timeout 15000 --exit", "lint": "eslint src/**/*.js", "docs": "./node_modules/.bin/apidoc -i src/ -o docs", "coverage": "nyc report --reporter=text-lcov | coveralls", diff --git a/src/modules/iredmail/controller.js b/src/modules/iredmail/controller.js index a2f8492..c427331 100644 --- a/src/modules/iredmail/controller.js +++ b/src/modules/iredmail/controller.js @@ -1,18 +1,27 @@ // const User = require('../../models/users') +const util = require('util') +util.inspect.defaultOptions = { depth: 1 } + /* Create a new user by manipulating the postgres database. */ async function createUser (ctx) { try { - // Sync the model. - await ctx.iRedMail.models.usedQuota.sync() + // console.log(`ctx.iRedMail: ${util.inspect(ctx.iRedMail)}`) + // Sync the model. + const model = await ctx.iRedMail.models.usedQuota.sync() + + const data = await model.findAll() + /* // Get all the models in the database: const models = await ctx.iRedMail.usedQuota.findAll() console.log(`data: ${JSON.stringify(models, null, 2)}`) ctx.body = { models } + */ + ctx.body = data } catch (err) { console.log(`Error in iredmail.createUser(): `, err) ctx.throw(500, err.message) diff --git a/test/a03-iredmail.spec.js b/test/a03-iredmail.spec.js index 03ccdda..d784f70 100644 --- a/test/a03-iredmail.spec.js +++ b/test/a03-iredmail.spec.js @@ -22,31 +22,32 @@ describe('#iRedMail', () => { if (process.env.TEST_ENV === 'integration') { testType = 'integration' - iRedMail = new IRedMail() - await iRedMail.sequelize.authenticate() + // iRedMail = new IRedMail() + // await iRedMail.sequelize.authenticate() } }) describe('#createUser', () => { - it('should get database models', async () => { - try { - console.log(`Hello world!`) - /* - const options = { - method: 'POST', - uri: `${LOCALHOST}/iredmail/adduser`, - resolveWithFullResponse: true, - json: true, - body: { - } - } + if (process.env.TEST_ENV === 'integration') { + it('should get database models', async () => { + try { + // console.log(`Hello world!`) - let result = await rp(options) - console.log(`result: ${util.inspect(result)}`) - */ - } catch (err) { - console.log(`Error in #createUser test: `, err) - } - }) + const options = { + method: 'POST', + uri: `${LOCALHOST}/iredmail/adduser`, + resolveWithFullResponse: true, + json: true, + body: { + } + } + + let result = await rp(options) + console.log(`result: ${util.inspect(result.body)}`) + } catch (err) { + console.log(`Error in #createUser test: `, err) + } + }) + } }) })