Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fsr 1055 #36

Merged
merged 12 commits into from
Dec 4, 2023
2 changes: 1 addition & 1 deletion lib/functions/getMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports.getMessage = async (event) => {

const ret = await service.getMessage(event.pathParameters.id)

if (!ret?.rows?.[0]?.getmessage) {
if (!ret || !ret.rows || !Array.isArray(ret.rows) || ret.rows.length < 1 || !ret.rows[0].getmessage) {
console.log('No message found for ' + event.pathParameters.id)
throw new Error('No message found')
}
Expand Down
4 changes: 2 additions & 2 deletions lib/functions/processMessage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const xml2js = require('xml2js')
const moment = require('moment')
const AWSConfig = require('../../config/config.json').aws
const service = require('../helpers/service')
Expand All @@ -19,10 +20,9 @@ module.exports.processMessage = async (event) => {

// Add in the references field
const xmlMessage = event.bodyXml.replace('</scope>', '</scope>\n<references></references>')
const parseString = require('xml2js').parseString

const xmlResult = await new Promise((resolve, reject) => {
parseString(xmlMessage, function (err, value) {
xml2js.parseString(xmlMessage, (err, value) => {
if (err) return reject(err)
resolve(value)
})
Expand Down
21 changes: 18 additions & 3 deletions test/getMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const lab = exports.lab = Lab.script()
const Code = require('@hapi/code')
const getMessage = require('../lib/functions/getMessage').getMessage
const service = require('../lib/helpers/service')

let event

lab.experiment('getMessage', () => {
Expand Down Expand Up @@ -51,7 +52,16 @@ lab.experiment('getMessage', () => {

lab.test('Incorrect database rows object', async () => {
service.getMessage = (query, params) => Promise.resolve({
rows: {}
rows: [{}]
})

const err = await Code.expect(getMessage(event)).to.reject()
Code.expect(err.message).to.equal('No message found')
})

lab.test('Missing database rows object', async () => {
service.getMessage = (query, params) => Promise.resolve({
no_rows: []
})

const err = await Code.expect(getMessage(event)).to.reject()
Expand Down Expand Up @@ -85,10 +95,15 @@ lab.experiment('getMessage', () => {
await Code.expect(getMessage(event)).to.reject()
})
lab.test('Invalid id format test', async () => {
// Set the id to a value that is not a hexadecimal string
event.pathParameters.id = 'invalid_id_format'

// Expect the getMessage function to reject due to validation failure
await Code.expect(getMessage(event)).to.reject()
})
lab.test('Valid id format test', async () => {
event.pathParameters.id = 'a1b2c3'
const result = await getMessage(event)
const body = result.body

Code.expect(body).to.equal('<alert xmlns="urn:oasis:names:tc:emergency:cap:1.2">test</alert>')
})
})
5 changes: 5 additions & 0 deletions test/processMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,9 @@ lab.experiment('processMessage', () => {
// Expect the processMessage function to reject due to validation failure
await Code.expect(processMessage({ bodyXml: invalidBodyXml })).to.reject()
})
lab.test('Valid bodyXml format test', async () => {
const validBodyXml = capAlert.bodyXml

await Code.expect(processMessage({ bodyXml: validBodyXml })).to.not.reject()
})
})
58 changes: 58 additions & 0 deletions test/schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

const Joi = require('joi')
const Lab = require('@hapi/lab')
const lab = exports.lab = Lab.script()
const Code = require('@hapi/code')
const configSchema = require('../config/schema')
const { getMessageEventSchema, processMessageEventSchema } = require('../lib/helpers/schemas')

lab.experiment('schemas', () => {
lab.test('configSchema', () => {
Code.expect(Joi.isSchema(configSchema)).to.equal(true)
})

lab.test('getMessageEventSchema', () => {
Code.expect(Joi.isSchema(getMessageEventSchema)).to.equal(true)
})

lab.test('getMessageEventSchema with invalid data', () => {
const invalidSampleData = { pathParameters: { id: 'invalid_hex' } }
const validationResult = getMessageEventSchema.validate(invalidSampleData)

Code.expect(validationResult.error).to.not.be.null()
})

lab.test('getMessageEventSchema with missing id', () => {
const dataWithoutId = { pathParameters: {} }
const validationResult = getMessageEventSchema.validate(dataWithoutId)

Code.expect(validationResult.error).to.not.be.null()
})

lab.test('processMessageEventSchema', () => {
Code.expect(Joi.isSchema(processMessageEventSchema)).to.equal(true)
Code.expect(processMessageEventSchema.validate({ bodyXml: '<xml />' })).to.equal({ value: { bodyXml: '<xml />' } })
})
lab.test('processMessageEventSchema with valid data', () => {
const validSampleData = { bodyXml: '<xml />' }
const validationResult = processMessageEventSchema.validate(validSampleData)

Code.expect(validationResult.error).to.not.exist()
Code.expect(validationResult.value).to.equal(validSampleData)
})

lab.test('processMessageEventSchema with missing bodyXml', () => {
const dataWithoutBodyXml = {}
const validationResult = processMessageEventSchema.validate(dataWithoutBodyXml)

Code.expect(validationResult.error).to.not.be.null()
})

lab.test('processMessageEventSchema with invalid bodyXml', () => {
const invalidSampleData = { bodyXml: 123 }
const validationResult = processMessageEventSchema.validate(invalidSampleData)

Code.expect(validationResult.error).to.not.be.null()
})
})