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": { "dependencies": {
"axios": "^1.2.1", "axios": "^1.2.1",
"compression": "^1.7.4", "compression": "^1.7.4",
@ -14,22 +30,6 @@
"redis": "^4.6.7", "redis": "^4.6.7",
"zlib": "^1.0.5" "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": { "devDependencies": {
"@owlboard/ts-types": "^0.0.3", "@owlboard/ts-types": "^0.0.3",
"@types/jest": "^29.5.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; return cleanInput;
} }
export function removeNewlineAndPTag(input: string): string { function getDomainFromEmail(mail: string) { // Needs testing
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
let split = mail.split('@'); let split = mail.split('@');
return split[1]; return split[1];
} }

View File

@ -2,7 +2,7 @@ import type { StaffLdb, NrccMessage, TrainServices,
ServiceLocation } from '@owlboard/ts-types'; ServiceLocation } from '@owlboard/ts-types';
import { tz } from 'moment-timezone'; 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 /// I do not yet have a type defined for any of the input object
export function transform(input: any): StaffLdb | null { export function transform(input: any): StaffLdb | null {
@ -14,9 +14,9 @@ export function transform(input: any): StaffLdb | null {
locationName: data?.locationName || "Not Found", locationName: data?.locationName || "Not Found",
stationManagerCode: data?.stationManagerCode || "UK", stationManagerCode: data?.stationManagerCode || "UK",
nrccMessages: transformNrcc(data?.nrccMessages) || undefined, nrccMessages: transformNrcc(data?.nrccMessages) || undefined,
trainServices: transformTrainServices(data?.trainServices), trainServices: transformTrainServices(data?.trainServices) || undefined,
busServices: transformTrainServices(data?.busServices), busServices: transformTrainServices(data?.busServices) || undefined,
ferryServices: transformTrainServices(data?.ferryServices) ferryServices: transformTrainServices(data?.ferryServices) || undefined
} }
return output return output
} catch (err) { } catch (err) {
@ -26,15 +26,18 @@ export function transform(input: any): StaffLdb | null {
} }
function transformDateTime(input: string): Date { function transformDateTime(input: string): Date {
console.debug(`staffStation.transformDateTime Input: ${input}`)
return new Date(input) return new Date(input)
} }
function transformNrcc(input: any): NrccMessage[] { function transformNrcc(input: any): NrccMessage[] {
console.debug(`staffStations.transformNrcc: Running`)
let output: NrccMessage[] = [] let output: NrccMessage[] = []
let messages = input
if (!Array.isArray(input?.message)) { 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 = { let message: NrccMessage = {
severity: item?.severity, severity: item?.severity,
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage) xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage)
@ -45,37 +48,45 @@ function transformNrcc(input: any): NrccMessage[] {
} }
function transformTrainServices(input: any): TrainServices[] { function transformTrainServices(input: any): TrainServices[] {
let services: any = input.service console.debug(`staffStation.transformTrainServices running`)
let services: any = input?.service
let output: TrainServices[] = [] let output: TrainServices[] = []
if (services === undefined) {
return output
}
if (!Array.isArray(input.service)) { if (!Array.isArray(input.service)) {
services = [input.service] services = [input.service]
} }
const trainServices: TrainServices = { for (const service of services) {
rid: services?.rid, const trainService: TrainServices = {
uid: services?.uid, rid: service?.rid,
trainid: services?.trainid, uid: service?.uid,
operatorCode: services?.operatorCode || 'UK', trainid: service?.trainid,
platform: services?.platform || '', operatorCode: service?.operatorCode || 'UK',
platformIsHidden: services?.platformIsHidden || '', platform: service?.platform || '',
serviceIsSupressed: services?.serviceIsSupressed || '', platformIsHidden: service?.platformIsHidden || '',
origin: transformLocation(services?.origin), serviceIsSupressed: service?.serviceIsSupressed || '',
destination: transformLocation(services?.destination), origin: transformLocation(service?.origin),
isCancelled: services?.isCancelled || '', destination: transformLocation(service?.destination),
cancelReason: services?.cancelReason, isCancelled: service?.isCancelled || '',
delayReason: services?.delayReason, cancelReason: service?.cancelReason,
arrivalType: services?.arrivalType, delayReason: service?.delayReason,
departureType: services?.departureType, arrivalType: service?.arrivalType,
sta: transformUnspecifiedDateTime(services?.sta), departureType: service?.departureType,
eta: transformUnspecifiedDateTime(services?.eta), sta: transformUnspecifiedDateTime(service?.sta),
ata: transformUnspecifiedDateTime(services?.ata), eta: transformUnspecifiedDateTime(service?.eta),
std: transformUnspecifiedDateTime(services?.std), ata: transformUnspecifiedDateTime(service?.ata),
etd: transformUnspecifiedDateTime(services?.etd), std: transformUnspecifiedDateTime(service?.std),
atd: transformUnspecifiedDateTime(services?.atd), etd: transformUnspecifiedDateTime(service?.etd),
atd: transformUnspecifiedDateTime(service?.atd),
}
output.push(trainService)
} }
return output return output
} }
function transformLocation(input: any): ServiceLocation[] { function transformLocation(input: any): ServiceLocation[] {
console.debug(`staffStation.transformLocation running`)
let output: ServiceLocation[] = [] let output: ServiceLocation[] = []
if (!Array.isArray(input)) { if (!Array.isArray(input)) {
input = [input] input = [input]
@ -91,6 +102,7 @@ function transformLocation(input: any): ServiceLocation[] {
} }
function transformUnspecifiedDateTime(input: string): Date { function transformUnspecifiedDateTime(input: string): Date {
console.debug(`staffStation.transformUnspecifiedDateTime running`)
const date = tz(input, "Europe/London"); const date = tz(input, "Europe/London");
return date.toDate() 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', () => { describe('Sanitize Email', () => {
test('Should remove /\n and <p>/</p> elements', () => { const inputs = [
const input = "\n<p>This is a string</p>"; "this+is+an-_email@example.com",
const expectedOutput = "This is a string" '"unusual email"@example.com',
expect(removeNewlineAndPTag(input)).toEqual(expectedOutput); "(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", rid: "202308017159276",
uid: "G59276", uid: "G59276",
trainuid: "1M83", trainid: "1M83",
sdd: "2023-08-01", sdd: "2023-08-01",
operator: "CrossCountry", operator: "CrossCountry",
operatorCode: "XC", operatorCode: "XC",

View File

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