From 1c746c0ca0c10e6717d6e2b6d3ca9b18dd832261 Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 13 Oct 2022 12:10:44 +0800 Subject: [PATCH] Added test coverage for 'subscribed' transform refs https://github.com/TryGhost/Toolbox/issues/430 - Not having any extra logic in the mapper will allow to have a generalized "mapping" concept for CSV input serialization - This is groundwork for stricter header value filtering on the parsing stage --- ghost/members-csv/lib/parse.js | 12 +++++++----- .../test/fixtures/subscribed-to-emails-header.csv | 3 +++ ghost/members-csv/test/parse.test.js | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 ghost/members-csv/test/fixtures/subscribed-to-emails-header.csv diff --git a/ghost/members-csv/lib/parse.js b/ghost/members-csv/lib/parse.js index bde957e736..3df59412c2 100644 --- a/ghost/members-csv/lib/parse.js +++ b/ghost/members-csv/lib/parse.js @@ -4,18 +4,20 @@ const papaparse = require('papaparse'); const fs = require('fs-extra'); module.exports = (path, mapping, defaultLabels = []) => { + const inputMapping = Object.assign({}, mapping, { + subscribed_to_emails: 'subscribed' + }); + return new Promise(function (resolve, reject) { const csvFileStream = fs.createReadStream(path); const csvParserStream = papaparse.parse(papaparse.NODE_STREAM_INPUT, { header: true, transformHeader(_header) { let header = _header; - if (mapping && Reflect.has(mapping, _header)) { - header = mapping[_header]; - } - if (header === 'subscribed_to_emails') { - return 'subscribed'; + if (inputMapping && Reflect.has(inputMapping, _header)) { + header = inputMapping[_header]; } + return header; }, transform(value, header) { diff --git a/ghost/members-csv/test/fixtures/subscribed-to-emails-header.csv b/ghost/members-csv/test/fixtures/subscribed-to-emails-header.csv new file mode 100644 index 0000000000..d3dc77bb65 --- /dev/null +++ b/ghost/members-csv/test/fixtures/subscribed-to-emails-header.csv @@ -0,0 +1,3 @@ +email,subscribed_to_emails +jbloggs@example.com,true +test@example.com,false diff --git a/ghost/members-csv/test/parse.test.js b/ghost/members-csv/test/parse.test.js index 408483eba7..a6464e0cf9 100644 --- a/ghost/members-csv/test/parse.test.js +++ b/ghost/members-csv/test/parse.test.js @@ -1,5 +1,6 @@ const should = require('should'); const path = require('path'); +const assert = require('assert'); const {parse} = require('../index'); const csvPath = path.join(__dirname, '/fixtures/'); @@ -122,4 +123,18 @@ describe('parse', function () { result[1].email.should.eql('test@example.com'); should.equal(result[1].name, null); }); + + it('read csv: transforms "subscribed_to_emails" column to "subscribed" property', async function () { + const result = await readCSV({ + filePath: csvPath + 'subscribed-to-emails-header.csv' + }); + + assert.ok(result); + assert.equal(result.length, 2); + assert.equal(result[0].email, 'jbloggs@example.com'); + assert.ok(result[0].subscribed); + + assert.equal(result[1].email, 'test@example.com'); + assert.equal(result[1].subscribed, false); + }); });