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;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
fsWriteSpy = sinon.spy(fs, 'writeFile');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
const writtenFile = fsWriteSpy.args[0][0];
|
|
|
|
|
|
|
|
if (writtenFile) {
|
|
|
|
fs.removeSync(writtenFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2021-07-21 16:28:39 +03:00
|
|
|
describe('process', function () {
|
|
|
|
it('should import a CSV file', async function () {
|
|
|
|
const defaultProduct = {
|
|
|
|
id: 'default_product_id'
|
|
|
|
};
|
|
|
|
|
2022-04-13 19:35:24 +03:00
|
|
|
const memberCreateStub = sinon.stub().resolves(null);
|
2021-07-21 16:28:39 +03:00
|
|
|
const membersApi = {
|
|
|
|
productRepository: {
|
|
|
|
list: async () => {
|
|
|
|
return {
|
|
|
|
data: [defaultProduct]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
members: {
|
|
|
|
get: async () => {
|
|
|
|
return null;
|
|
|
|
},
|
2022-04-13 19:35:24 +03:00
|
|
|
create: memberCreateStub
|
2021-07-21 16:28:39 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const knexStub = {
|
|
|
|
transaction: sinon.stub().resolves({
|
|
|
|
rollback: () => {},
|
|
|
|
commit: () => {}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const LabelModelStub = {
|
|
|
|
findOne: sinon.stub().resolves(null)
|
|
|
|
};
|
|
|
|
|
|
|
|
const importer = new MembersCSVImporter({
|
|
|
|
storagePath: csvPath,
|
|
|
|
getTimezone: sinon.stub().returns('UTC'),
|
|
|
|
getMembersApi: () => membersApi,
|
|
|
|
sendEmail: sinon.stub(),
|
2021-08-20 16:27:56 +03:00
|
|
|
isSet: sinon.stub(),
|
2021-07-21 16:28:39 +03:00
|
|
|
addJob: sinon.stub(),
|
|
|
|
knex: knexStub,
|
2022-04-06 22:12:38 +03:00
|
|
|
urlFor: sinon.stub(),
|
|
|
|
context: {importer: true}
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('prepare', function () {
|
|
|
|
it('processes a basic valid import file for members', async function () {
|
|
|
|
const membersImporter = new MembersCSVImporter({
|
|
|
|
storagePath: csvPath,
|
|
|
|
getTimezone: sinon.stub().returns('UTC'),
|
|
|
|
getMembersApi: sinon.stub(),
|
|
|
|
sendEmail: sinon.stub(),
|
|
|
|
isSet: sinon.stub(),
|
|
|
|
addJob: sinon.stub(),
|
|
|
|
knex: sinon.stub(),
|
2022-04-06 22:12:38 +03:00
|
|
|
urlFor: sinon.stub(),
|
|
|
|
context: {importer: true}
|
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 () {
|
|
|
|
const membersImporter = new MembersCSVImporter({
|
|
|
|
storagePath: csvPath,
|
|
|
|
getTimezone: sinon.stub().returns('UTC'),
|
|
|
|
getMembersApi: sinon.stub(),
|
|
|
|
sendEmail: sinon.stub(),
|
|
|
|
isSet: sinon.stub(),
|
|
|
|
addJob: sinon.stub(),
|
|
|
|
knex: sinon.stub(),
|
2022-04-06 22:12:38 +03:00
|
|
|
urlFor: sinon.stub(),
|
|
|
|
context: {importer: true}
|
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
|
|
|
});
|
|
|
|
});
|