Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

167 - Upload of branding and upload of partners #340

Merged
merged 14 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 8
"ecmaVersion": 9
latin-panda marked this conversation as resolved.
Show resolved Hide resolved
},
"rules": {
"eqeqeq": "error",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ In order to avoid overwriting someone elses configuration medic-conf records the
* upload app settings to server
* upload resources to server
* upload custom translations to the server
* upload branding to server
* upload partners to server

## Forms

Expand Down
12 changes: 12 additions & 0 deletions src/fn/upload-branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const environment = require('../lib/environment');
const uploadConfigurationDocs = require('../lib/upload-configuration-docs');

module.exports = {
requiresInstance: true,
execute: () => {
const configurationPath = `${environment.pathToProject}/branding.json`;
const directoryPath = `${environment.pathToProject}/branding`;

return uploadConfigurationDocs(configurationPath, directoryPath, 'branding');
}
};
12 changes: 12 additions & 0 deletions src/fn/upload-partners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const environment = require('../lib/environment');
const uploadConfigurationDocs = require('../lib/upload-configuration-docs');

module.exports = {
requiresInstance: true,
execute: () => {
const configurationPath = `${environment.pathToProject}/partners.json`;
const directoryPath = `${environment.pathToProject}/partners`;

return uploadConfigurationDocs(configurationPath, directoryPath, 'partners');
}
};
latin-panda marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 5 additions & 32 deletions src/fn/upload-resources.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
const attachmentsFromDir = require('../lib/attachments-from-dir');
const environment = require('../lib/environment');
const fs = require('../lib/sync-fs');
const pouch = require('../lib/db');
const { info, warn } = require('../lib/log');
const insertOrReplace = require('../lib/insert-or-replace');
const warnUploadOverwrite = require('../lib/warn-upload-overwrite');
const uploadConfigurationDocs = require('../lib/upload-configuration-docs');

module.exports = {
requiresInstance: true,
execute: async () => {
const resourcesPath = fs.path.resolve(`${environment.pathToProject}/resources.json`);
execute: () => {
const configurationPath = `${environment.pathToProject}/resources.json`;
const directoryPath = `${environment.pathToProject}/resources`;

if(!fs.exists(resourcesPath)) {
warn(`No resources file found at path: ${resourcesPath}`);
return Promise.resolve();
}

const doc = {
_id: 'resources',
resources: fs.readJson(resourcesPath),
_attachments: attachmentsFromDir(`${environment.pathToProject}/resources`),
};

const db = pouch();

const changes = await warnUploadOverwrite.preUploadDoc(db, doc);
if (changes) {
await insertOrReplace(db, doc);
info('Resources file uploaded');
} else {
info('Resources file not uploaded as no changes found');
}

warnUploadOverwrite.postUploadDoc(doc);

return Promise.resolve();
return uploadConfigurationDocs(configurationPath, directoryPath, 'resources');
}
};
4 changes: 4 additions & 0 deletions src/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const defaultActions = [
'upload-collect-forms',
'upload-contact-forms',
'upload-resources',
'upload-branding',
'upload-partners',
'upload-custom-translations',
];
const defaultArchiveActions = [
Expand All @@ -37,6 +39,8 @@ const defaultArchiveActions = [
'upload-collect-forms',
'upload-contact-forms',
'upload-resources',
'upload-branding',
latin-panda marked this conversation as resolved.
Show resolved Hide resolved
'upload-partners',
'upload-custom-translations',
];

Expand Down
43 changes: 43 additions & 0 deletions src/lib/upload-configuration-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const attachmentsFromDir = require('../lib/attachments-from-dir');
const fs = require('../lib/sync-fs');
const pouch = require('../lib/db');
const insertOrReplace = require('../lib/insert-or-replace');
const warnUploadOverwrite = require('../lib/warn-upload-overwrite');
const { info, warn } = require('../lib/log');

module.exports = async (configPath, directoryPath, dbDocName) => {
if (!configPath && !directoryPath && !dbDocName) {
warn('Information missing: Make sure to provide the configuration file path and the directory path.');
return Promise.resolve();
}

const jsonPath = fs.path.resolve(configPath);

if (!fs.exists(jsonPath)) {
warn(`No configuration file found at path: ${jsonPath}`);
return Promise.resolve();
}

const settings = fs.readJson(jsonPath);

const doc = {
...settings,
_id: dbDocName,
_attachments: attachmentsFromDir(directoryPath),
};

const db = pouch();

const changes = await warnUploadOverwrite.preUploadDoc(db, doc);

if (changes) {
await insertOrReplace(db, doc);
info('Configuration upload complete!');
} else {
info('Configuration not uploaded as no changes found');
}

warnUploadOverwrite.postUploadDoc(doc);

return Promise.resolve();
};
26 changes: 26 additions & 0 deletions test/fn/upload-branding.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { expect } = require('chai');
const sinon = require('sinon');
const rewire = require('rewire');
const environment = require('../../src/lib/environment');
const uploadBranding = rewire('../../src/fn/upload-branding');

describe('Upload Branding', () => {
afterEach(() => {
sinon.reset();
});

it('should call uploadConfigurationDocs with expected parameters', async () => {
const configurationPath = `${environment.pathToProject}/branding.json`;
const directoryPath = `${environment.pathToProject}/branding`;
const dbDocName = 'branding';
const uploadConfigurationDocs = sinon.stub().returns(Promise.resolve());

return uploadBranding.__with__({ uploadConfigurationDocs })(async () => {
await uploadBranding.execute();

expect(uploadConfigurationDocs.args[0][0]).to.equal(configurationPath);
expect(uploadConfigurationDocs.args[0][1]).to.equal(directoryPath);
expect(uploadConfigurationDocs.args[0][2]).to.equal(dbDocName);
});
});
});
26 changes: 26 additions & 0 deletions test/fn/upload-partners.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { expect } = require('chai');
const sinon = require('sinon');
const rewire = require('rewire');
const environment = require('../../src/lib/environment');
const uploadPartners = rewire('../../src/fn/upload-partners');

describe('Upload Partners', () => {
afterEach(() => {
sinon.reset();
});

it('should call uploadConfigurationDocs with expected parameters', async () => {
const configurationPath = `${environment.pathToProject}/partners.json`;
const directoryPath = `${environment.pathToProject}/partners`;
const dbDocName = 'partners';
const uploadConfigurationDocs = sinon.stub().returns(Promise.resolve());

return uploadPartners.__with__({ uploadConfigurationDocs })(async () => {
await uploadPartners.execute();

expect(uploadConfigurationDocs.args[0][0]).to.equal(configurationPath);
expect(uploadConfigurationDocs.args[0][1]).to.equal(directoryPath);
expect(uploadConfigurationDocs.args[0][2]).to.equal(dbDocName);
});
});
});
latin-panda marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 26 additions & 0 deletions test/fn/upload-resources.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { expect } = require('chai');
const sinon = require('sinon');
const rewire = require('rewire');
const environment = require('../../src/lib/environment');
const uploadResources = rewire('../../src/fn/upload-resources');

describe('Upload Resources', () => {
afterEach(() => {
sinon.reset();
});

it('should call uploadConfigurationDocs with expected parameters', async () => {
const configurationPath = `${environment.pathToProject}/resources.json`;
const directoryPath = `${environment.pathToProject}/resources`;
const dbDocName = 'resources';
const uploadConfigurationDocs = sinon.stub().returns(Promise.resolve());

return uploadResources.__with__({ uploadConfigurationDocs })(async () => {
await uploadResources.execute();

expect(uploadConfigurationDocs.args[0][0]).to.equal(configurationPath);
expect(uploadConfigurationDocs.args[0][1]).to.equal(directoryPath);
expect(uploadConfigurationDocs.args[0][2]).to.equal(dbDocName);
});
});
});
148 changes: 148 additions & 0 deletions test/lib/upload-configuration-docs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const { expect } = require('chai');
const sinon = require('sinon');
const api = require('../api-stub');
const rewire = require('rewire');
const uploadConfigurationDocs = rewire('../../src/lib/upload-configuration-docs');

describe('Upload Configuration Docs', () => {
let fs;
let warn;
let pouch;
let warnUploadOverwrite;
let insertOrReplace;
let attachmentsFromDir;
let configuration = {
title: 'ABC Company',
resources: {
greatCompany: 'greatCompany.png',
aliasCompany: 'aliasCompany.png'
}
};

beforeEach(() => {
api.start();
fs = {
exists: () => true,
readJson: () => configuration,
path: {
resolve: () => 'path/configuration.json'
}
};
warnUploadOverwrite = {
preUploadDoc: sinon.stub(),
postUploadDoc: sinon.stub()
};
warn = sinon.stub();
pouch = sinon.stub();
insertOrReplace = sinon.stub();
attachmentsFromDir = sinon.stub();
});

afterEach(() => {
api.stop();
sinon.reset();
});

it('should upload configuration', async () => {
warnUploadOverwrite.preUploadDoc.returns(true);
insertOrReplace.returns(Promise.resolve());
attachmentsFromDir.returns({ image: {} });

const configurationDoc = {
_id: 'configurationDoc',
title: 'ABC Company',
resources: {
greatCompany: 'greatCompany.png',
aliasCompany: 'aliasCompany.png'
},
_attachments: { image: {} }
};
const rewireWith = {
fs,
pouch,
attachmentsFromDir,
warnUploadOverwrite,
insertOrReplace
};

return uploadConfigurationDocs.__with__(rewireWith)(async () => {
await uploadConfigurationDocs('path/configuration.json', 'path/configuration', 'configurationDoc');

expect(attachmentsFromDir.called).to.be.true;
expect(pouch.called).to.be.true;
expect(warnUploadOverwrite.preUploadDoc.args[0][1]).to.deep.include(configurationDoc);
expect(warnUploadOverwrite.postUploadDoc.args[0][0]).to.deep.include(configurationDoc);
expect(insertOrReplace.args[0][1]).to.deep.include(configurationDoc);
});
});

it('should warn when paths no provided', async () => {
fs.exists = () => false;
const rewireWith = {
fs,
warn,
pouch,
attachmentsFromDir,
warnUploadOverwrite,
insertOrReplace
};

return uploadConfigurationDocs.__with__(rewireWith)(async () => {
await uploadConfigurationDocs('configuration.js', 'configuration', 'configuration');

expect(warn.called).to.be.true;
expect(attachmentsFromDir.called).to.be.false;
expect(pouch.called).to.be.false;
expect(warnUploadOverwrite.preUploadDoc.called).to.be.false;
expect(insertOrReplace.called).to.be.false;
});
});

it('should warn when config file doesnt exists', async () => {
fs.exists = () => false;
const rewireWith = {
fs,
warn,
pouch,
attachmentsFromDir,
warnUploadOverwrite,
insertOrReplace
};

return uploadConfigurationDocs.__with__(rewireWith)(async () => {
await uploadConfigurationDocs('configuration.js', 'configuration', 'configuration');

expect(warn.called).to.be.true;
expect(attachmentsFromDir.called).to.be.false;
expect(pouch.called).to.be.false;
expect(warnUploadOverwrite.preUploadDoc.called).to.be.false;
expect(insertOrReplace.called).to.be.false;
});
});

it('should inform when no changes detected', async () => {
warnUploadOverwrite.preUploadDoc.returns(false);
insertOrReplace.returns(Promise.resolve());
attachmentsFromDir.returns({ image: {} });
const info = sinon.stub();
const rewireWith = {
fs,
info,
pouch,
attachmentsFromDir,
warnUploadOverwrite,
insertOrReplace
};

return uploadConfigurationDocs.__with__(rewireWith)(async () => {
await uploadConfigurationDocs('configuration.js', 'configuration', 'configuration');

expect(info.called).to.be.true;
expect(attachmentsFromDir.called).to.be.true;
expect(pouch.called).to.be.true;
expect(warnUploadOverwrite.preUploadDoc.called).to.be.true;
expect(warnUploadOverwrite.postUploadDoc.called).to.be.true;
expect(insertOrReplace.called).to.be.false;
});
});
});