|
| 1 | +'use strict'; |
| 2 | +const expect = require('chai').expect; |
| 3 | +const sinon = require('sinon'); |
| 4 | + |
| 5 | +const execa = require('execa'); |
| 6 | +const errors = require('../../../../../lib/errors'); |
| 7 | + |
| 8 | +const contentFolderPermissions = require('../../../../../lib/commands/doctor/checks/content-folder'); |
| 9 | + |
| 10 | +describe('Unit: Doctor Checks > Content folder permissions', function () { |
| 11 | + const sandbox = sinon.sandbox.create(); |
| 12 | + const shouldUseGhostUserStub = sinon.stub(); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + sandbox.restore(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('skips when content folder is not owned by ghost', function () { |
| 19 | + shouldUseGhostUserStub.returns(false); |
| 20 | + const execaStub = sandbox.stub(execa, 'shell').resolves(); |
| 21 | + |
| 22 | + expect(contentFolderPermissions).to.exist; |
| 23 | + expect(contentFolderPermissions.enabled(), 'skips if no Ghost user should be used').to.be.false; |
| 24 | + expect(execaStub.called).to.be.false; |
| 25 | + }); |
| 26 | + |
| 27 | + it('rejects with error if folders have incorrect permissions', function () { |
| 28 | + const execaStub = sandbox.stub(execa, 'shell').resolves({stdout: './content/images\n./content/apps\n./content/themes'}); |
| 29 | + |
| 30 | + shouldUseGhostUserStub.returns(true); |
| 31 | + |
| 32 | + return contentFolderPermissions.task({}).then(() => { |
| 33 | + expect(false, 'error should have been thrown').to.be.true; |
| 34 | + }).catch((error) => { |
| 35 | + expect(error).to.be.an.instanceof(errors.SystemError); |
| 36 | + expect(error.message).to.match(/Your content folder contains some directories or files with incorrect permissions:/); |
| 37 | + expect(error.message).to.match(/- \.\/content\/images/); |
| 38 | + expect(execaStub.called).to.be.true; |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('rejects with error if files have incorrect permissions', function () { |
| 43 | + const execaStub = sandbox.stub(execa, 'shell').resolves({stdout: './content/images/test.jpg'}); |
| 44 | + |
| 45 | + shouldUseGhostUserStub.returns(true); |
| 46 | + |
| 47 | + return contentFolderPermissions.task({}).then(() => { |
| 48 | + expect(false, 'error should have been thrown').to.be.true; |
| 49 | + }).catch((error) => { |
| 50 | + expect(error).to.be.an.instanceof(errors.SystemError); |
| 51 | + expect(error.message).to.match(/Your content folder contains a directory or file with incorrect permissions/); |
| 52 | + expect(error.message).to.match(/- .\/content\/images\/test.jpg/); |
| 53 | + expect(execaStub.called).to.be.true; |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('passes if all folders have the correct permissions', function () { |
| 58 | + const execaStub = sandbox.stub(execa, 'shell').resolves({stdout: ''}); |
| 59 | + |
| 60 | + shouldUseGhostUserStub.returns(true); |
| 61 | + |
| 62 | + return contentFolderPermissions.task({}).then(() => { |
| 63 | + expect(execaStub.called).to.be.true; |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('rejects with error if execa command fails', function () { |
| 68 | + const execaStub = sandbox.stub(execa, 'shell').rejects(new Error('oops, cmd could not be executed')); |
| 69 | + |
| 70 | + shouldUseGhostUserStub.returns(true); |
| 71 | + |
| 72 | + return contentFolderPermissions.task({}).then(() => { |
| 73 | + expect(false, 'error should have been thrown').to.be.true; |
| 74 | + }).catch((error) => { |
| 75 | + expect(error).to.be.an.instanceof(errors.ProcessError); |
| 76 | + expect(error.message).to.match(/oops, cmd could not be executed/); |
| 77 | + expect(execaStub.called).to.be.true; |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments