Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into noErrorsPages
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Nov 17, 2023
2 parents 0845cb4 + e1ed226 commit 415f2c5
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 72 deletions.
4 changes: 2 additions & 2 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const HmpoConfig = require('hmpo-config')
import HmpoConfig from 'hmpo-config'

const config = new HmpoConfig()
config.addFile('./config/default.yaml')
module.exports = config.toJSON()
export default config.toJSON()
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict'

const hmpoLogger = require('hmpo-logger')
const express = require('express')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const nunjucks = require('nunjucks')
const path = require('path')
const bodyParser = require('body-parser')
const config = require('./config')
const { govukMarkdown } = require('@x-govuk/govuk-prototype-filters')
import hmpoLogger from 'hmpo-logger'
import express from 'express'
import cookieParser from 'cookie-parser'
import session from 'express-session'
import nunjucks from 'nunjucks'
import bodyParser from 'body-parser'
import config from './config/index.js'
import xGovFilters from '@x-govuk/govuk-prototype-filters'
import formWizard from './src/routes/form-wizard/index.js'

const { govukMarkdown } = xGovFilters

const logger = hmpoLogger.config(config.logs).get()

Expand All @@ -18,7 +20,7 @@ const app = express()
app.use(hmpoLogger.middleware())

// add routing for static assets
app.use('/public', express.static(path.resolve(__dirname, 'public')))
app.use('/public', express.static('./public'))

// cookies and sessions (redis or elasticache should be used in a prod env)
app.use(cookieParser())
Expand Down Expand Up @@ -54,7 +56,7 @@ nunjucksEnv.addFilter('govukMarkdown', govukMarkdown)
// body parser
app.use(bodyParser.urlencoded({ extended: true }))

app.use('/', require('./src/routes/form-wizard'))
app.use('/', formWizard)

// file not found handler
app.use((req, res, next) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"prepare": "husky install",
"start": "node index.js",
Expand Down
4 changes: 2 additions & 2 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
const { defineConfig, devices } = require('@playwright/test')
import { defineConfig, devices } from '@playwright/test'

/**
* Read environment variables from file.
Expand All @@ -10,7 +10,7 @@ const { defineConfig, devices } = require('@playwright/test')
/**
* @see https://playwright.dev/docs/test-configuration
*/
module.exports = defineConfig({
export default defineConfig({
testDir: './test/acceptance',
/* Run tests in files in parallel */
fullyParallel: true,
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/MyController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Controller } = require('hmpo-form-wizard')
import hmpoFormWizard from 'hmpo-form-wizard'
const { Controller } = hmpoFormWizard

class MyController extends Controller {
locals (req, res, callback) {
Expand All @@ -7,4 +8,4 @@ class MyController extends Controller {
}
}

module.exports = MyController
export default MyController
4 changes: 2 additions & 2 deletions src/controllers/dataSubjectController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const MyController = require('./MyController.js')
import MyController from './MyController.js'

class DataSubjectController extends MyController {

}

module.exports = DataSubjectController
export default DataSubjectController
4 changes: 2 additions & 2 deletions src/controllers/datasetController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const MyController = require('./MyController')
import MyController from './MyController.js'

// ToDo: we shouldn't hardcode these values here, should we get them from the API
// maybe take from specification
Expand Down Expand Up @@ -58,4 +58,4 @@ class DatasetController extends MyController {
}
}

module.exports = DatasetController
export default DatasetController
4 changes: 2 additions & 2 deletions src/controllers/errorsController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const MyController = require('./MyController')
import MyController from './MyController.js'

class ErrorsController extends MyController {
get (req, res, next) {
Expand Down Expand Up @@ -76,4 +76,4 @@ class ErrorsController extends MyController {
}
}

module.exports = ErrorsController
export default ErrorsController
24 changes: 12 additions & 12 deletions src/controllers/uploadController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'
import multer from 'multer';
import axios from 'axios';
import { readFile } from 'fs/promises';
import { lookup } from 'mime-types';
import MyController from './MyController.js';
import config from '../../config';
import multer from 'multer'
import axios from 'axios'
import { readFile } from 'fs/promises'
import { lookup } from 'mime-types'
import MyController from './MyController.js'
import config from '../../config/index.js'

const upload = multer({ dest: 'uploads/' });
const upload = multer({ dest: 'uploads/' })

const apiRoute = config.api.url + config.api.validationEndpoint

Expand All @@ -24,7 +24,7 @@ class UploadController extends MyController {
fileName: req.file.originalname,
dataset: req.sessionModel.get('dataset'),
dataSubject: req.sessionModel.get('data-subject'),
organization: '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?
})
this.errorCount = jsonResult['issue-log'].length
req.body.validationResult = jsonResult
Expand All @@ -35,24 +35,24 @@ class UploadController extends MyController {
super.post(req, res, next)
}

async validateFile ({ filePath, fileName, dataset, dataSubject, organization }) {
async validateFile ({ filePath, fileName, dataset, dataSubject, organisation }) {
const formData = new FormData()
formData.append('dataset', dataset)
formData.append('collection', dataSubject)
import { organization } from 'organization'
formData.append('organisation', organisation)

const file = new Blob([await readFile(filePath)], { type: lookup(filePath) })

formData.append('upload_file', file, fileName)

const result = await axios.post(apiRoute, formData)

return JSON.parse(result.data)
return result.data
}

hasErrors () {
return this.errorCount > 0
}
}

module.exports = UploadController
export default UploadController
2 changes: 1 addition & 1 deletion src/routes/form-wizard/fields.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
'data-subject': {
validate: 'required'
},
Expand Down
10 changes: 5 additions & 5 deletions src/routes/form-wizard/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { Router } = require('express')
const wizard = require('hmpo-form-wizard')
const steps = require('./steps')
const fields = require('./fields')
import { Router } from 'express'
import wizard from 'hmpo-form-wizard'
import steps from './steps.js'
import fields from './fields.js'

const app = Router()

app.use(wizard(steps, fields, { name: 'my-wizard', csrf: false }))

module.exports = app
export default app
18 changes: 12 additions & 6 deletions src/routes/form-wizard/steps.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
module.exports = {
import dataSubjectController from '../../controllers/dataSubjectController.js'
import datasetController from '../../controllers/datasetController.js'
import uploadController from '../../controllers/uploadController.js'
import errorsController from '../../controllers/errorsController.js'
import MyController from '../../controllers/MyController.js'

export default {
'/': {
entryPoint: true,
resetJourney: true,
next: 'data-subject',
template: '../views/start.html'
},
'/data-subject': {
controller: require('../../controllers/dataSubjectController'),
controller: dataSubjectController,
fields: ['data-subject'],
next: 'dataset'
},
'/dataset': {
controller: require('../../controllers/datasetController'),
controller: datasetController,
fields: ['dataset'],
next: 'upload'
},
'/upload': {
controller: require('../../controllers/uploadController'),
controller: uploadController,
fields: ['validationResult'],
next: [
{ fn: 'hasErrors', next: 'errors' },
'no-errors'
]
},
'/errors': {
controller: require('../../controllers/errorsController'),
controller: errorsController,
next: 'no-errors'
},
'/no-errors': {
controller: require('../../controllers/MyController'),
controller: MyController,
next: 'transformations'
}
}
6 changes: 3 additions & 3 deletions test/mock-api/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// a basic json express server with one endpoint that returns a json object

const express = require('express')
import express from 'express'

const app = express()
import config from '../../config/index.js'

const config = require('../../config')
const app = express()

app.post(config.api.validationEndpoint, (req, res) => {
const filename = req.file.originalname
Expand Down
46 changes: 24 additions & 22 deletions test/unit/uploadController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import mockApiValue from '../testData/API_RUN_PIPELINE_RESPONSE.json'

import UploadController from '../../src/controllers/uploadController.js'

// import proxyquire from 'proxyquire'

let uploadController
const validateFileMock = vi.fn().mockReturnValue(mockApiValue)
describe('UploadController', () => {
let uploadController
const validateFileMock = vi.fn().mockReturnValue(mockApiValue)

beforeEach(() => {
const options = {
route: '/upload'
}
uploadController = new UploadController(options)
})
beforeEach(() => {
const options = {
route: '/upload'
}
uploadController = new UploadController(options)
})

describe('UploadController', () => {
it('post adds the validation result to the session and the error count to the controller', async () => {
expect(uploadController.post).toBeDefined()

Expand Down Expand Up @@ -45,16 +43,16 @@ describe('UploadController', () => {
expect(uploadController.errorCount).toEqual(mockApiValue['issue-log'].length)
})

// skipping this for now and will come back to it in a later ticket as I can't get the stubbing to work
it.skip('validateFile returns the result of the API call', async () => {
/*
need to stub
- readfile
- lookup
- axios.post
*/

// const uploadControllerRequireStubs = proxyquire('../../src/controllers/uploadController', {})
it('validateFile correctly calls the API', async () => {
vi.mock('axios', async () => {
const actualAxios = vi.importActual('axios')
return {
default: {
...actualAxios.default,
post: vi.fn().mockResolvedValue({ data: { test: 'test' } })
}
}
})

expect(uploadController.validateFile).toBeDefined()

Expand All @@ -66,6 +64,10 @@ describe('UploadController', () => {
organization: 'local-authority-eng:CAT'
}

await uploadController.validateFile(params)
const result = await uploadController.validateFile(params)

expect(result).toEqual({ test: 'test' })

// expect().toHaveBeenCalledWith(config.api.url + config.api.validationEndpoint, expect.any(FormData))
})
})

0 comments on commit 415f2c5

Please sign in to comment.