From 59c1c58b0849bc951422d37c4e427c77a350dd6e Mon Sep 17 00:00:00 2001 From: George Goodall Date: Tue, 5 Dec 2023 11:32:26 +0000 Subject: [PATCH 1/9] log page requests and include sessionId and ipAddress --- src/controllers/MyController.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/controllers/MyController.js b/src/controllers/MyController.js index 2b3c7110..c6a5506e 100644 --- a/src/controllers/MyController.js +++ b/src/controllers/MyController.js @@ -1,4 +1,5 @@ import hmpoFormWizard from 'hmpo-form-wizard' +import logger from '../utils/logger.js' const { Controller } = hmpoFormWizard class MyController extends Controller { @@ -6,6 +7,17 @@ class MyController extends Controller { req.form.options.lastPage = req.journeyModel.get('lastVisited') super.locals(req, res, callback) } + + get (req, res) { + logger.info({ + type: 'PageView', + endpoint: req.originalUrl, + message: `page view occurred for page: ${req.originalUrl}`, + sessionId: req.sessionID, + ipAddress: req.ip + }) + super.get(req, res) + } } export default MyController From e9ea3fcd96fc630bdc1b713bbb77964f3699e528 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Tue, 5 Dec 2023 11:33:04 +0000 Subject: [PATCH 2/9] send sessionId and ipAddress with form data posted to the api --- src/controllers/uploadController.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index 9d3c61b5..1fa9b239 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -27,7 +27,9 @@ class UploadController extends MyController { fileName: req.file.originalname, dataset: req.sessionModel.get('dataset'), dataSubject: req.sessionModel.get('data-subject'), - organisation: 'local-authority-eng:CAT' // ToDo: this needs to be dynamic, not collected in the prototype, should it be? + organisation: 'local-authority-eng:CAT', // ToDo: this needs to be dynamic, not collected in the prototype, should it be? + sessionId: req.sessionID, + ipAddress: req.ip }) this.errorCount = jsonResult['issue-log'].filter(issue => issue.severity === severityLevels.error).length req.body.datafile = req.file @@ -39,11 +41,13 @@ class UploadController extends MyController { super.post(req, res, next) } - async validateFile ({ filePath, fileName, dataset, dataSubject, organisation }) { + async validateFile ({ filePath, fileName, dataset, dataSubject, organisation, sessionId, ipAddress }) { const formData = new FormData() formData.append('dataset', dataset) formData.append('collection', dataSubject) formData.append('organisation', organisation) + formData.append('sessionId', sessionId) + formData.append('ipAddress', ipAddress) const file = new Blob([await readFile(filePath)], { type: lookup(filePath) }) From e8611e2a025d597c1c37eecaf2b1247a4ef7dda0 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Tue, 5 Dec 2023 13:34:17 +0000 Subject: [PATCH 3/9] updated myController to be called myPageController --- src/controllers/{MyController.js => MyPageController.js} | 4 ++-- src/controllers/datasetController.js | 4 ++-- src/controllers/errorsController.js | 4 ++-- src/controllers/uploadController.js | 4 ++-- src/routes/form-wizard/steps.js | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) rename src/controllers/{MyController.js => MyPageController.js} (87%) diff --git a/src/controllers/MyController.js b/src/controllers/MyPageController.js similarity index 87% rename from src/controllers/MyController.js rename to src/controllers/MyPageController.js index c6a5506e..e7c2f3f5 100644 --- a/src/controllers/MyController.js +++ b/src/controllers/MyPageController.js @@ -2,7 +2,7 @@ import hmpoFormWizard from 'hmpo-form-wizard' import logger from '../utils/logger.js' const { Controller } = hmpoFormWizard -class MyController extends Controller { +class MyPageController extends Controller { locals (req, res, callback) { req.form.options.lastPage = req.journeyModel.get('lastVisited') super.locals(req, res, callback) @@ -20,4 +20,4 @@ class MyController extends Controller { } } -export default MyController +export default MyPageController diff --git a/src/controllers/datasetController.js b/src/controllers/datasetController.js index 2862c122..d5d83ca3 100644 --- a/src/controllers/datasetController.js +++ b/src/controllers/datasetController.js @@ -1,12 +1,12 @@ 'use strict' -import MyController from './MyController.js' +import MyPageController from './MyPageController.js' // ToDo: we shouldn't hardcode these values here, should we get them from the API // maybe take from specification import { dataSubjects } from '../utils/utils.js' -class DatasetController extends MyController { +class DatasetController extends MyPageController { get (req, res, next) { // const dataset = req.sessionModel.get('data-subject') // const options = datasetOptions[dataset] diff --git a/src/controllers/errorsController.js b/src/controllers/errorsController.js index 241711a7..a6de057b 100644 --- a/src/controllers/errorsController.js +++ b/src/controllers/errorsController.js @@ -1,10 +1,10 @@ 'use strict' -import MyController from './MyController.js' +import MyPageController from './MyPageController.js' import { severityLevels, dataSubjects } from '../utils/utils.js' -class ErrorsController extends MyController { +class ErrorsController extends MyPageController { get (req, res, next) { const validationResult = req.sessionModel.get('validationResult') diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index 1fa9b239..88da8417 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -3,7 +3,7 @@ import multer from 'multer' import axios from 'axios' import { readFile } from 'fs/promises' import { lookup } from 'mime-types' -import MyController from './MyController.js' +import MyPageController from './MyPageController.js' import config from '../../config/index.js' import { severityLevels } from '../utils/utils.js' @@ -13,7 +13,7 @@ const upload = multer({ dest: 'uploads/' }) const apiRoute = config.api.url + config.api.validationEndpoint -class UploadController extends MyController { +class UploadController extends MyPageController { middlewareSetup () { super.middlewareSetup() this.use('/upload', upload.single('datafile')) diff --git a/src/routes/form-wizard/steps.js b/src/routes/form-wizard/steps.js index 8a7a3538..6beb0b31 100644 --- a/src/routes/form-wizard/steps.js +++ b/src/routes/form-wizard/steps.js @@ -1,10 +1,10 @@ -import MyController from '../../controllers/MyController.js' +import MyPageController from '../../controllers/MyPageController.js' import datasetController from '../../controllers/datasetController.js' import uploadController from '../../controllers/uploadController.js' import errorsController from '../../controllers/errorsController.js' const baseSettings = { - controller: MyController, + controller: MyPageController, editable: true, editBackStep: 'check' } From 8afb4d08d1348cc90a31668a8b12e23b320a26ac Mon Sep 17 00:00:00 2001 From: George Goodall Date: Wed, 6 Dec 2023 08:23:19 +0000 Subject: [PATCH 4/9] change page log info --- src/controllers/MyPageController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/MyPageController.js b/src/controllers/MyPageController.js index e7c2f3f5..d68ce0e2 100644 --- a/src/controllers/MyPageController.js +++ b/src/controllers/MyPageController.js @@ -11,7 +11,7 @@ class MyPageController extends Controller { get (req, res) { logger.info({ type: 'PageView', - endpoint: req.originalUrl, + pageRoute: this.options.route, message: `page view occurred for page: ${req.originalUrl}`, sessionId: req.sessionID, ipAddress: req.ip From 18c630896e26323f939b2c78dd808af33e4a4380 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Wed, 6 Dec 2023 09:33:49 +0000 Subject: [PATCH 5/9] make sure to pass next through --- src/controllers/MyPageController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/MyPageController.js b/src/controllers/MyPageController.js index d68ce0e2..24c08583 100644 --- a/src/controllers/MyPageController.js +++ b/src/controllers/MyPageController.js @@ -8,7 +8,7 @@ class MyPageController extends Controller { super.locals(req, res, callback) } - get (req, res) { + get (req, res, next) { logger.info({ type: 'PageView', pageRoute: this.options.route, @@ -16,7 +16,7 @@ class MyPageController extends Controller { sessionId: req.sessionID, ipAddress: req.ip }) - super.get(req, res) + super.get(req, res, next) } } From bf9be404ea98c04bb408150a21bcf7ce22a38879 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Wed, 6 Dec 2023 09:33:57 +0000 Subject: [PATCH 6/9] add test to check page logging --- test/unit/MyController.test.js | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/unit/MyController.test.js diff --git a/test/unit/MyController.test.js b/test/unit/MyController.test.js new file mode 100644 index 00000000..a729734b --- /dev/null +++ b/test/unit/MyController.test.js @@ -0,0 +1,41 @@ +import MyPageController from '../../src/controllers/MyPageController.js' + +import { describe, it, vi, expect } from 'vitest' + +import logger from '../../src/utils/logger.js' + +describe('MyPageController', () => { + const loggerInfoMock = vi.fn() + + vi.mock('../utils/logger.js', () => { + return { + info: loggerInfoMock + } + }) + + const loggerInfoSpy = vi.spyOn(logger, 'info') + + it('Correctly creates a log when the page is viewed', () => { + const req = { + originalUrl: '/dataset', + sessionID: '123', + ip: '1234' + } + const pageController = new MyPageController({ + route: '/dataset' + }) + // pageController.super.get = vi.fn(); + pageController.get(req, {}, vi.fn()) + expect(loggerInfoSpy).toHaveBeenCalledOnce() + + const callArgs = loggerInfoSpy.mock.calls[0][0] + + expect(callArgs.type).toEqual('PageView') + expect(callArgs.pageRoute).toEqual('/dataset') + expect(callArgs.message).toEqual('page view occurred for page: /dataset') + expect(callArgs.sessionId).toEqual('123') + expect(callArgs.ipAddress).toEqual('1234') + expect(callArgs.level).toEqual('info') + expect(callArgs.service).toEqual('lpa-data-validation-frontend') + }) +}) From 9d347d123460b30d00d78aa8692a567e83eeb324 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Wed, 6 Dec 2023 11:33:00 +0000 Subject: [PATCH 7/9] changed myPageController to pageController --- src/controllers/{MyPageController.js => PageController.js} | 4 ++-- src/controllers/datasetController.js | 4 ++-- src/controllers/errorsController.js | 4 ++-- src/controllers/uploadController.js | 4 ++-- src/routes/form-wizard/steps.js | 4 ++-- test/unit/MyController.test.js | 6 +++--- 6 files changed, 13 insertions(+), 13 deletions(-) rename src/controllers/{MyPageController.js => PageController.js} (87%) diff --git a/src/controllers/MyPageController.js b/src/controllers/PageController.js similarity index 87% rename from src/controllers/MyPageController.js rename to src/controllers/PageController.js index 24c08583..82fb3a95 100644 --- a/src/controllers/MyPageController.js +++ b/src/controllers/PageController.js @@ -2,7 +2,7 @@ import hmpoFormWizard from 'hmpo-form-wizard' import logger from '../utils/logger.js' const { Controller } = hmpoFormWizard -class MyPageController extends Controller { +class PageController extends Controller { locals (req, res, callback) { req.form.options.lastPage = req.journeyModel.get('lastVisited') super.locals(req, res, callback) @@ -20,4 +20,4 @@ class MyPageController extends Controller { } } -export default MyPageController +export default PageController diff --git a/src/controllers/datasetController.js b/src/controllers/datasetController.js index d5d83ca3..e419df64 100644 --- a/src/controllers/datasetController.js +++ b/src/controllers/datasetController.js @@ -1,12 +1,12 @@ 'use strict' -import MyPageController from './MyPageController.js' +import PageController from './PageController.js' // ToDo: we shouldn't hardcode these values here, should we get them from the API // maybe take from specification import { dataSubjects } from '../utils/utils.js' -class DatasetController extends MyPageController { +class DatasetController extends PageController { get (req, res, next) { // const dataset = req.sessionModel.get('data-subject') // const options = datasetOptions[dataset] diff --git a/src/controllers/errorsController.js b/src/controllers/errorsController.js index a6de057b..982b329a 100644 --- a/src/controllers/errorsController.js +++ b/src/controllers/errorsController.js @@ -1,10 +1,10 @@ 'use strict' -import MyPageController from './MyPageController.js' +import PageController from './PageController.js' import { severityLevels, dataSubjects } from '../utils/utils.js' -class ErrorsController extends MyPageController { +class ErrorsController extends PageController { get (req, res, next) { const validationResult = req.sessionModel.get('validationResult') diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index 88da8417..49e97c1f 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -3,7 +3,7 @@ import multer from 'multer' import axios from 'axios' import { readFile } from 'fs/promises' import { lookup } from 'mime-types' -import MyPageController from './MyPageController.js' +import PageController from './PageController.js' import config from '../../config/index.js' import { severityLevels } from '../utils/utils.js' @@ -13,7 +13,7 @@ const upload = multer({ dest: 'uploads/' }) const apiRoute = config.api.url + config.api.validationEndpoint -class UploadController extends MyPageController { +class UploadController extends PageController { middlewareSetup () { super.middlewareSetup() this.use('/upload', upload.single('datafile')) diff --git a/src/routes/form-wizard/steps.js b/src/routes/form-wizard/steps.js index 6beb0b31..211b69fc 100644 --- a/src/routes/form-wizard/steps.js +++ b/src/routes/form-wizard/steps.js @@ -1,10 +1,10 @@ -import MyPageController from '../../controllers/MyPageController.js' +import PageController from '../../controllers/PageController.js' import datasetController from '../../controllers/datasetController.js' import uploadController from '../../controllers/uploadController.js' import errorsController from '../../controllers/errorsController.js' const baseSettings = { - controller: MyPageController, + controller: PageController, editable: true, editBackStep: 'check' } diff --git a/test/unit/MyController.test.js b/test/unit/MyController.test.js index a729734b..cce0e233 100644 --- a/test/unit/MyController.test.js +++ b/test/unit/MyController.test.js @@ -1,10 +1,10 @@ -import MyPageController from '../../src/controllers/MyPageController.js' +import PageController from '../../src/controllers/PageController.js' import { describe, it, vi, expect } from 'vitest' import logger from '../../src/utils/logger.js' -describe('MyPageController', () => { +describe('PageController', () => { const loggerInfoMock = vi.fn() vi.mock('../utils/logger.js', () => { @@ -21,7 +21,7 @@ describe('MyPageController', () => { sessionID: '123', ip: '1234' } - const pageController = new MyPageController({ + const pageController = new PageController({ route: '/dataset' }) // pageController.super.get = vi.fn(); From 0f05190aa844b82c303ee9e3b602fdbfb96eda89 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Wed, 6 Dec 2023 11:34:29 +0000 Subject: [PATCH 8/9] updated pagecontroller file name to be lowercase --- src/controllers/datasetController.js | 2 +- src/controllers/errorsController.js | 2 +- src/controllers/{PageController.js => pageController.js} | 0 src/controllers/uploadController.js | 2 +- test/unit/MyController.test.js | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename src/controllers/{PageController.js => pageController.js} (100%) diff --git a/src/controllers/datasetController.js b/src/controllers/datasetController.js index e419df64..3b8c9bcd 100644 --- a/src/controllers/datasetController.js +++ b/src/controllers/datasetController.js @@ -1,6 +1,6 @@ 'use strict' -import PageController from './PageController.js' +import PageController from './pageController.js' // ToDo: we shouldn't hardcode these values here, should we get them from the API // maybe take from specification diff --git a/src/controllers/errorsController.js b/src/controllers/errorsController.js index 982b329a..4859b987 100644 --- a/src/controllers/errorsController.js +++ b/src/controllers/errorsController.js @@ -1,6 +1,6 @@ 'use strict' -import PageController from './PageController.js' +import PageController from './pageController.js' import { severityLevels, dataSubjects } from '../utils/utils.js' diff --git a/src/controllers/PageController.js b/src/controllers/pageController.js similarity index 100% rename from src/controllers/PageController.js rename to src/controllers/pageController.js diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index 49e97c1f..4bf78fec 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -3,7 +3,7 @@ import multer from 'multer' import axios from 'axios' import { readFile } from 'fs/promises' import { lookup } from 'mime-types' -import PageController from './PageController.js' +import PageController from './pageController.js' import config from '../../config/index.js' import { severityLevels } from '../utils/utils.js' diff --git a/test/unit/MyController.test.js b/test/unit/MyController.test.js index cce0e233..b8c47809 100644 --- a/test/unit/MyController.test.js +++ b/test/unit/MyController.test.js @@ -1,4 +1,4 @@ -import PageController from '../../src/controllers/PageController.js' +import PageController from '../../src/controllers/pageController.js' import { describe, it, vi, expect } from 'vitest' From b4af6a87c5719eaf739102e347c67c2b76e2b668 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Wed, 6 Dec 2023 11:47:35 +0000 Subject: [PATCH 9/9] fix tests --- src/routes/form-wizard/steps.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/form-wizard/steps.js b/src/routes/form-wizard/steps.js index 211b69fc..380a3fef 100644 --- a/src/routes/form-wizard/steps.js +++ b/src/routes/form-wizard/steps.js @@ -1,4 +1,4 @@ -import PageController from '../../controllers/PageController.js' +import PageController from '../../controllers/pageController.js' import datasetController from '../../controllers/datasetController.js' import uploadController from '../../controllers/uploadController.js' import errorsController from '../../controllers/errorsController.js'