Skip to content

Commit

Permalink
Merge pull request #151 from GSA/feature/test_updates
Browse files Browse the repository at this point in the history
Test Updates
  • Loading branch information
BuckinghamAJ authored Apr 29, 2024
2 parents 9b3b487 + 50bb97d commit 5d162fd
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 113 deletions.
2 changes: 1 addition & 1 deletion server/routes/auth.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand Down
7 changes: 4 additions & 3 deletions server/tests/admin.check.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
Expand All @@ -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()
})
})

Expand Down
5 changes: 3 additions & 2 deletions server/tests/admin.reports.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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 () => {
Expand Down
9 changes: 5 additions & 4 deletions server/tests/agency.routes.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) => {
Expand All @@ -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 })
Expand All @@ -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 })
Expand Down
7 changes: 4 additions & 3 deletions server/tests/analytics.routes.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'})
Expand All @@ -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'})
Expand Down
7 changes: 4 additions & 3 deletions server/tests/app.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let app = require('../app')()
const { app, clientPromise } = require('../app');
const appInstance = app();
let {getConfig} = require('../config/configuration')


Expand All @@ -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)
})
Expand Down
66 changes: 42 additions & 24 deletions server/tests/auth.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'])
})

Expand All @@ -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) => {
Expand All @@ -65,7 +67,7 @@ describe('/api/auth/', () => {
user.userRole = 'Public'
user.email = '[email protected]'
let token = await mockToken(user, common['jwtSecret'])
return request(app)
return request(appInstance)
.post('/api/auth/tokenCheck')
.send({ token: token })
.then((res) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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()
Expand All @@ -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}
*/
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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

})
})
Expand All @@ -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)
Expand All @@ -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 }})
Expand Down Expand Up @@ -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)
})

Expand All @@ -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)
})

Expand Down
11 changes: 6 additions & 5 deletions server/tests/email.routes.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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' })
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 5d162fd

Please sign in to comment.