2021-07-21 16:28:39 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
|
|
|
|
2021-07-21 18:25:41 +03:00
|
|
|
const fs = require('fs-extra');
|
2021-07-21 16:28:39 +03:00
|
|
|
const path = require('path');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const MembersCSVImporter = require('..');
|
|
|
|
|
|
|
|
const csvPath = path.join(__dirname, '/fixtures/');
|
|
|
|
|
|
|
|
describe('Importer', function () {
|
2021-07-21 18:25:41 +03:00
|
|
|
let fsWriteSpy;
|
2022-10-13 09:21:08 +03:00
|
|
|
let memberCreateStub;
|
|
|
|
let knexStub;
|
|
|
|
let sendEmailStub;
|
2021-07-21 18:25:41 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
fsWriteSpy = sinon.spy(fs, 'writeFile');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2022-08-23 22:19:30 +03:00
|
|
|
const writtenFile = fsWriteSpy.args?.[0]?.[0];
|
2021-07-21 18:25:41 +03:00
|
|
|
|
|
|
|
if (writtenFile) {
|
|
|
|
fs.removeSync(writtenFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2022-10-13 09:21:08 +03:00
|
|
|
// @NOTE: this is way too much mocking! the MembersCSVImporter constructor API should be simplified
|
|
|
|
const buildMockImporterInstance = () => {
|
|
|
|
const defaultProduct = {
|
|
|
|
id: 'default_product_id'
|
|
|
|
};
|
|
|
|
|
|
|
|
memberCreateStub = sinon.stub().resolves(null);
|
|
|
|
const membersApi = {
|
|
|
|
productRepository: {
|
|
|
|
list: async () => {
|
|
|
|
return {
|
|
|
|
data: [defaultProduct]
|
|
|
|
};
|
2021-07-21 16:28:39 +03:00
|
|
|
}
|
2022-10-13 09:21:08 +03:00
|
|
|
},
|
|
|
|
members: {
|
|
|
|
get: async () => {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
create: memberCreateStub
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
knexStub = {
|
|
|
|
transaction: sinon.stub().resolves({
|
|
|
|
rollback: () => {},
|
|
|
|
commit: () => {}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
sendEmailStub = sinon.stub();
|
|
|
|
|
|
|
|
return new MembersCSVImporter({
|
|
|
|
storagePath: csvPath,
|
|
|
|
getTimezone: sinon.stub().returns('UTC'),
|
|
|
|
getMembersApi: () => membersApi,
|
|
|
|
sendEmail: sendEmailStub,
|
|
|
|
isSet: sinon.stub(),
|
|
|
|
addJob: sinon.stub(),
|
|
|
|
knex: knexStub,
|
|
|
|
urlFor: sinon.stub(),
|
|
|
|
context: {importer: true}
|
|
|
|
});
|
|
|
|
};
|
2021-07-21 16:28:39 +03:00
|
|
|
|
2022-10-13 09:21:08 +03:00
|
|
|
describe('process', function () {
|
|
|
|
it('should import a CSV file', async function () {
|
2021-07-21 16:28:39 +03:00
|
|
|
const LabelModelStub = {
|
|
|
|
findOne: sinon.stub().resolves(null)
|
|
|
|
};
|
|
|
|
|
2022-10-13 09:21:08 +03:00
|
|
|
const importer = buildMockImporterInstance();
|
2021-07-23 19:29:19 +03:00
|
|
|
|
|
|
|
const result = await importer.process({
|
|
|
|
pathToCSV: `${csvPath}/single-column-with-header.csv`,
|
|
|
|
headerMapping: {},
|
|
|
|
importLabel: {
|
|
|
|
name: 'test import'
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
email: 'test@example.com'
|
|
|
|
},
|
|
|
|
LabelModel: LabelModelStub
|
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result.meta);
|
|
|
|
should.exist(result.meta.stats);
|
|
|
|
should.exist(result.meta.stats.imported);
|
|
|
|
result.meta.stats.imported.should.equal(2);
|
|
|
|
|
|
|
|
should.exist(result.meta.stats.invalid);
|
|
|
|
should.equal(result.meta.import_label, null);
|
|
|
|
|
2021-07-28 18:13:48 +03:00
|
|
|
should.exist(result.meta.originalImportSize);
|
|
|
|
result.meta.originalImportSize.should.equal(2);
|
|
|
|
|
2021-07-21 18:25:41 +03:00
|
|
|
fsWriteSpy.calledOnce.should.be.true();
|
2022-04-13 19:35:24 +03:00
|
|
|
|
|
|
|
// Called at least once
|
|
|
|
memberCreateStub.notCalled.should.be.false();
|
|
|
|
memberCreateStub.firstCall.lastArg.context.import.should.be.true();
|
2021-07-21 16:28:39 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-23 22:19:30 +03:00
|
|
|
describe('sendErrorEmail', function () {
|
|
|
|
it('should send email with errors for invalid CSV file', async function () {
|
2022-10-13 09:21:08 +03:00
|
|
|
const importer = buildMockImporterInstance();
|
2022-08-23 22:19:30 +03:00
|
|
|
|
|
|
|
await importer.sendErrorEmail({
|
|
|
|
emailRecipient: 'test@example.com',
|
|
|
|
emailSubject: 'Your member import was unsuccessful',
|
|
|
|
emailContent: 'Import was unsuccessful',
|
|
|
|
errorCSV: 'id,email,invalid email',
|
|
|
|
importLabel: {name: 'Test import'}
|
|
|
|
});
|
|
|
|
|
|
|
|
sendEmailStub.calledWith({
|
|
|
|
to: 'test@example.com',
|
|
|
|
subject: 'Your member import was unsuccessful',
|
|
|
|
html: 'Import was unsuccessful',
|
|
|
|
forceTextContent: true,
|
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
filename: 'Test import - Errors.csv',
|
|
|
|
content: 'id,email,invalid email',
|
|
|
|
contentType: 'text/csv',
|
|
|
|
contentDisposition: 'attachment'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}).should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-21 16:28:39 +03:00
|
|
|
describe('prepare', function () {
|
|
|
|
it('processes a basic valid import file for members', async function () {
|
2022-10-13 09:21:08 +03:00
|
|
|
const membersImporter = buildMockImporterInstance();
|
2021-07-21 16:28:39 +03:00
|
|
|
|
|
|
|
const result = await membersImporter.prepare(`${csvPath}/single-column-with-header.csv`);
|
|
|
|
|
|
|
|
should.exist(result.id);
|
|
|
|
result.id.should.match(/\/members-importer\/test\/fixtures\/Members Import/);
|
|
|
|
|
|
|
|
result.batches.should.equal(2);
|
|
|
|
should.exist(result.metadata);
|
2021-07-21 18:25:41 +03:00
|
|
|
|
|
|
|
fsWriteSpy.calledOnce.should.be.true();
|
2021-07-21 16:28:39 +03:00
|
|
|
});
|
2021-11-04 20:48:39 +03:00
|
|
|
|
|
|
|
it('Does not include columns not in the original CSV or mapped', async function () {
|
2022-10-13 09:21:08 +03:00
|
|
|
const membersImporter = buildMockImporterInstance();
|
2021-11-04 20:48:39 +03:00
|
|
|
|
|
|
|
await membersImporter.prepare(`${csvPath}/single-column-with-header.csv`);
|
|
|
|
|
|
|
|
const fileContents = fsWriteSpy.firstCall.args[1];
|
|
|
|
|
|
|
|
fileContents.should.match(/^email,labels\r\n/);
|
|
|
|
});
|
2021-07-21 16:28:39 +03:00
|
|
|
});
|
|
|
|
});
|