From 2d2fdbb8dbec84dfaee0f6848f774a68e5ad7750 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 1 Aug 2023 22:45:08 +0100 Subject: [PATCH] Corrections to unit tests Signed-off-by: Fred Boniface --- package.json | 32 ++++----- src/utils/newSanitizer.ts | 10 +++ src/utils/sanitizer.utils.ts | 13 +--- src/utils/translators/ldb/staffStation.ts | 68 ++++++++++++-------- test/utils/newSanitizer.utils.test.ts | 9 +++ test/utils/sanitizer.utils.test.ts | 25 ++++--- test/utils/translators/ldb/stationInputs.ts | 2 +- test/utils/translators/ldb/stationOutputs.ts | 4 +- 8 files changed, 97 insertions(+), 66 deletions(-) create mode 100644 src/utils/newSanitizer.ts create mode 100644 test/utils/newSanitizer.utils.test.ts diff --git a/package.json b/package.json index 9d98905..d4be9d8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils/newSanitizer.ts b/src/utils/newSanitizer.ts new file mode 100644 index 0000000..0dbf925 --- /dev/null +++ b/src/utils/newSanitizer.ts @@ -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 ""; + } + }); +} \ No newline at end of file diff --git a/src/utils/sanitizer.utils.ts b/src/utils/sanitizer.utils.ts index da4136b..61c189d 100644 --- a/src/utils/sanitizer.utils.ts +++ b/src/utils/sanitizer.utils.ts @@ -20,18 +20,7 @@ function cleanNrcc(input: string) { // Remove newlines and then

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]; } diff --git a/src/utils/translators/ldb/staffStation.ts b/src/utils/translators/ldb/staffStation.ts index f282ac0..5647542 100644 --- a/src/utils/translators/ldb/staffStation.ts +++ b/src/utils/translators/ldb/staffStation.ts @@ -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() } \ No newline at end of file diff --git a/test/utils/newSanitizer.utils.test.ts b/test/utils/newSanitizer.utils.test.ts new file mode 100644 index 0000000..ca7f952 --- /dev/null +++ b/test/utils/newSanitizer.utils.test.ts @@ -0,0 +1,9 @@ +import { removeNewlineAndPTag } from "../../src/utils/newSanitizer"; + +describe('newSanitizer', () => { + test('Should remove /\n and

/

elements', () => { + const input = "\n

This is a string

"; + const expectedOutput = "This is a string" + expect(removeNewlineAndPTag(input)).toEqual(expectedOutput); + }); +}); \ No newline at end of file diff --git a/test/utils/sanitizer.utils.test.ts b/test/utils/sanitizer.utils.test.ts index d506cbd..f7c80ce 100644 --- a/test/utils/sanitizer.utils.test.ts +++ b/test/utils/sanitizer.utils.test.ts @@ -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

/

elements', () => { - const input = "\n

This is a string

"; - const expectedOutput = "This is a string" - expect(removeNewlineAndPTag(input)).toEqual(expectedOutput); - }); -}); \ No newline at end of file +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); + }) + } +}); diff --git a/test/utils/translators/ldb/stationInputs.ts b/test/utils/translators/ldb/stationInputs.ts index 9ea0023..e7a348e 100644 --- a/test/utils/translators/ldb/stationInputs.ts +++ b/test/utils/translators/ldb/stationInputs.ts @@ -19,7 +19,7 @@ export const inputs: any[] = [ { rid: "202308017159276", uid: "G59276", - trainuid: "1M83", + trainid: "1M83", sdd: "2023-08-01", operator: "CrossCountry", operatorCode: "XC", diff --git a/test/utils/translators/ldb/stationOutputs.ts b/test/utils/translators/ldb/stationOutputs.ts index 2b9d609..2c0a146 100644 --- a/test/utils/translators/ldb/stationOutputs.ts +++ b/test/utils/translators/ldb/stationOutputs.ts @@ -35,6 +35,8 @@ export const outputs: any[] = [ } ] } - ] + ], + busServices: [], + ferryServices: [] }, ] \ No newline at end of file