Corrections to unit tests

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-08-01 22:45:08 +01:00
parent 0e748d545e
commit 2d2fdbb8db
8 changed files with 97 additions and 66 deletions

View File

@ -1,4 +1,20 @@
{
"name": "owlboard",
"version": "1.2.0-dev",
"description": "OwlBoard is an API and PWA for live rail departure board in the UK.",
"repository": {
"type": "git",
"url": "https://git.fjla.uk/owlboard/backend.git"
},
"license": "GPL-3.0-or-later",
"author": "Fred Boniface",
"main": "express.js",
"scripts": {
"build": "tsc",
"run": "tsc && node dist/app.js",
"start": "node app.js",
"test": "jest"
},
"dependencies": {
"axios": "^1.2.1",
"compression": "^1.7.4",
@ -14,22 +30,6 @@
"redis": "^4.6.7",
"zlib": "^1.0.5"
},
"name": "owlboard",
"description": "OwlBoard is an API and PWA for live rail departure board in the UK.",
"version": "1.2.0-dev",
"main": "express.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js",
"run": "tsc && node dist/app.js",
"build": "tsc"
},
"repository": {
"type": "git",
"url": "https://git.fjla.uk/owlboard/backend.git"
},
"author": "Fred Boniface",
"license": "GPL-3.0-or-later",
"devDependencies": {
"@owlboard/ts-types": "^0.0.3",
"@types/jest": "^29.5.3",

10
src/utils/newSanitizer.ts Normal file
View File

@ -0,0 +1,10 @@
export function removeNewlineAndPTag(input: string): string {
const regex = /[\n\r]|<\/?p[^>]*>/g;
return input.replace(regex, function(match) {
if (match === "\n" || match === "\r") {
return "";
} else {
return "";
}
});
}

View File

@ -20,18 +20,7 @@ function cleanNrcc(input: string) { // Remove newlines and then <p> tags from in
return cleanInput;
}
export function removeNewlineAndPTag(input: string): string {
const regex = /[\n\r]|<\/?p[^>]*>/g;
return input.replace(regex, function(match) {
if (match === "\n" || match === "\r") {
return "";
} else {
return "";
}
});
}
async function getDomainFromEmail(mail: string) { // Needs testing
function getDomainFromEmail(mail: string) { // Needs testing
let split = mail.split('@');
return split[1];
}

View File

@ -2,7 +2,7 @@ import type { StaffLdb, NrccMessage, TrainServices,
ServiceLocation } from '@owlboard/ts-types';
import { tz } from 'moment-timezone';
import { removeNewlineAndPTag } from '../../sanitizer.utils';
import { removeNewlineAndPTag } from '../../newSanitizer';
/// I do not yet have a type defined for any of the input object
export function transform(input: any): StaffLdb | null {
@ -14,9 +14,9 @@ export function transform(input: any): StaffLdb | null {
locationName: data?.locationName || "Not Found",
stationManagerCode: data?.stationManagerCode || "UK",
nrccMessages: transformNrcc(data?.nrccMessages) || undefined,
trainServices: transformTrainServices(data?.trainServices),
busServices: transformTrainServices(data?.busServices),
ferryServices: transformTrainServices(data?.ferryServices)
trainServices: transformTrainServices(data?.trainServices) || undefined,
busServices: transformTrainServices(data?.busServices) || undefined,
ferryServices: transformTrainServices(data?.ferryServices) || undefined
}
return output
} catch (err) {
@ -26,15 +26,18 @@ export function transform(input: any): StaffLdb | null {
}
function transformDateTime(input: string): Date {
console.debug(`staffStation.transformDateTime Input: ${input}`)
return new Date(input)
}
function transformNrcc(input: any): NrccMessage[] {
console.debug(`staffStations.transformNrcc: Running`)
let output: NrccMessage[] = []
let messages = input
if (!Array.isArray(input?.message)) {
input.message = [input?.message]
messages = [input?.message]
}
for (const item of input?.message) {
for (const item of messages) {
let message: NrccMessage = {
severity: item?.severity,
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage)
@ -45,37 +48,45 @@ function transformNrcc(input: any): NrccMessage[] {
}
function transformTrainServices(input: any): TrainServices[] {
let services: any = input.service
console.debug(`staffStation.transformTrainServices running`)
let services: any = input?.service
let output: TrainServices[] = []
if (services === undefined) {
return output
}
if (!Array.isArray(input.service)) {
services = [input.service]
}
const trainServices: TrainServices = {
rid: services?.rid,
uid: services?.uid,
trainid: services?.trainid,
operatorCode: services?.operatorCode || 'UK',
platform: services?.platform || '',
platformIsHidden: services?.platformIsHidden || '',
serviceIsSupressed: services?.serviceIsSupressed || '',
origin: transformLocation(services?.origin),
destination: transformLocation(services?.destination),
isCancelled: services?.isCancelled || '',
cancelReason: services?.cancelReason,
delayReason: services?.delayReason,
arrivalType: services?.arrivalType,
departureType: services?.departureType,
sta: transformUnspecifiedDateTime(services?.sta),
eta: transformUnspecifiedDateTime(services?.eta),
ata: transformUnspecifiedDateTime(services?.ata),
std: transformUnspecifiedDateTime(services?.std),
etd: transformUnspecifiedDateTime(services?.etd),
atd: transformUnspecifiedDateTime(services?.atd),
for (const service of services) {
const trainService: TrainServices = {
rid: service?.rid,
uid: service?.uid,
trainid: service?.trainid,
operatorCode: service?.operatorCode || 'UK',
platform: service?.platform || '',
platformIsHidden: service?.platformIsHidden || '',
serviceIsSupressed: service?.serviceIsSupressed || '',
origin: transformLocation(service?.origin),
destination: transformLocation(service?.destination),
isCancelled: service?.isCancelled || '',
cancelReason: service?.cancelReason,
delayReason: service?.delayReason,
arrivalType: service?.arrivalType,
departureType: service?.departureType,
sta: transformUnspecifiedDateTime(service?.sta),
eta: transformUnspecifiedDateTime(service?.eta),
ata: transformUnspecifiedDateTime(service?.ata),
std: transformUnspecifiedDateTime(service?.std),
etd: transformUnspecifiedDateTime(service?.etd),
atd: transformUnspecifiedDateTime(service?.atd),
}
output.push(trainService)
}
return output
}
function transformLocation(input: any): ServiceLocation[] {
console.debug(`staffStation.transformLocation running`)
let output: ServiceLocation[] = []
if (!Array.isArray(input)) {
input = [input]
@ -91,6 +102,7 @@ function transformLocation(input: any): ServiceLocation[] {
}
function transformUnspecifiedDateTime(input: string): Date {
console.debug(`staffStation.transformUnspecifiedDateTime running`)
const date = tz(input, "Europe/London");
return date.toDate()
}

View File

@ -0,0 +1,9 @@
import { removeNewlineAndPTag } from "../../src/utils/newSanitizer";
describe('newSanitizer', () => {
test('Should remove /\n and <p>/</p> elements', () => {
const input = "\n<p>This is a string</p>";
const expectedOutput = "This is a string"
expect(removeNewlineAndPTag(input)).toEqual(expectedOutput);
});
});

View File

@ -1,9 +1,18 @@
import { removeNewlineAndPTag } from "../../src/utils/sanitizer.utils";
import { getDomainFromEmail } from "../../src/utils/sanitizer.utils";
describe('sanitizer', () => {
test('Should remove /\n and <p>/</p> elements', () => {
const input = "\n<p>This is a string</p>";
const expectedOutput = "This is a string"
expect(removeNewlineAndPTag(input)).toEqual(expectedOutput);
});
describe('Sanitize Email', () => {
const inputs = [
"this+is+an-_email@example.com",
'"unusual email"@example.com',
"(brackets)addr@example.com",
"I%Have{Special}%Characters@example.com",
"Basic.address@example.com",
`"very.(),:;<>[]\".VERY.\"very\ \"very\".unusual"@example.com`
]
const expectedOutput = "example.com"
for (const addr of inputs) {
test(`Should return only domain: ${addr}`, () => {
expect(getDomainFromEmail(addr)).toEqual(expectedOutput);
})
}
});

View File

@ -19,7 +19,7 @@ export const inputs: any[] = [
{
rid: "202308017159276",
uid: "G59276",
trainuid: "1M83",
trainid: "1M83",
sdd: "2023-08-01",
operator: "CrossCountry",
operatorCode: "XC",

View File

@ -35,6 +35,8 @@ export const outputs: any[] = [
}
]
}
]
],
busServices: [],
ferryServices: []
},
]