diff --git a/test/fn/upload-custom-translations.spec.js b/test/fn/upload-custom-translations.spec.js index f4eb07b5b..3e0d5b146 100644 --- a/test/fn/upload-custom-translations.spec.js +++ b/test/fn/upload-custom-translations.spec.js @@ -1,7 +1,7 @@ const { assert } = require('chai'); const sinon = require('sinon'); const readline = require('readline-sync'); -const api = require('../api-stub'); +const apiStub = require('../api-stub'); const environment = require('../../src/lib/environment'); const log = require('../../src/lib/log'); const uploadCustomTranslations = require('../../src/fn/upload-custom-translations').execute; @@ -17,12 +17,12 @@ describe('upload-custom-translations', () => { sinon.stub(environment, 'isArchiveMode').get(() => false); sinon.stub(environment, 'skipTranslationCheck').get(() => false); sinon.stub(environment, 'force').get(() => false); - return api.start(); + return apiStub.start(); }); afterEach(async () => { sinon.restore(); - await api.stop(); + await apiStub.stop(); }); describe('medic-2.x', function () { @@ -30,24 +30,28 @@ describe('upload-custom-translations', () => { beforeEach(() => { // medic-client does not have deploy_info property - return api.db.put({ _id: '_design/medic-client' }); + return apiStub.db.put({ _id: '_design/medic-client' }); }); it('should upload simple translations', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, + { + status: 200, + body: { compressible_types: 'text/*, application/javascript, application/json, application/xml' }, + }, ); mockTestDir(`simple`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.values, { a:'first', b:'second', c:'third' }); assert(!messagesEn.generic); @@ -57,26 +61,30 @@ describe('upload-custom-translations', () => { it('should upload translations for multiple languages', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, + { + status: 200, + body: { compressible_types: 'text/*, application/javascript, application/json, application/xml' }, + }, ); mockTestDir(`multi-lang`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en', 'fr')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en', 'fr')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert(messagesEn.name === 'English'); assert.deepEqual(messagesEn.values, { one: 'one' }); assert(!messagesEn.generic); assert(!messagesEn.custom); }) - .then(() => getTranslationDoc(api, 'fr')) + .then(() => getTranslationDoc(apiStub, 'fr')) .then(messagesFr => { assert(messagesFr.name === 'Français (French)'); assert.deepEqual(messagesFr.values, { one: 'un(e)' }); @@ -87,7 +95,7 @@ describe('upload-custom-translations', () => { it('should upload translations containing equals signs', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -98,8 +106,8 @@ describe('upload-custom-translations', () => { mockTestDir(`contains-equals`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.values, { 'some.words':'one equals one', @@ -112,7 +120,7 @@ describe('upload-custom-translations', () => { it('should work correctly when falling back to testing messages-en', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -122,7 +130,7 @@ describe('upload-custom-translations', () => { ); mockTestDir(`custom-lang`); - return api.db + return apiStub.db .put({ _id: 'messages-en', code: 'en', @@ -131,14 +139,14 @@ describe('upload-custom-translations', () => { values: { a: 'first' } }) .then(() => uploadCustomTranslations()) - .then(() => expectTranslationDocs(api, 'en', 'fr')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en', 'fr')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.values, { a:'first' }); assert(!messagesEn.generic); assert(!messagesEn.custom); }) - .then(() => getTranslationDoc(api, 'fr')) + .then(() => getTranslationDoc(apiStub, 'fr')) .then(messagesFr => { assert.deepEqual(messagesFr.values, { one: 'un(e)' }); assert(!messagesFr.generic); @@ -148,7 +156,7 @@ describe('upload-custom-translations', () => { it('should set default name for unknown language', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -160,8 +168,8 @@ describe('upload-custom-translations', () => { mockTestDir(`unknown-lang`); sinon.replace(log, 'warn', sinon.fake()); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'qp')) - .then(() => getTranslationDoc(api, 'qp')) + .then(() => expectTranslationDocs(apiStub, 'qp')) + .then(() => getTranslationDoc(apiStub, 'qp')) .then(messagesQp => { assert(messagesQp.name === 'TODO: please ask admin to set this in settings UI'); assert(log.warn.lastCall.calledWithMatch('\'qp\' is not a recognized ISO 639 language code, please ask admin to set the name')); @@ -176,12 +184,12 @@ describe('upload-custom-translations', () => { beforeEach(() => { readline.keyInYN = () => true; readline.keyInSelect = () => 0; - return api.db.put({ _id: '_design/medic-client', deploy_info: { version: '3.0.0' } }); + return apiStub.db.put({ _id: '_design/medic-client', deploy_info: { version: '3.0.0' } }); }); it('should upload simple translations', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -191,8 +199,8 @@ describe('upload-custom-translations', () => { ); mockTestDir(`simple`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.values, { a:'first', b:'second', c:'third' }); assert(!messagesEn.generic); @@ -202,7 +210,7 @@ describe('upload-custom-translations', () => { it('should upload translations for multiple languages', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -212,15 +220,15 @@ describe('upload-custom-translations', () => { ); mockTestDir(`multi-lang`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en', 'fr')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en', 'fr')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert(messagesEn.name === 'English'); assert.deepEqual(messagesEn.values, { one: 'one' }); assert(!messagesEn.generic); assert(!messagesEn.custom); }) - .then(() => getTranslationDoc(api, 'fr')) + .then(() => getTranslationDoc(apiStub, 'fr')) .then(messagesFr => { assert(messagesFr.name === 'Français (French)'); assert.deepEqual(messagesFr.values, { one: 'un(e)' }); @@ -231,7 +239,7 @@ describe('upload-custom-translations', () => { it('should upload translations containing equals signs', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -241,8 +249,8 @@ describe('upload-custom-translations', () => { ); mockTestDir(`contains-equals`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.values, { 'some.words':'one equals one', @@ -255,7 +263,7 @@ describe('upload-custom-translations', () => { it('should merge with existent translations', () => { mockTestDir(`with-customs`); - return api.db + return apiStub.db .put({ _id: 'messages-en', code: 'en', @@ -264,8 +272,8 @@ describe('upload-custom-translations', () => { values: { a:'first', from_custom:'third' } }) .then(() => uploadCustomTranslations()) - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.values, { a:'first', from_custom: 'overwritten', from_custom_new: 'new' }); assert(!messagesEn.generic); @@ -275,7 +283,7 @@ describe('upload-custom-translations', () => { it('should error for malformed translation files', () => { mockTestDir(`with-customs`); - return api.db + return apiStub.db .put({ _id: 'messages-en', code: 'en', @@ -291,8 +299,8 @@ describe('upload-custom-translations', () => { it('should set default name for unknown language', () => { mockTestDir(`unknown-lang`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'qp')) - .then(() => getTranslationDoc(api, 'qp')) + .then(() => expectTranslationDocs(apiStub, 'qp')) + .then(() => getTranslationDoc(apiStub, 'qp')) .then(messagesQp => { assert(messagesQp.name === 'TODO: please ask admin to set this in settings UI'); }); @@ -303,12 +311,12 @@ describe('upload-custom-translations', () => { this.timeout(60000); beforeEach(() => { - api.db.put({ _id: '_design/medic-client', deploy_info: { version: '3.4.0' } }); + apiStub.db.put({ _id: '_design/medic-client', deploy_info: { version: '3.4.0' } }); }); it('should upload simple translations', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -318,8 +326,8 @@ describe('upload-custom-translations', () => { ); mockTestDir(`simple`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.custom, { a:'first', b:'second', c:'third' }); assert.deepEqual(messagesEn.generic, {}); @@ -329,7 +337,7 @@ describe('upload-custom-translations', () => { it('should upload translations for multiple languages', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -339,15 +347,15 @@ describe('upload-custom-translations', () => { ); mockTestDir(`multi-lang`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en', 'fr')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en', 'fr')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert(messagesEn.name === 'English'); assert.deepEqual(messagesEn.custom, { one: 'one' }); assert.deepEqual(messagesEn.generic, {}); assert(!messagesEn.values); }) - .then(() => getTranslationDoc(api, 'fr')) + .then(() => getTranslationDoc(apiStub, 'fr')) .then(messagesFr => { assert(messagesFr.name === 'Français (French)'); assert.deepEqual(messagesFr.custom, { one: 'un(e)' }); @@ -358,7 +366,7 @@ describe('upload-custom-translations', () => { it('should upload translations containing equals signs', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -368,8 +376,8 @@ describe('upload-custom-translations', () => { ); mockTestDir(`contains-equals`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.custom, { 'some.words':'one equals one', @@ -382,7 +390,7 @@ describe('upload-custom-translations', () => { it('should replace existent custom values', () => { mockTestDir(`with-customs`); - return api.db + return apiStub.db .put({ _id: 'messages-en', code: 'en', @@ -392,8 +400,8 @@ describe('upload-custom-translations', () => { custom: { c: 'third' } }) .then(() => uploadCustomTranslations()) - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.generic, { a: 'first' }); assert.deepEqual(messagesEn.custom, { from_custom: 'overwritten', from_custom_new: 'new' }); @@ -403,7 +411,7 @@ describe('upload-custom-translations', () => { it('should replace delete custom values', () => { mockTestDir(`no-customs`); - return api.db + return apiStub.db .put({ _id: 'messages-en', code: 'en', @@ -413,8 +421,8 @@ describe('upload-custom-translations', () => { custom: { c: 'third' } }) .then(() => uploadCustomTranslations()) - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.generic, { a: 'first' }); assert.deepEqual(messagesEn.custom, { }); @@ -424,7 +432,7 @@ describe('upload-custom-translations', () => { it('should work correctly when falling back to testing messages-en', () => { // api/deploy-info endpoint doesn't exist - api.giveResponses( + apiStub.giveResponses( { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, { status: 404, body: { error: 'not_found' } }, @@ -434,13 +442,13 @@ describe('upload-custom-translations', () => { ); mockTestDir(`custom-lang`); // for *some* reason, medic-client doesn't have deploy-info - return api.db + return apiStub.db .get('_design/medic-client') .then(ddoc => { delete ddoc.deploy_info; - return api.db.put(ddoc); + return apiStub.db.put(ddoc); }) - .then(() => api.db.put({ + .then(() => apiStub.db.put({ _id: 'messages-en', code: 'en', name: 'English', @@ -448,14 +456,14 @@ describe('upload-custom-translations', () => { generic: { a: 'first' } })) .then(() => uploadCustomTranslations()) - .then(() => expectTranslationDocs(api, 'en', 'fr')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en', 'fr')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.generic, { a:'first' }); assert(!messagesEn.custom); assert(!messagesEn.values); }) - .then(() => getTranslationDoc(api, 'fr')) + .then(() => getTranslationDoc(apiStub, 'fr')) .then(messagesFr => { assert.deepEqual(messagesFr.custom, { one: 'un(e)' }); assert.deepEqual(messagesFr.generic, {}); @@ -466,8 +474,8 @@ describe('upload-custom-translations', () => { it('should set default name for unknown language', () => { mockTestDir(`unknown-lang`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'qp')) - .then(() => getTranslationDoc(api, 'qp')) + .then(() => expectTranslationDocs(apiStub, 'qp')) + .then(() => getTranslationDoc(apiStub, 'qp')) .then(messagesQp => { assert(messagesQp.name === 'TODO: please ask admin to set this in settings UI'); }); @@ -476,18 +484,19 @@ describe('upload-custom-translations', () => { describe('3.5.0', function () { this.timeout(60000); - beforeEach(() => { - // api/deploy-info endpoint exists - api.giveResponses({ status: 200, body: { version: '3.5.0' } }); - return api.db.put({ _id: '_design/medic-client', deploy_info: { version: '3.5.0' } }); + return apiStub.db.put({ _id: '_design/medic-client', deploy_info: { version: '3.5.0' } }); }); it('should upload simple translations', () => { + // api/deploy-info endpoint exists + apiStub.giveResponses( + { status: 200, body: { version: '3.5.0' } }, + ); mockTestDir(`simple`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.custom, { a:'first', b:'second', c:'third' }); assert.deepEqual(messagesEn.generic, {}); @@ -496,17 +505,21 @@ describe('upload-custom-translations', () => { }); it('should upload translations for multiple languages', () => { + // api/deploy-info endpoint exists + apiStub.giveResponses( + { status: 200, body: { version: '3.5.0' } }, + ); mockTestDir(`multi-lang`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en', 'fr')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en', 'fr')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert(messagesEn.name === 'English'); assert.deepEqual(messagesEn.custom, { one: 'one' }); assert.deepEqual(messagesEn.generic, {}); assert(!messagesEn.values); }) - .then(() => getTranslationDoc(api, 'fr')) + .then(() => getTranslationDoc(apiStub, 'fr')) .then(messagesFr => { assert(messagesFr.name === 'Français (French)'); assert.deepEqual(messagesFr.custom, { one: 'un(e)' }); @@ -516,10 +529,12 @@ describe('upload-custom-translations', () => { }); it('should upload translations containing equals signs', () => { + // api/deploy-info endpoint exists + apiStub.giveResponses({ status: 200, body: { version: '3.5.0' } }); mockTestDir(`contains-equals`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.custom, { 'some.words':'one equals one', @@ -531,20 +546,24 @@ describe('upload-custom-translations', () => { }); it('should set default name for unknown language', () => { + // api/deploy-info endpoint exists + apiStub.giveResponses({ status: 200, body: { version: '3.5.0' } }); mockTestDir(`unknown-lang`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'qp')) - .then(() => getTranslationDoc(api, 'qp')) + .then(() => expectTranslationDocs(apiStub, 'qp')) + .then(() => getTranslationDoc(apiStub, 'qp')) .then(messagesQp => { assert(messagesQp.name === 'TODO: please ask admin to set this in settings UI'); }); }); it('should properly upload translations containing escaped exclamation marks', () => { + // api/deploy-info endpoint exists + apiStub.giveResponses({ status: 200, body: { version: '3.5.0' } }); mockTestDir(`escaped-exclamation`); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) - .then(() => getTranslationDoc(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) + .then(() => getTranslationDoc(apiStub, 'en')) .then(messagesEn => { assert.deepEqual(messagesEn.custom, { 'one.escaped.exclamation':'one equals one!', @@ -556,10 +575,12 @@ describe('upload-custom-translations', () => { }); it('upload translations containing empty messages raises warn logs but works', () => { + // api/deploy-info endpoint exists + apiStub.giveResponses({ status: 200, body: { version: '3.5.0' } }); mockTestDir('contains-empty-messages'); sinon.replace(log, 'warn', sinon.fake()); return uploadCustomTranslations() - .then(() => expectTranslationDocs(api, 'en')) + .then(() => expectTranslationDocs(apiStub, 'en')) .then(() => { assert(log.warn.lastCall.calledWithMatch( '1 empty messages trying to compile translations')); diff --git a/test/fn/watch-project.spec.js b/test/fn/watch-project.spec.js index 74a48328a..e3fd617e3 100644 --- a/test/fn/watch-project.spec.js +++ b/test/fn/watch-project.spec.js @@ -99,7 +99,7 @@ function watchWrapper(action, file) { }); } -describe.skip('watch-project', function () { +describe.only('watch-project', () => { beforeEach(() => { sinon.stub(environment, 'pathToProject').get(() => testDir); sinon.stub(environment, 'extraArgs').get(() => { }); diff --git a/test/lib/warn-upload-overwrite.spec.js b/test/lib/warn-upload-overwrite.spec.js index cbbc602eb..f1249733d 100644 --- a/test/lib/warn-upload-overwrite.spec.js +++ b/test/lib/warn-upload-overwrite.spec.js @@ -4,17 +4,19 @@ const { assert, expect } = require('chai'); const request = require('request-promise-native'); const environment = require('../../src/lib/environment'); -const api = require('../api-stub'); +const apiStub = require('../api-stub'); const fs = require('../../src/lib/sync-fs'); const readline = require('readline-sync'); -const warnUploadOverwrite = rewire('../../src/lib/warn-upload-overwrite'); const log = require('../../src/lib/log'); +let api = rewire('../../src/lib/api'); +let warnUploadOverwrite = rewire('../../src/lib/warn-upload-overwrite'); let calls; describe('warn-upload-overwrite', () => { beforeEach(() => { + warnUploadOverwrite.__set__('api', api); sinon.stub(environment, 'isArchiveMode').get(() => false); sinon.stub(environment, 'force').get(() => false); sinon.stub(environment, 'pathToProject').get(() => '.'); @@ -23,12 +25,14 @@ describe('warn-upload-overwrite', () => { calls.push(args); }; sinon.stub(fs, 'exists').returns(true); - api.start(); + apiStub.start(); }); afterEach(() => { sinon.restore(); - return api.stop(); + api = rewire('../../src/lib/api'); + warnUploadOverwrite = rewire('../../src/lib/warn-upload-overwrite'); + return apiStub.stop(); }); describe('getStoredHash', () => { @@ -119,12 +123,13 @@ describe('warn-upload-overwrite', () => { it('shows diff when local is different from remote and the user requests a diff', () => { sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(readline, 'keyInSelect').returns(2); - sinon.stub(api.db, 'get').resolves({ _id: 'a', _rev: 'x', value: 1 }); + sinon.stub(apiStub.db, 'get').resolves({ _id: 'a', _rev: 'x', value: 1 }); sinon.stub(fs, 'read').returns(JSON.stringify({ a: { 'localhost/medic': 'y' }})); sinon.stub(environment, 'apiUrl').get(() => 'http://admin:pass@localhost:35423/medic'); sinon.stub(request, 'get').resolves({'compressible_types':'text/*, application/*','compression_level':'8'}); + api.__set__('cache', new Map()); const localDoc = { _id: 'a', value: 2 }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).then(() => { assert.equal(calls.length, 1); assert.equal(request.get.args[0][0].url, 'http://admin:pass@localhost:35423/api/couch-config-attachments'); assert.equal(request.get.callCount, 1); @@ -132,13 +137,14 @@ describe('warn-upload-overwrite', () => { }); }); - it('aborts when local is different from remote and the user requests an abort', () => { + it('aborts when local is different from remote and the user requests an abort', function () { + this.timeout(30000); sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(readline, 'keyInSelect').returns(3); - sinon.stub(api.db, 'get').resolves({ _rev: 'x' }); + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x' }); sinon.stub(fs, 'read').returns(JSON.stringify({ a: { 'localhost/medic': 'y' }})); const localDoc = { _id: 'a', _rev: 'y' }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).catch(e => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).catch(e => { assert.equal('configuration modified', e.message); }); }); @@ -147,9 +153,8 @@ describe('warn-upload-overwrite', () => { const write = sinon.stub(fs, 'write').returns(); sinon.stub(fs, 'read').returns(JSON.stringify({ a: { 'y/m': 'a-12' }})); sinon.stub(request, 'get').resolves({'compressible_types':'text/*, application/*','compression_level':'8'}); - warnUploadOverwrite.__set__('cache', new Map()); const localDoc = { _id: 'a' }; - await warnUploadOverwrite.postUploadDoc(api.db, localDoc); + await warnUploadOverwrite.postUploadDoc(apiStub.db, localDoc); assert.equal(write.callCount, 1); assert.deepEqual( JSON.parse(write.args[0][1]), @@ -157,13 +162,14 @@ describe('warn-upload-overwrite', () => { ); }); - it('forces execution by returning early', () => { + it('forces execution by returning early', function () { + this.timeout(30000); sinon.stub(environment, 'force').get(() => true); sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(readline, 'keyInSelect').returns(-1); - sinon.stub(api.db, 'get').resolves({ _rev: 'x' }); + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x' }); const localDoc = { _id: 'x' }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).then(() => { assert.equal(0, readline.keyInSelect.callCount); }); }); @@ -174,25 +180,25 @@ describe('warn-upload-overwrite', () => { warnUploadOverwrite.__set__('api', ()=> ({ getCompressibleTypes: () => ['text/*', 'application/*'] })); - sinon.stub(api.db, 'get').resolves({ + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', digest: 'md5-digest' } } }); - sinon.stub(api.db, 'getAttachment').resolves('data'); + sinon.stub(apiStub.db, 'getAttachment').resolves('data'); const localDoc = { _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', data: 'data changed' } } }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).then(() => { assert.equal(1, readline.keyInSelect.callCount); - assert.equal(api.db.getAttachment.args[0][0], 'x'); - assert.equal(api.db.getAttachment.args[0][1], 'random.txt'); - assert.equal(api.db.getAttachment.callCount, 1); + assert.equal(apiStub.db.getAttachment.args[0][0], 'x'); + assert.equal(apiStub.db.getAttachment.args[0][1], 'random.txt'); + assert.equal(apiStub.db.getAttachment.callCount, 1); }); }); @@ -201,21 +207,21 @@ describe('warn-upload-overwrite', () => { sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(request, 'get').resolves({'compressible_types':'text/*, application/*','compression_level':'8'}); warnUploadOverwrite.__set__('cache', new Map()); - sinon.stub(api.db, 'get').resolves({ + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', digest: 'md5-digest' } } }); - sinon.stub(api.db, 'getAttachment').resolves('data'); + sinon.stub(apiStub.db, 'getAttachment').resolves('data'); const localDoc = { _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', data: 'data' } } }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).then(() => { assert.equal(0, readline.keyInSelect.callCount); }); }); @@ -227,7 +233,7 @@ describe('warn-upload-overwrite', () => { warnUploadOverwrite.__set__('api', ()=> ({ getCompressibleTypes: () => ['text/*', 'application/*'] })); - sinon.stub(api.db, 'get').resolves({ + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _id: 'x', _attachments: { @@ -236,42 +242,43 @@ describe('warn-upload-overwrite', () => { 'anotherRandom.txt': { content_type: 'text/plain', digest: 'md5-digest' }, } }); - sinon.stub(api.db, 'getAttachment').resolves('data'); + sinon.stub(apiStub.db, 'getAttachment').resolves('data'); const localDoc = { _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', data: 'data changed' } } }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).then(() => { assert.equal(1, readline.keyInSelect.callCount); - assert.equal(api.db.getAttachment.callCount, 2); - assert.equal(api.db.getAttachment.args[0][0], 'x'); - assert.equal(api.db.getAttachment.args[0][1], 'random.txt'); - assert.equal(api.db.getAttachment.args[1][0], 'x'); - assert.equal(api.db.getAttachment.args[1][1], 'anotherRandom.txt'); + assert.equal(apiStub.db.getAttachment.callCount, 2); + assert.equal(apiStub.db.getAttachment.args[0][0], 'x'); + assert.equal(apiStub.db.getAttachment.args[0][1], 'random.txt'); + assert.equal(apiStub.db.getAttachment.args[1][0], 'x'); + assert.equal(apiStub.db.getAttachment.args[1][1], 'anotherRandom.txt'); }); }); - it('handles failure of the getAttachment endpoint on doc with no changes', () => { + it('handles failure of the getAttachment endpoint on doc with no changes', function () { + this.timeout(30000); sinon.stub(readline, 'keyInSelect').returns(-1); sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(request, 'get').rejects({ error: 'not_found', reason: 'Database does not exist.' }); - sinon.stub(api.db, 'get').resolves({ + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', digest: 'md5-digest' } } }); - sinon.stub(api.db, 'getAttachment').resolves('data'); + sinon.stub(apiStub.db, 'getAttachment').resolves('data'); const localDoc = { _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', data: 'data' } } }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).then(() => { assert.equal(0, readline.keyInSelect.callCount); }); }); @@ -280,21 +287,21 @@ describe('warn-upload-overwrite', () => { sinon.stub(readline, 'keyInSelect').returns(-1); sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(request, 'get').resolves({ error: 'not_found', reason: 'Database does not exist.' }); - sinon.stub(api.db, 'get').resolves({ + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', digest: 'md5-digest' } } }); - sinon.stub(api.db, 'getAttachment').resolves('data'); + sinon.stub(apiStub.db, 'getAttachment').resolves('data'); const localDoc = { _id: 'x', _attachments: { 'random.txt': { content_type: 'text/plain', data: 'data changed' } } }; - return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => { + return warnUploadOverwrite.preUploadDoc(apiStub.db, localDoc).then(() => { assert.equal(1, readline.keyInSelect.callCount); }); }); @@ -305,12 +312,12 @@ describe('warn-upload-overwrite', () => { it('shows diff when local xml is different from remote xml and the user requests a diff', () => { sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(readline, 'keyInSelect').returns(2); - sinon.stub(api.db, 'get').resolves({ _rev: 'x', _attachments: { xml: { digest: 'abc' } } }); - sinon.stub(api.db, 'getAttachment').resolves(Buffer.from('', 'utf8')); + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _attachments: { xml: { digest: 'abc' } } }); + sinon.stub(apiStub.db, 'getAttachment').resolves(Buffer.from('', 'utf8')); sinon.stub(fs, 'read').returns('{"x":{"localhost/medic":"y"}}'); const localXml = ''; const localDoc = { _id: 'x' }; - return warnUploadOverwrite.preUploadForm(api.db, localDoc, localXml, []).then(() => { + return warnUploadOverwrite.preUploadForm(apiStub.db, localDoc, localXml, []).then(() => { assert.equal(calls.length, 1); assert.equal(calls[0][0], '/\n\tExpected element \'x\' instead of \'y\''); }); @@ -319,12 +326,12 @@ describe('warn-upload-overwrite', () => { it('aborts when local xml is different from remote xml and the user requests an abort', () => { sinon.stub(readline, 'keyInYN').returns(true); sinon.stub(readline, 'keyInSelect').returns(3); - sinon.stub(api.db, 'get').resolves({ _rev: 'x', _attachments: { xml: { digest: 'abc' } } }); - sinon.stub(api.db, 'getAttachment').resolves(Buffer.from('', 'utf8')); + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _attachments: { xml: { digest: 'abc' } } }); + sinon.stub(apiStub.db, 'getAttachment').resolves(Buffer.from('', 'utf8')); sinon.stub(fs, 'read').returns('{"localhost/medic":"y"}'); const localXml = ''; const localDoc = { _id: 'x' }; - return warnUploadOverwrite.preUploadForm(api.db, localDoc, localXml, []).catch(e => { + return warnUploadOverwrite.preUploadForm(apiStub.db, localDoc, localXml, []).catch(e => { assert.equal('configuration modified', e.message); }); }); @@ -332,11 +339,11 @@ describe('warn-upload-overwrite', () => { it('uploads the local xml if remote xml does not exist', () => { let error = new Error('No attachment'); error.status = 404; - const getAttachment = sinon.stub(api.db, 'get').rejects(error); + const getAttachment = sinon.stub(apiStub.db, 'get').rejects(error); sinon.stub(fs, 'read').returns('{"localhost/medic":"y"}'); const localXml = ''; const localDoc = { _id: 'x' }; - return warnUploadOverwrite.preUploadForm(api.db, localDoc, localXml, []).then(() => { + return warnUploadOverwrite.preUploadForm(apiStub.db, localDoc, localXml, []).then(() => { assert(getAttachment.calledOnce); }); }); @@ -344,13 +351,13 @@ describe('warn-upload-overwrite', () => { it('overwrites config when force is set', () => { sinon.stub(environment, 'force').get(() => true); sinon.stub(readline, 'keyInSelect').returns(2); - sinon.stub(api.db, 'get').resolves({ _rev: 'x', _attachments: { xml: { digest: 'abc' } } }); - sinon.stub(api.db, 'getAttachment').resolves(Buffer.from('', 'utf8')); + sinon.stub(apiStub.db, 'get').resolves({ _rev: 'x', _attachments: { xml: { digest: 'abc' } } }); + sinon.stub(apiStub.db, 'getAttachment').resolves(Buffer.from('', 'utf8')); sinon.stub(fs, 'read').returns('{"localhost/medic":"y"}'); const localXml = ''; const localDoc = { _id: 'x' }; - return warnUploadOverwrite.preUploadForm(api.db, localDoc, localXml, []).then(() => { - assert(1, api.db.get.callCount); + return warnUploadOverwrite.preUploadForm(apiStub.db, localDoc, localXml, []).then(() => { + assert(1, apiStub.db.get.callCount); assert.equal(0, readline.keyInSelect.callCount); }); });