diff --git a/server/routes/auth.routes.js b/server/routes/auth.routes.js index 50be0cf..870bde7 100644 --- a/server/routes/auth.routes.js +++ b/server/routes/auth.routes.js @@ -3,7 +3,7 @@ const jwt = require('jsonwebtoken') const fs = require('fs'); const path = require("path"); -const util = require('node:util'); +//const util = require('node:util'); const {jsonToURI} = require('../utilities.js'); diff --git a/server/tests/admin.check.test.js b/server/tests/admin.check.test.js index de4befe..169d331 100644 --- a/server/tests/admin.check.test.js +++ b/server/tests/admin.check.test.js @@ -1,5 +1,6 @@ const supertestSession = require('supertest-session') -const app = require('../app')() +const { app, clientPromise } = require('../app'); +const appInstance = app(); const mockToken = require('./mocktoken') const mocks = require('./mocks') // noinspection JSUnresolvedVariable @@ -22,7 +23,7 @@ describe('Tests for admin check', () => { // tests can give false failure if the time cuttoff removes all the useful test data process.env.minPredictionCutoffDate = '1990-01-01'; - testSession = supertestSession(app) + testSession = supertestSession(appInstance) adminUser = Object.assign({}, adminCASData) adminUser.firstName = 'adminCheck-beforeAllUser' @@ -36,7 +37,7 @@ describe('Tests for admin check', () => { afterAll(() => { return User.destroy({ where: { firstName: 'adminCheck-beforeAllUser' } }).then( () =>{ - app.db.sequelize.close() + appInstance.db.sequelize.close() }) }) diff --git a/server/tests/admin.reports.test.js b/server/tests/admin.reports.test.js index 0f171b5..b3f4c93 100644 --- a/server/tests/admin.reports.test.js +++ b/server/tests/admin.reports.test.js @@ -1,4 +1,5 @@ -const app = require('../app')() +const { app, clientPromise } = require('../app'); +const appInstance = app(); const db = require('../models/index') const mockToken = require('./mocktoken') const mocks = require('./mocks') @@ -22,7 +23,7 @@ describe('Tests for admin reports and charts', () => { }) afterAll(() => { - return app.db.sequelize.close() + return appInstance.db.sequelize.close() }) test('daily login report', async () => { diff --git a/server/tests/agency.routes.test.js b/server/tests/agency.routes.test.js index 3fbd6e2..3254408 100644 --- a/server/tests/agency.routes.test.js +++ b/server/tests/agency.routes.test.js @@ -1,5 +1,6 @@ const request = require('supertest') -const app = require('../app')() +const { app, clientPromise } = require('../app'); +const appInstance = app(); const mockToken = require('./mocktoken') // noinspection JSUnresolvedVariable const Agency = require('../models').Agency @@ -25,7 +26,7 @@ describe('/api/agencies', () => { }) test('/api/agencies (get)', async () => { - return request(app) + return request(appInstance) .get('/api/agencies') .set('Authorization', `Bearer ${token}`) .then((res) => { @@ -37,7 +38,7 @@ describe('/api/agencies', () => { }) test('/api/agencies (put)', async () => { - return request(app) + return request(appInstance) .put('/api/agencies') .set('Authorization', `Bearer ${token}`) .send({ agency: agency, acronym: acronym }) @@ -52,7 +53,7 @@ describe('/api/agencies', () => { }) test('/api/AgencyList', async () => { - return request(app) + return request(appInstance) .get('/api/AgencyList') .set('Authorization', `Bearer ${token}`) .send({ agency: agency, acronym: acronym }) diff --git a/server/tests/analytics.routes.test.js b/server/tests/analytics.routes.test.js index 88cb28b..7c83c45 100644 --- a/server/tests/analytics.routes.test.js +++ b/server/tests/analytics.routes.test.js @@ -1,5 +1,6 @@ const request = require('supertest') -let app = require('../app')() +const { app, clientPromise } = require('../app'); +const appInstance = app(); const mockToken = require('./mocktoken') // noinspection JSUnresolvedVariable const User = require('../models').User @@ -77,7 +78,7 @@ describe('Analytics routes tests', () => { }, 100000) test('/api/analytics', () => { - return request(app) + return request(appInstance) .post('/api/analytics') .set('Authorization', `Bearer ${adminToken}`) .send({agency: 'Government-wide', fromPeriod: '1/1/1900', toPeriod: '12/31/2100'}) @@ -92,7 +93,7 @@ describe('Analytics routes tests', () => { }) test('/api/analytics notApplicable values', () => { - return request(app) + return request(appInstance) .post('/api/analytics') .set('Authorization', `Bearer ${adminToken}`) .send({agency: 'Government-wide', fromPeriod: '1/1/1900', toPeriod: '12/31/2100'}) diff --git a/server/tests/app.test.js b/server/tests/app.test.js index b678e09..7a2a2c6 100644 --- a/server/tests/app.test.js +++ b/server/tests/app.test.js @@ -1,4 +1,5 @@ -let app = require('../app')() +const { app, clientPromise } = require('../app'); +const appInstance = app(); let {getConfig} = require('../config/configuration') @@ -11,8 +12,8 @@ describe('App Tests', () => { falseCallCount++ } }) - app.corsTest("bad.example.com",mockCallback) - app.corsTest(getConfig("CORSWhitelist")[0],mockCallback) + appInstance.corsTest("bad.example.com",mockCallback) + appInstance.corsTest(getConfig("CORSWhitelist")[0],mockCallback) expect(falseCallCount).toBe(1) }) diff --git a/server/tests/auth.routes.test.js b/server/tests/auth.routes.test.js index 8ef0fa4..9593b2b 100644 --- a/server/tests/auth.routes.test.js +++ b/server/tests/auth.routes.test.js @@ -12,7 +12,7 @@ const authRoutes = require('../routes/auth.routes') const logger = require('../config/winston') const mocks = require('./mocks') -const { userAcceptedCASData } = require('./test.data') +const { userAcceptedCASData } = require('./test.data'); let myUser = Object.assign({}, userAcceptedCASData) myUser.firstName = 'auth-beforeAllUser' @@ -31,7 +31,9 @@ describe('/api/auth/', () => { casConfig.dev_mode_user = "dev_user" let cas = new CASAuthentication(casConfig) - app = require('../app')(null, cas) + const { app, clientPromise } = require('../app'); + appInstance = app(null, cas); + token = await mockToken(myUser, common['jwtSecret']) }) @@ -49,7 +51,7 @@ describe('/api/auth/', () => { let token = await mockToken(user, common['jwtSecret']) return User.create(user) .then(() => { - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({ token: token }) .then((res) => { @@ -65,7 +67,7 @@ describe('/api/auth/', () => { user.userRole = 'Public' user.email = 'notreal@example.com' let token = await mockToken(user, common['jwtSecret']) - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({ token: token }) .then((res) => { @@ -86,7 +88,7 @@ describe('/api/auth/', () => { let token = await mockToken(user, common['jwtSecret']) return User.create(user) .then(() => { - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({ token: token }) .then((res) => { @@ -109,7 +111,7 @@ describe('/api/auth/', () => { userCASData['grouplist'] = authRoutes.roles[authRoutes.roleKeys.PROGRAM_MANAGER_ROLE].casGroup delete userCASData.id let token = await mockToken(userCASData, common['jwtSecret']) - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({ token: token }) .then((res) => { @@ -121,7 +123,7 @@ describe('/api/auth/', () => { }) // send a fake token .then(() => { - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({ token: 'token fake' }) .then((res) => { @@ -133,7 +135,7 @@ describe('/api/auth/', () => { }) // send NO token .then(() => { - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({ no_token: 'token fake' }) .then((res) => { @@ -150,7 +152,7 @@ describe('/api/auth/', () => { // Test what happens when we send an invalid or null JWT test('bad token', () => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer null`) .send() @@ -166,7 +168,10 @@ describe('/api/auth/', () => { casConfig.is_dev_mode = true casConfig.dev_mode_user = "dev_user" let cas = new CASAuthentication(casConfig) - let app3 = require('../app')(null, cas) + + const { app, clientPromise } = require('../app'); + let app3 = app(null, cas); + /** * @type {cookies-session} */ @@ -193,7 +198,7 @@ describe('/api/auth/', () => { const token = await mockToken(casUserInfo, common['jwtSecret']) - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({'token' : token}) .then( (res) => { @@ -207,7 +212,7 @@ describe('/api/auth/', () => { test( 'reject bad token', async () => { const badToken = await mockToken(casUserInfo, 'not-secret') - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({'token' : badToken}) .then( (res) => { @@ -227,7 +232,7 @@ describe('/api/auth/', () => { } const token = await mockToken(fakeAdmin, common['jwtSecret']) - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({'token' : token}) .then( (res) => { @@ -248,7 +253,7 @@ describe('/api/auth/', () => { } const token = await mockToken(adminUser, common['jwtSecret']) - return request(app) + return request(appInstance) .post('/api/auth/tokenCheck') .send({'token' : token}) .then( (res) => { @@ -302,7 +307,7 @@ describe('/api/auth/', () => { return authRoutes.casStage2(mockReq, mockRes).then( () => { expect(mockRes.status.mock.calls[0][0]).toBe(302) // quirky but kept for client compatibility expect(mockRes.set.mock.calls[0][0]).toMatch(/Location/) - expect(mockRes.set.mock.calls[0][1]).toMatch(/token=/) // we will get a token in the redirect if login was successful + expect(mockRes.set.mock.calls[0][1]).toMatch(/info=/) // we will get a token in the redirect if login was successful }) }) @@ -320,7 +325,8 @@ describe('/api/auth/', () => { casConfig.dev_mode_info['authenticationmethod'] = 'urn:max:fips-201-pivcard' let cas = new CASAuthentication(casConfig) - let app2 = require('../app')(null, cas) + let {app, clientPromise} = require('../app'); + let app2 = app(null, cas); /** @type {cookie-session} */ let session = supertestSession(app2) @@ -330,9 +336,12 @@ describe('/api/auth/', () => { let location = res.get('Location') expect(location).toMatch(/token/) + expect(location).toMatch(/info/) + + var url = new URL(location); + var info = url.searchParams.get("info"); + let response = JSON.parse(decodeURIComponent(info)); - let token = location.substr( location.indexOf("token=") + 6 ) - let response = JSON.parse(token) let id = response.id expect(id).toBeGreaterThan(1) return User.findOne({ where: {id : id }}) @@ -387,9 +396,14 @@ describe('/api/auth/', () => { .then( () => { expect(mockRes.statusResult).toBe(302) // quirky but kept for client compatibility expect(mockRes.hResult).toMatch(/Location/) - expect(mockRes.vResult).toMatch(/token=/) - let token = mockRes.vResult.substr(mockRes.vResult.indexOf("token=") + 6 ) - let tokenObj = JSON.parse(token) + expect(mockRes.vResult).toMatch(/info=/) + + var url = new URL(mockRes.vResult); + var info = url.searchParams.get("info"); + let tokenObj = JSON.parse(decodeURIComponent(info)); + let token = tokenObj.token + //let token = mockRes.vResult.substr(mockRes.vResult.indexOf("info=") + 6 ) + //let tokenObj = JSON.parse(token) expect(tokenObj.userRole).toBe(authRoutes.roles[authRoutes.ADMIN_ROLE].name) }) @@ -401,9 +415,13 @@ describe('/api/auth/', () => { // the response.set function should be called with args ( 'Location', 'http://.....') let location = mockRes2.set.mock.calls[0][1] expect(mockRes2.set.mock.calls[0][0]).toMatch(/Location/) - expect(location).toMatch(/token=/) - let token = location.substr(location.indexOf("token=") + 6 ) - let tokenObj = JSON.parse(token) + expect(location).toMatch(/info=/) + + var url = new URL(location); + var info = url.searchParams.get("info"); + let tokenObj = JSON.parse(decodeURIComponent(info)); + let token = tokenObj.token + expect(tokenObj.userRole).toBe(authRoutes.roles[authRoutes.FIVE08_COORDINATOR_ROLE].name) }) diff --git a/server/tests/email.routes.test.js b/server/tests/email.routes.test.js index fbdcb10..3e3ae67 100644 --- a/server/tests/email.routes.test.js +++ b/server/tests/email.routes.test.js @@ -1,5 +1,5 @@ const request = require('supertest') -let app = null // require('../app')();; +let appInstance = null // require('../app')();; const nodemailerMock = require('nodemailer-mock') const mockToken = require('./mocktoken') // noinspection JSUnresolvedVariable @@ -18,7 +18,8 @@ let token = {} describe('/api/email', () => { beforeAll(() => { process.env.MAIL_ENGINE = 'nodemailer-mock' - app = require('../app')() // don't load the app till the mock is configured + const { app, clientPromise } = require('../app'); + appInstance = app(); // don't load the app till the mock is configured myUser = Object.assign({}, userAcceptedCASData) myUser.firstName = 'email-beforeAllUser' @@ -49,7 +50,7 @@ describe('/api/email', () => { } nodemailerMock.mock.reset() - return request(app) + return request(appInstance) .post('/api/email') .set('Authorization', `Bearer ${token}`) .send({ body: 'this is the body text' }) @@ -60,7 +61,7 @@ describe('/api/email', () => { }) .then(() => { nodemailerMock.mock.reset() - return request(app) + return request(appInstance) .post('/api/email') .set('Authorization', `Bearer ${token}`) .send(email) @@ -90,7 +91,7 @@ describe('/api/email', () => { } nodemailerMock.mock.reset() - return request(app) + return request(appInstance) .post('/api/email') .set('Authorization', `Bearer ${token}`) .send(email) diff --git a/server/tests/masquerade.route.test.js b/server/tests/masquerade.route.test.js index 3ecffe9..0073c6a 100644 --- a/server/tests/masquerade.route.test.js +++ b/server/tests/masquerade.route.test.js @@ -1,5 +1,6 @@ const supertestSession = require('supertest-session') -let app = require('../app')() +const { app, clientPromise } = require('../app'); +let appInstance = app(); const mockToken = require('./mocktoken') // noinspection JSUnresolvedVariable const User = require('../models').User @@ -19,7 +20,7 @@ describe('Test masquerade functionality', () => { // tests can give false failure if the time cuttoff removes all the useful test data process.env.minPredictionCutoffDate = '1990-01-01'; - testSession = supertestSession(app) + testSession = supertestSession(appInstance) adminUser = Object.assign({}, adminCASData) adminUser.firstName = 'masq-beforeAllUser' @@ -45,13 +46,14 @@ describe('Test masquerade functionality', () => { afterAll(() => { return User.destroy({ where: { firstName: 'masq-beforeAllUser' } }).then( () =>{ - app.db.sequelize.close() + appInstance.db.sequelize.close() }) }) test('Get new token', () => { let role = authRoutes.roles[ authRoutes.roleKeys["508_COORDINATOR_ROLE"]].name let agency = 'NIH' + let agencyName = 'National Institutes of Health' return testSession.get(`/api/user/masquerade?role=${role}&agency=${agency}`) .set('Authorization', `Bearer ${adminToken}`) .then(async res => { @@ -61,8 +63,8 @@ describe('Test masquerade functionality', () => { let decoded = jwt.decode(res.body.token) expect(decoded.user.grouplist).toBe(authRoutes.roleNameToCASGroup(role)) expect(decoded.user.userRole).toBe(role) - expect(decoded.user.agency).toBe(agency) - expect(res.body.agency).toBe(agency) + expect(decoded.user.agency).toBe(agencyName) + expect(res.body.agency).toBe(agencyName) expect(res.body.role).toBe(role) }) } ) diff --git a/server/tests/noticeType.routes.test.js b/server/tests/noticeType.routes.test.js index b7f6ec2..cb41ea4 100644 --- a/server/tests/noticeType.routes.test.js +++ b/server/tests/noticeType.routes.test.js @@ -1,4 +1,5 @@ -let app = require('../app')(); +const { app, clientPromise } = require('../app'); +const appInstance = app(); const request = require('supertest') // noinspection JSUnresolvedVariable const db = require('../models/index') @@ -24,11 +25,11 @@ describe('noticeType', () => { }) afterAll(() => { - return app.db.close(); + return appInstance.db.close(); }) test('noticeType get API requires login', async () => { - await request(app) + await request(appInstance) .get('/api/noticeTypes') .send() .then((res) => { @@ -36,7 +37,7 @@ describe('noticeType', () => { expect(res.statusCode).toBe(401) }) - await request(app) + await request(appInstance) .get('/api/noticeTypes') .set('Authorization', `Bearer ${token}`) .send() diff --git a/server/tests/prediction.history.test.js b/server/tests/prediction.history.test.js index cfb47a1..9a29c7e 100644 --- a/server/tests/prediction.history.test.js +++ b/server/tests/prediction.history.test.js @@ -1,4 +1,4 @@ -let app = null // require('../app')();; +let appInstance = null // require('../app')();; // noinspection JSUnresolvedVariable const User = require('../models').User const db = require('../models/index') @@ -29,7 +29,8 @@ describe('Prediction History', () => { process.env.minPredictionCutoffDate = '1990-01-01'; process.env.MAIL_ENGINE = 'nodemailer-mock' - app = require('../app')() // don't load the app till the mock is configured + const { app, clientPromise } = require('../app'); + appInstance = app(); // don't load the app till the mock is configured // let allowed_types = configuration.getConfig(config_keys.VISIBLE_NOTICE_TYPES).map( (x) => `'${x}'`).join(",") // let sql = `select solicitation_number @@ -47,7 +48,7 @@ describe('Prediction History', () => { afterAll(() => { return User.destroy({ where: { firstName: 'pred-beforeAllUser' } }) - .then( () => { app.db.close(); }) + .then( () => { appInstance.db.close(); }) }) test('Solicitations have a prediction history element', () => { @@ -69,7 +70,7 @@ describe('Prediction History', () => { // Don't check the date. There are now times where a predication can be updated without new history (ex. becomes inactive) // expect(last).toBe(pdate) let color = history.slice(-1)[0].value - expect(color).toBeOneOf(['red', 'green', 'black']) + expect(color).toBeOneOf(['red', 'green', 'black', 'yellow']) }) }, 10000) diff --git a/server/tests/prediction.routes.test.js b/server/tests/prediction.routes.test.js index 5062cf7..75e7e30 100644 --- a/server/tests/prediction.routes.test.js +++ b/server/tests/prediction.routes.test.js @@ -1,5 +1,5 @@ const request = require('supertest') -let app = null // require('../app')();; +let appInstance = null // require('../app')();; const mockToken = require('./mocktoken') // noinspection JSUnresolvedVariable const User = require('../models').User @@ -104,7 +104,8 @@ describe('prediction tests', () => { // tests can give false failure if the time cuttoff removes all the useful test data process.env.minPredictionCutoffDate = '1990-01-01'; - app = require('../app')() // don't load the app till the mock is configured + const { app, clientPromise } = require('../app'); + appInstance = app(); // don't load the app till the mock is configured myUser = Object.assign({}, userAcceptedCASData) delete myUser.id @@ -129,7 +130,7 @@ describe('prediction tests', () => { afterAll(() => { return User.destroy({ where: { firstName: 'pred-beforeAllUser' } }) .then(() => { - return app.db.close(); + return appInstance.db.close(); }) }) @@ -189,7 +190,7 @@ describe('prediction tests', () => { }) test('Empty predictions filter', () => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send() @@ -206,7 +207,7 @@ describe('prediction tests', () => { test('Test that all predictions with the same notice number are merged', () => { let row_count = 1000 let filter = {rows: row_count, first:0} - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send(filter) @@ -229,7 +230,7 @@ describe('prediction tests', () => { }, timeout) test('Test that all predictions with the same notice number are merged', () => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send() @@ -255,7 +256,7 @@ describe('prediction tests', () => { .then((rows) => { let office = rows[0][0].office - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ office: office }) @@ -271,7 +272,7 @@ describe('prediction tests', () => { } }) .then(() => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ office: 'not a real office' }) @@ -291,7 +292,7 @@ describe('prediction tests', () => { return db.sequelize.query(`select agency from solicitations where "solNum" = '${sample_sol_num}' limit 1;`, null) .then((rows) => { let agency = rows[0][0].agency - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ @@ -304,7 +305,7 @@ describe('prediction tests', () => { return expect(res.statusCode).toBe(501) // we don't yet support numDocs for the filter }) .then(() => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ @@ -332,7 +333,7 @@ describe('prediction tests', () => { expect(solNum).toBeDefined() - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ @@ -349,7 +350,7 @@ describe('prediction tests', () => { }, timeout) test('Test unsupported parameter for Filter predictions', () => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ @@ -361,7 +362,7 @@ describe('prediction tests', () => { expect(res.body.message).toMatch('unsupported') }) .then(() => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ @@ -373,7 +374,7 @@ describe('prediction tests', () => { expect(res.statusCode).toBe(200) }) .then(() => { - return request(app) + return request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ @@ -402,7 +403,7 @@ describe('prediction tests', () => { startBound.setDate( startBound.getDate() - 2) endBound.setDate(endBound.getDate() + 2) - let res = await request(app) + let res = await request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ @@ -421,7 +422,7 @@ describe('prediction tests', () => { expect(new Date(res.body.predictions[i].date) < endBound).toBeTruthy() } - res = await request(app) + res = await request(appInstance) .post('/api/predictions/filter') .set('Authorization', `Bearer ${token}`) .send({ endDate: '1/1/1945' }) diff --git a/server/tests/prediction.update.test.js b/server/tests/prediction.update.test.js index 5cbfd3a..0b73e4f 100644 --- a/server/tests/prediction.update.test.js +++ b/server/tests/prediction.update.test.js @@ -1,4 +1,5 @@ -let app = require('../app')(); +const { app, clientPromise } = require('../app'); +const appInstance = app(); const db = require('../models/index') let predictionRoutes = require('../routes/prediction.routes') // noinspection JSUnresolvedVariable @@ -12,7 +13,7 @@ describe('Prediction Update Test', () => { }) afterAll(() => { - return app.db.close(); + return appInstance.db.close(); }) // TODO: remove test after solications table migration is complete diff --git a/server/tests/predictions.table.test.js b/server/tests/predictions.table.test.js index c822cd2..8414103 100644 --- a/server/tests/predictions.table.test.js +++ b/server/tests/predictions.table.test.js @@ -1,4 +1,5 @@ -let app = require('../app')(); +const { app, clientPromise } = require('../app'); +const appInstance = app(); const db = require('../models/index') let predictionRoutes = require('../routes/prediction.routes') // noinspection JSUnresolvedVariable @@ -16,7 +17,7 @@ describe('Predictions table Test', () => { }) afterAll(() => { - return app.db.close(); + return appInstance.db.close(); }) test('Test solicitation -> feedback association', async () => { diff --git a/server/tests/solicitation.routes.mock.test.js b/server/tests/solicitation.routes.mock.test.js index 07a4047..8b6c7a3 100644 --- a/server/tests/solicitation.routes.mock.test.js +++ b/server/tests/solicitation.routes.mock.test.js @@ -1,4 +1,4 @@ -let app = null // require('../app')() +let appInstance = null // require('../app')() const mockToken = require('./mocktoken') const mocks = require('./mocks') const db = require('../models/index') @@ -24,7 +24,8 @@ describe('solicitation tests', () => { // tests can give false failure if the time cuttoff removes all the useful test data process.env.minPredictionCutoffDate = '1990-01-01'; - app = require('../app')() // don't load the app till the mock is configured + const { app, clientPromise } = require('../app'); + appInstance = app(); // don't load the app till the mock is configured adminToken = await mockToken(adminCASData, common['jwtSecret']) coordinatorToken = await mockToken(coordinatorCASData, common['jwtSecret']) @@ -32,7 +33,7 @@ describe('solicitation tests', () => { }) afterAll(() => { - return app.db.close(); + return appInstance.db.close(); }) test('Update Not Applicable', async () => { diff --git a/server/tests/solicitation.routes.test.js b/server/tests/solicitation.routes.test.js index f73d3e5..0253aef 100644 --- a/server/tests/solicitation.routes.test.js +++ b/server/tests/solicitation.routes.test.js @@ -1,5 +1,5 @@ const request = require('supertest') -let app = null // require('../app')();; +let appInstance = null // require('../app')();; const mockToken = require('./mocktoken') // noinspection JSUnresolvedVariable const User = require('../models').User @@ -31,7 +31,8 @@ describe('solicitation tests', () => { // tests can give false failure if the time cuttoff removes all the useful test data process.env.minPredictionCutoffDate = '1990-01-01'; process.env.MAIL_ENGINE = 'nodemailer-mock' - app = require('../app')() // don't load the app till the mock is configured + const { app, clientPromise } = require('../app'); + appInstance = app(); // don't load the app till the mock is configured solicitationRoutes = require('../routes/solicitation.routes')( db, { whoAmI: () => myUser.email }) @@ -48,7 +49,7 @@ describe('solicitation tests', () => { afterAll(() => { return User.destroy({ where: { firstName: 'sol-beforeAllUser' } }) - .then( () => { app.db.close(); }) + .then( () => { appInstance.db.close(); }) }) /*** @@ -87,7 +88,7 @@ describe('solicitation tests', () => { 'answer': 'second answer' } ) - let res = await request(app) + let res = await request(appInstance) .post('/api/solicitation') .set('Authorization', `Bearer ${token}`) .send( @@ -127,7 +128,7 @@ describe('solicitation tests', () => { let id = await testUtils.solNumToSolicitationID(solNum) expect(id).toBeDefined() - return request(app) + return request(appInstance) .get('/api/solicitation/' + id) .set('Authorization', `Bearer ${token}`) .send({}) @@ -148,7 +149,7 @@ describe('solicitation tests', () => { .then((rows) => { let id = rows[0][0].id + 9999 expect(id).toBeDefined() - return request(app) + return request(appInstance) .get('/api/solicitation/' + id) .set('Authorization', `Bearer ${token}`) .send({}) @@ -161,7 +162,7 @@ describe('solicitation tests', () => { test('sending a too large/invalid ID to get solicitation', () => { let url = '/api/solicitation/' + Number.MAX_SAFE_INTEGER - return request(app) + return request(appInstance) .get(url) .set('Authorization', `Bearer ${token}`) .send({}) @@ -180,7 +181,7 @@ describe('solicitation tests', () => { return db.sequelize.query(`select id from notice where solicitation_number = '${solNum}' limit 1`) .then(rows => { let noticeId = rows[0][0].id - return request(app) + return request(appInstance) .get('/api/solicitation/' + noticeId) .set('Authorization', `Bearer ${token}`) .send({}) @@ -196,10 +197,9 @@ describe('solicitation tests', () => { }) test('get solicitation feedback (using POST of all things because that is how the UI is coded', async () => { - let app = require('../app')() const solNum = await testUtils.getSolNumForTesting({has_feedback: true}) - return request(app) + return request(appInstance) .post('/api/feedback') .set('Authorization', `Bearer ${token}`) .send({ solNum: solNum}) @@ -254,7 +254,7 @@ describe('solicitation tests', () => { let solNum = await testUtils.getSolNumForTesting({"attachment_count": 1}) let solId = await testUtils.solNumToSolicitationID(solNum) let files = await db.sequelize.query(`select filename from attachment where solicitation_id = '${solId}'`) - let res = await request(app) + let res = await request(appInstance) .get('/api/solicitation/' + solId) .set('Authorization', `Bearer ${token}`) .send({}) diff --git a/server/tests/token.test.js b/server/tests/token.test.js index d61d585..a6c9cdd 100644 --- a/server/tests/token.test.js +++ b/server/tests/token.test.js @@ -56,11 +56,12 @@ describe('JWT Tests', () => { }) test('refresh token API call', async () => { - let app = require('../app')() + const { app, clientPromise } = require('../app'); + const appInstance = app(); /** * @type {Session} */ - let testSession = supertestSession(app) + let testSession = supertestSession(appInstance) // noinspection JSUnresolvedFunction return testSession.get('/api/renewToken') @@ -81,11 +82,12 @@ describe('JWT Tests', () => { }) test('Valid token required for refresh', async () => { - let app = require('../app')() + const { app, clientPromise } = require('../app'); + const appInstance = app(); /** * @type {Session} */ - let testSession = supertestSession(app) + let testSession = supertestSession(appInstance) // noinspection JSUnresolvedFunction return testSession.get('/api/renewToken') @@ -98,11 +100,12 @@ describe('JWT Tests', () => { test('timed out tokens can not be used for refresh', async() => { - let app = require('../app')() + const { app, clientPromise } = require('../app'); + const appInstance = app(); /** * @type {Session} */ - let testSession = supertestSession(app) + let testSession = supertestSession(appInstance) let token = await mockToken(userAcceptedCASData, null, null, 10) // generate an expired token // noinspection JSUnresolvedFunction @@ -124,11 +127,12 @@ describe('JWT Tests', () => { global.Date.now = jest.fn(() => realNow - (common.sessionLength * 1000) - 10000); - let app = require('../app')() + const { app, clientPromise } = require('../app'); + const appInstance = app(); /** * @type {Session} */ - let testSession = supertestSession(app) + let testSession = supertestSession(appInstance) let token = await mockToken(userAcceptedCASData) // generate an expired token global.Date.now = realDateNow; // restore the proper date implementation @@ -192,11 +196,14 @@ describe('JWT Tests', () => { casConfig.is_dev_mode = true casConfig.dev_mode_user = "dev_user" let cas = new CASAuthentication(casConfig) - let app = require('../app')(null, cas) + + const { app, clientPromise } = require('../app'); + const appInstance = app(null, cas); + /** * @type {Session} */ - let testSession = supertestSession(app) + let testSession = supertestSession(appInstance) // noinspection JSUnresolvedFunction return testSession.get('/api/casLogin') @@ -238,7 +245,7 @@ describe('JWT Tests', () => { let locationRedirect = res.set.mock.calls[0][1] res.set.mock.calls - let matches = locationRedirect.match('token=({[^}]+})') + let matches = locationRedirect.match('info=({[^}]+})') let userTokenData = JSON.parse(matches[1]) expect(userTokenData.agency).toBe("TEST, DEPARTMENT OF") let decoded = jwt.decode(userTokenData.token) diff --git a/server/tests/user.routes.test.js b/server/tests/user.routes.test.js index 45a1d76..ef23287 100644 --- a/server/tests/user.routes.test.js +++ b/server/tests/user.routes.test.js @@ -1,5 +1,5 @@ const request = require('supertest') -let app = null // require('../app')(); +let appInstance = null // require('../app')(); const mockToken = require('./mocktoken') // noinspection JSUnresolvedVariable const User = require('../models').User @@ -21,7 +21,8 @@ describe('User API Routes', () => { userAcceptedCASData = Object.assign({}, userAcceptedCASData, { "email-address": 'crowley+accepted-user@tcg.com', firstName: 'beforeAllUser' }) process.env.MAIL_ENGINE = 'nodemailer-mock' - app = require('../app')() // don't load the app till the mock is configured + const { app, clientPromise } = require('../app'); + appInstance = app(); // don't load the app till the mock is configured let filterUser = Object.assign({}, userAcceptedCASData) filterUser.firstName = 'beforeAll-filter' @@ -89,7 +90,7 @@ describe('User API Routes', () => { }) test('/api/user/update', async () => { - return request(app) + return request(appInstance) .post('/api/user/update') .send({ id: user1Id, isAccepted: false, isRejected: false }) .set('Authorization', `Bearer ${token}`) @@ -101,7 +102,7 @@ describe('User API Routes', () => { test('/api/user/updatePassword', async () => { - await request(app) + await request(appInstance) .post('/api/user/updatePassword') .send({ password: 'newPassword', oldpassword: 'not the old password or temp password' }) .set('Authorization', `Bearer ${token}`) @@ -112,7 +113,7 @@ describe('User API Routes', () => { }) test('test /api/user/getUserInfo', async () => { - await request(app) + await request(appInstance) .get('/api/user/getUserInfo') .send({ UserId: acceptedUserId }) .set('Authorization', `Bearer ${token}`) @@ -124,7 +125,7 @@ describe('User API Routes', () => { }) test('test filter', async () => { - return request(app) + return request(appInstance) .post('/api/user/filter') .send({ isAccepted: true }) .set('Authorization', `Bearer ${token}`) @@ -137,7 +138,7 @@ describe('User API Routes', () => { }) test('authentication required', async () => { - return request(app) + return request(appInstance) .post('/api/user/filter') .then((response) => { // noinspection JSUnresolvedVariable,JSUnresolvedVariable @@ -154,7 +155,7 @@ describe('User API Routes', () => { let results = await db.sequelize.query(sql, null) let id = results[0][0].id - let response = await request(app) + let response = await request(appInstance) .post('/api/user/getUserInfo') .set('Authorization', `Bearer ${token}`) .send({UserID: id}) diff --git a/server/tests/version.routes.test.js b/server/tests/version.routes.test.js index e16ec00..092715e 100644 --- a/server/tests/version.routes.test.js +++ b/server/tests/version.routes.test.js @@ -1,9 +1,10 @@ const request = require('supertest') -let app = require('../app')() +const { app, clientPromise } = require('../app'); +const appInstance = app(); describe('Version route tests', () => { test('Get version', () => { - return request(app) + return request(appInstance) .get('/api/version') .send({}) .then(res => {