From ac4e6a86fd99597aad17aa2605fa70460a714324 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Mon, 6 Nov 2023 10:36:31 +0000 Subject: [PATCH 1/2] update tests to reflect removed submit page --- test/acceptance/pages_load_ok.test.js | 4 ---- test/acceptance/upload_data.test.js | 13 +------------ 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/test/acceptance/pages_load_ok.test.js b/test/acceptance/pages_load_ok.test.js index 103339ea..88036f05 100644 --- a/test/acceptance/pages_load_ok.test.js +++ b/test/acceptance/pages_load_ok.test.js @@ -28,8 +28,4 @@ test.describe('without a valid session, the user can not access the later pages' test('/upload', async ({ page }) => { await checkSessionExpired(page, '/upload') }) - - test('/submit', async ({ page }) => { - await checkSessionExpired(page, '/submit') - }) }) diff --git a/test/acceptance/upload_data.test.js b/test/acceptance/upload_data.test.js index dd6395aa..f3b8c8b1 100644 --- a/test/acceptance/upload_data.test.js +++ b/test/acceptance/upload_data.test.js @@ -1,4 +1,4 @@ -import { test, expect } from '@playwright/test' +import { test } from '@playwright/test' test('Upload data', async ({ page }) => { await page.goto('/') @@ -21,15 +21,4 @@ test('Upload data', async ({ page }) => { const fileChooser = await fileChooserPromise await fileChooser.setFiles('test/testData/conservation-area.csv') await page.getByRole('button', { name: 'Continue' }).click() - - await page.waitForURL('**/submit') - - const ListedDataSubject = await page.getByText('data-subject: Conservation area') - await expect(ListedDataSubject !== undefined).toBeTruthy() - - const ListedDataset = await page.getByText('dataset: Conservation area dataset') - await expect(ListedDataset !== undefined).toBeTruthy() - - const ListedDatafile = await page.getByText('datafile: conservation-area.csv') - await expect(ListedDatafile !== undefined).toBeTruthy() }) From 24c31fb5426ab31368bb7931ffa2a248e99b6e7c Mon Sep 17 00:00:00 2001 From: George Goodall Date: Mon, 6 Nov 2023 10:37:47 +0000 Subject: [PATCH 2/2] removed submit page and added functionality to end of upload step --- src/controllers/submitForm.js | 41 ----------------------------- src/controllers/uploadController.js | 35 +++++++++++++++++++++--- src/routes/form-wizard/steps.js | 4 --- 3 files changed, 32 insertions(+), 48 deletions(-) delete mode 100644 src/controllers/submitForm.js diff --git a/src/controllers/submitForm.js b/src/controllers/submitForm.js deleted file mode 100644 index 49ca60e5..00000000 --- a/src/controllers/submitForm.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict' - -const { Controller } = require('hmpo-form-wizard') - -const { readFile } = require('node:fs/promises') -const { lookup } = require('mime-types') - -const apiRoute = 'http://127.0.0.1:8082/api/dataset/validate/file/request/' - -class SubmitForm extends Controller { - async post (req, res, next) { - const formData = new FormData() - formData.append('dataset', req.sessionModel.get('dataset')) - formData.append('collection', req.sessionModel.get('data-subject')) - formData.append('organization', 'mockOrg') - - const filePath = req.sessionModel.get('datafile').path - const file = new Blob([await readFile(filePath)], { type: lookup(filePath) }) - - formData.append('upload_file', file, req.sessionModel.get('datafile').originalname) - - try { - // post the data to the api - const result = await fetch(apiRoute, { - method: 'POST', - body: formData - }) - - const json = await result.json() - - console.log(json) - - // send the response back to the user - res.send(json) - } catch (e) { - res.send(e) - } - } -} - -module.exports = SubmitForm diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index 71d69b33..01c57bc9 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -4,15 +4,44 @@ const upload = multer({ dest: 'uploads/' }) const { Controller } = require('hmpo-form-wizard') +const { readFile } = require('node:fs/promises') +const { lookup } = require('mime-types') + +const apiRoute = 'http://127.0.0.1:8082/api/dataset/validate/file/request/' + class UploadController extends Controller { middlewareSetup () { super.middlewareSetup() this.use('/upload', upload.single('datafile')) } - post (req, res, next) { - req.body.datafile = req.file - super.post(req, res, next) + async post (req, res, next) { + const formData = new FormData() + formData.append('dataset', req.sessionModel.get('dataset')) + formData.append('collection', req.sessionModel.get('data-subject')) + formData.append('organization', 'mockOrg') + + const filePath = req.file.path + const file = new Blob([await readFile(filePath)], { type: lookup(filePath) }) + + formData.append('upload_file', file, req.file.originalname) + + try { + // post the data to the api + const result = await fetch(apiRoute, { + method: 'POST', + body: formData + }) + + const json = await result.json() + + console.log(json) + + // send the response back to the user + res.send(json) + } catch (e) { + res.send(e) + } } } diff --git a/src/routes/form-wizard/steps.js b/src/routes/form-wizard/steps.js index 0c867e2e..0a77de9a 100644 --- a/src/routes/form-wizard/steps.js +++ b/src/routes/form-wizard/steps.js @@ -17,10 +17,6 @@ module.exports = { '/upload': { controller: require('../../controllers/uploadController'), fields: ['datafile', 'path'], - next: 'submit' - }, - '/submit': { - controller: require('../../controllers/submitForm'), next: 'done' } }