From e9cbf3451a71a8def698f2c3cb36e4d186f3a37e Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 1 Jun 2021 12:50:17 +0400 Subject: [PATCH] Added update check data test coverage refs https://github.com/TryGhost/Team/issues/728 - This is a continuation of the test coverage for the UpdateCheckService. - Covers a basic check for what kind of data is sent from the service to external analytics --- .../update-check/update_check_spec.js | 141 +++++++++++++----- 1 file changed, 103 insertions(+), 38 deletions(-) diff --git a/test/regression/update-check/update_check_spec.js b/test/regression/update-check/update_check_spec.js index d895e67f8c..81465e1bde 100644 --- a/test/regression/update-check/update_check_spec.js +++ b/test/regression/update-check/update_check_spec.js @@ -16,47 +16,47 @@ let ghostVersion = rewire('../../../core/server/lib/ghost-version'); const UpdateCheckService = rewire('../../../core/server/update-check-service'); describe('Update Check', function () { - describe('fn: check', function () { - const internal = {context: {internal: true}}; - let settingsStub; - let i18nStub; - let loggingStub; - let requestStub; - let urlUtilsStub; + const internal = {context: {internal: true}}; + let settingsStub; + let i18nStub; + let loggingStub; + let requestStub; + let urlUtilsStub; - beforeEach(function () { - settingsStub = sinon.stub().resolves({ - settings: [] - }); - settingsStub.withArgs(Object.assign({key: 'db_hash'}, internal)).resolves({ - settings: [{ - value: 'dummy_db_hash' - }] - }); - settingsStub.withArgs(Object.assign({key: 'active_theme'}, internal)).resolves({ - settings: [{ - value: 'casperito' - }] - }); - - i18nStub = { - t: sinon.stub() - }; - - loggingStub = { - error: sinon.stub() - }; - - requestStub = sinon.stub(); - urlUtilsStub = urlUtils.stubUrlUtilsFromConfig(); + beforeEach(function () { + settingsStub = sinon.stub().resolves({ + settings: [] + }); + settingsStub.withArgs(Object.assign({key: 'db_hash'}, internal)).resolves({ + settings: [{ + value: 'dummy_db_hash' + }] + }); + settingsStub.withArgs(Object.assign({key: 'active_theme'}, internal)).resolves({ + settings: [{ + value: 'casperito' + }] }); - afterEach(function () { - sinon.restore(); - urlUtils.restore(); - configUtils.restore(); - }); + i18nStub = { + t: sinon.stub() + }; + loggingStub = { + error: sinon.stub() + }; + + requestStub = sinon.stub(); + urlUtilsStub = urlUtils.stubUrlUtilsFromConfig(); + }); + + afterEach(function () { + sinon.restore(); + urlUtils.restore(); + configUtils.restore(); + }); + + describe('fn: updateCheck', function () { it('update check was executed', async function () { const updateCheckService = new UpdateCheckService({ api: { @@ -143,6 +143,71 @@ describe('Update Check', function () { requestStub.calledOnce.should.equal(true); requestStub.args[0][0].should.equal('https://updates.ghost.org'); - requestStub.args[0][1].query.ghost_version.should.equal(ghostVersion.full); }); + requestStub.args[0][1].query.ghost_version.should.equal(ghostVersion.full); + }); + }); + + describe('Data sent with the POST request', function () { + before(function () { + configUtils.set('privacy:useUpdateCheck', true); + }); + + after(function () { + configUtils.restore(); + }); + + it('should report the correct data', async function () { + const updateCheckService = new UpdateCheckService({ + api: { + settings: { + read: settingsStub, + edit: settingsStub + }, + users: { + browse: sinon.stub().resolves({ + users: [{ + created_at: '1995-12-24T23:15:00Z' + }, {}] + }) + }, + posts: { + browse: sinon.stub().resolves({ + meta: { + pagination: { + total: 13 + } + } + }) + } + }, + config: configUtils.config, + i18n: i18nStub, + logging: loggingStub, + urlUtils: urlUtilsStub, + request: requestStub, + ghostVersion, + ghostMailer: mailService + }); + + await updateCheckService.check(); + + requestStub.calledOnce.should.equal(true); + + requestStub.args[0][0].should.equal('https://updates.ghost.org'); + + const data = requestStub.args[0][1].body; + data.ghost_version.should.equal(packageInfo.version); + data.node_version.should.equal(process.versions.node); + data.env.should.equal(process.env.NODE_ENV); + data.database_type.should.match(/sqlite3|mysql/); + data.blog_id.should.be.a.String(); + data.blog_id.should.not.be.empty(); + data.theme.should.be.equal('casperito'); + data.blog_created_at.should.equal(819846900); + data.user_count.should.be.equal(2); + data.post_count.should.be.equal(13); + data.npm_version.should.be.a.String(); + data.npm_version.should.not.be.empty(); + }); }); });