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 3 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: 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
43 changes: 43 additions & 0 deletions src/fn/upload-branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const attachmentsFromDir = require('../lib/attachments-from-dir');
const environment = require('../lib/environment');
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 = {
requiresInstance: true,
execute: async () => {
const brandingPath = fs.path.resolve(`${environment.pathToProject}/branding.json`);

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

const brandingSettings = fs.readJson(brandingPath);

const doc = {
_id: 'branding',
title: brandingSettings.title,
resources: brandingSettings.resources,
_attachments: attachmentsFromDir(`${environment.pathToProject}/branding`),
};

const db = pouch();

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

if (changes) {
await insertOrReplace(db, doc);
info('Branding file uploaded');
} else {
info('Branding file not uploaded as no changes found');
}

warnUploadOverwrite.postUploadDoc(doc);

return Promise.resolve();
}
};
42 changes: 42 additions & 0 deletions src/fn/upload-partners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const attachmentsFromDir = require('../lib/attachments-from-dir');
const environment = require('../lib/environment');
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 = {
requiresInstance: true,
execute: async () => {
const partnersPath = fs.path.resolve(`${environment.pathToProject}/partners.json`);

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

const partnersSettings = fs.readJson(partnersPath);

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

const db = pouch();

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

if (changes) {
await insertOrReplace(db, doc);
info('Partners file uploaded');
} else {
info('Partners file not uploaded as no changes found');
}

warnUploadOverwrite.postUploadDoc(doc);

return Promise.resolve();
}
};
latin-panda marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/lib/attachment-from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function mimeTypeFor(fileName) {
case 'json': return 'application/json';
case 'png' : return 'image/png';
case 'svg' : return 'image/svg+xml';
case 'ico' : return 'image/x-icon';
latin-panda marked this conversation as resolved.
Show resolved Hide resolved
case 'xml' : return 'application/xml';
default: throw new Error(`Unrecongised file extension: ${extension} for file ${fileName}`);
}
Expand Down
76 changes: 76 additions & 0 deletions test/fn/upload-branding.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const { expect } = require('chai');
const sinon = require('sinon');
const api = require('../api-stub');
const rewire = require('rewire');
const uploadBranding = rewire('../../src/fn/upload-branding');

describe('Upload Branding', () => {
let fs;
let pouch;
let warnUploadOverwrite;
let insertOrReplace;
let attachmentsFromDir;
let branding = {
title: 'Rockstar Clinic',
resources: {
logo: 'star.png',
favicon: 'favicon.ico'
}
};

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

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

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

const brandingDoc = {
_id: 'branding',
title: 'Rockstar Clinic',
resources: {
logo: 'star.png',
favicon: 'favicon.ico'
},
_attachments: { image: {} }
};
const rewireWith = {
fs,
pouch,
attachmentsFromDir,
warnUploadOverwrite,
insertOrReplace
};

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

expect(attachmentsFromDir.called).to.be.true;
expect(pouch.called).to.be.true;
expect(warnUploadOverwrite.preUploadDoc.args[0][1]).to.deep.include(brandingDoc);
expect(warnUploadOverwrite.postUploadDoc.args[0][0]).to.deep.include(brandingDoc);
expect(insertOrReplace.args[0][1]).to.deep.include(brandingDoc);
});
});
});
74 changes: 74 additions & 0 deletions test/fn/upload-partners.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { expect } = require('chai');
const sinon = require('sinon');
const api = require('../api-stub');
const rewire = require('rewire');
const uploadPartners = rewire('../../src/fn/upload-partners');

describe('Upload Partners', () => {
let fs;
let pouch;
let warnUploadOverwrite;
let insertOrReplace;
let attachmentsFromDir;
let partners = {
resources: {
greatCompany: 'greatCompany.png',
aliasCompany: 'aliasCompany.png'
}
};

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

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

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

const partnersDoc = {
_id: 'partners',
resources: {
greatCompany: 'greatCompany.png',
aliasCompany: 'aliasCompany.png'
},
_attachments: { image: {} }
};
const rewireWith = {
fs,
pouch,
attachmentsFromDir,
warnUploadOverwrite,
insertOrReplace
};

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

expect(attachmentsFromDir.called).to.be.true;
expect(pouch.called).to.be.true;
expect(warnUploadOverwrite.preUploadDoc.args[0][1]).to.deep.include(partnersDoc);
expect(warnUploadOverwrite.postUploadDoc.args[0][0]).to.deep.include(partnersDoc);
expect(insertOrReplace.args[0][1]).to.deep.include(partnersDoc);
});
});
});
latin-panda marked this conversation as resolved.
Show resolved Hide resolved