From 273499f61abe90b37a46842b98f996b4f4f16a62 Mon Sep 17 00:00:00 2001 From: Rafi Date: Tue, 21 Jan 2020 17:22:44 -0800 Subject: [PATCH] remove nulls from https://github.com/Seneca-CDOT/telescope/pull/550/commits/daf9988167755e681768d4c0bbfdc26a67aebb09 and add logging to config.js --- src/backend/lib/config.js | 11 ++++++++++- src/backend/lib/redis.js | 4 ++-- src/backend/login/usingPassport.js | 2 +- src/backend/utils/email-sender.js | 8 ++++---- src/backend/utils/logger.js | 4 ++-- test/email-sender.test.js | 10 +++++----- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/backend/lib/config.js b/src/backend/lib/config.js index 4ca624acf4..19bec5fcb0 100644 --- a/src/backend/lib/config.js +++ b/src/backend/lib/config.js @@ -1,3 +1,12 @@ const dotenv = require('dotenv'); +const { logger } = require('../utils/logger'); -dotenv.config(); +const result = dotenv.config(); + +if (result.error) { + logger.error( + '\n\n\t💡 It appears that you have not yet configurated a .env file.', + '\n\t Please refer to our documentation regarding environment configuration:', + '\n\t https://github.com/Seneca-CDOT/telescope/blob/master/docs/CONTRIBUTING.md\n' + ); +} diff --git a/src/backend/lib/redis.js b/src/backend/lib/redis.js index ccfcfc0950..0f97b1b487 100644 --- a/src/backend/lib/redis.js +++ b/src/backend/lib/redis.js @@ -8,7 +8,7 @@ const { logger } = require('../utils/logger'); const redisUrl = process.env.REDIS_URL || 'redis://127.0.0.1:6379'; // Set MOCK_REDIS=1 to mock, MOCK_REDIS= to use real redis -const useMockRedis = process.env.MOCK_REDIS || null; +const useMockRedis = process.env.MOCK_REDIS; // RedisConstructor is one of Redis or MockRedis const RedisConstructor = useMockRedis ? MockRedis : Redis; @@ -16,7 +16,7 @@ const RedisConstructor = useMockRedis ? MockRedis : Redis; function createRedisClient() { try { const { port, host } = url.parse(redisUrl, true); - return new RedisConstructor(port, host, { password: process.env.REDIS_PASSWORD || null }); + return new RedisConstructor(port, host, { password: process.env.REDIS_PASSWORD }); } catch (err) { const message = `Unable to parse port and host from "${redisUrl}"`; logger.error({ err }, message); diff --git a/src/backend/login/usingPassport.js b/src/backend/login/usingPassport.js index a9eea9d979..9fa128fe0d 100644 --- a/src/backend/login/usingPassport.js +++ b/src/backend/login/usingPassport.js @@ -13,7 +13,7 @@ SAML2_REDIRECT_URI=http://localhost:3000/oauth/callback let cert = null; const SAML2_BASE_URI = process.env.SAML2_BASE_URI || ''; -const SAML2_REDIRECT_URI = process.env.SAML2_REDIRECT_URI || null; +const SAML2_REDIRECT_URI = process.env.SAML2_REDIRECT_URI; try { cert = fs.readFileSync(path.resolve(process.cwd(), './certs/key.pem'), 'utf8'); diff --git a/src/backend/utils/email-sender.js b/src/backend/utils/email-sender.js index 87d873e3a4..444700d489 100644 --- a/src/backend/utils/email-sender.js +++ b/src/backend/utils/email-sender.js @@ -38,7 +38,7 @@ exports.createTransporter = function(hosted, portNum, isSecure, username, passwo exports.createMail = function(receipients, subjectMessage, message) { // Email Content const mailOptions = { - from: process.env.NODEMAILER_USERNAME || null, // Email Name + from: process.env.NODEMAILER_USERNAME, // Email Name to: receipients, // People to send to subject: subjectMessage, // Subject Line html: @@ -82,11 +82,11 @@ exports.verifyTransporter = function(transporter) { exports.sendMessage = async function(receipiants, subjectMessage, message) { return new Promise((resolve, reject) => { const transporter = this.createTransporter( - process.env.NODEMAILER_SERVER || null, + process.env.NODEMAILER_SERVER, 2222, false, - process.env.NODEMAILER_USERNAME || null, - process.env.NODEMAILER_PASSWORD || null + process.env.NODEMAILER_USERNAME, + process.env.NODEMAILER_PASSWORD ); const allGood = this.verifyTransporter(transporter); if (!allGood) { diff --git a/src/backend/utils/logger.js b/src/backend/utils/logger.js index e45221ebb6..0839e5b344 100644 --- a/src/backend/utils/logger.js +++ b/src/backend/utils/logger.js @@ -22,8 +22,8 @@ const options = { // Log to stdout, or a file if LOG_FILE is specified let destination; -if (process.env.LOG_FILE || null) { - destination = pino.destination(path.resolve(process.cwd(), process.env.LOG_FILE || null)); +if (process.env.LOG_FILE) { + destination = pino.destination(path.resolve(process.cwd(), process.env.LOG_FILE)); options.prettyPrint = false; // options.prettyPrint.colorize = false; } else { diff --git a/test/email-sender.test.js b/test/email-sender.test.js index cc053246d9..a94f40588a 100644 --- a/test/email-sender.test.js +++ b/test/email-sender.test.js @@ -9,7 +9,7 @@ test.skip('Tests if sendMessage resolves with expected info', async () => { // Feeds test data into sendMessage function const testSubjectMessage = 'Test'; const testHTML = '

Hello World

'; - const testRecipient = process.env.NODEMAILER_TESTRECIPIENT || null; + const testRecipient = process.env.NODEMAILER_TESTRECIPIENT; const testReturnValue = emailsender.sendMessage(testRecipient, testSubjectMessage, testHTML); // Tests if the expected resolve is correct expect(testReturnValue).resolves.toBe([testRecipient]); @@ -44,11 +44,11 @@ test.skip('Tests if verifyTransport returns with expected info', () => { test.skip('Tests if transport is created correctly', () => { const transporter = emailsender.createTransporter(); - expect(transporter.options).toHaveProperty('host', process.env.NODEMAILER_SERVER || null); + expect(transporter.options).toHaveProperty('host', process.env.NODEMAILER_SERVER); expect(transporter.options).toHaveProperty('port', 2222); expect(transporter.options).toHaveProperty('secure', false); - expect(transporter.options).toHaveProperty('auth.user', process.env.NODEMAILER_USERNAME || null); - expect(transporter.options).toHaveProperty('auth.pass', process.env.NODEMAILER_PASSWORD || null); + expect(transporter.options).toHaveProperty('auth.user', process.env.NODEMAILER_USERNAME); + expect(transporter.options).toHaveProperty('auth.pass', process.env.NODEMAILER_PASSWORD); }, 10000); test('Tests if email object is created correctly', () => { @@ -57,7 +57,7 @@ test('Tests if email object is created correctly', () => { const message = 'FIX ME!'; const email = emailsender.createMail(recipient, subjectMessage, message); - expect(email).toHaveProperty('from', process.env.NODEMAILER_USERNAME || null); + expect(email).toHaveProperty('from', process.env.NODEMAILER_USERNAME); expect(email).toHaveProperty('to', 'dummy@email.com'); expect(email).toHaveProperty('subject', 'Hello'); expect(email).toHaveProperty(