Skip to content

Commit

Permalink
fix(migrations): Use proper permissions to create content/settings fo…
Browse files Browse the repository at this point in the history
…lder (#703)

no issue

Fixes an issue where the migration wouldn't have the correct permission the create the new folder within the content directory, when it's owned by the `ghost` user.
  • Loading branch information
e-nomem authored and aileen committed Apr 11, 2018
1 parent 3a77ec8 commit 9b0b0c5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
15 changes: 11 additions & 4 deletions lib/migrations.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
'use strict';

function ensureSettingsFolder() {
const cwd = process.cwd();
const fs = require('fs-extra');
function ensureSettingsFolder(context) {
const path = require('path');
const ghostUser = require('./utils/use-ghost-user');

fs.ensureDirSync(path.resolve(cwd, 'content', 'settings'));
const contentDir = context.instance.config.get('paths.contentPath');

if (ghostUser.shouldUseGhostUser(contentDir)) {
return context.ui.sudo(`mkdir ${path.resolve(contentDir, 'settings')}`, {sudoArgs: '-E -u ghost'});
} else {
const fs = require('fs-extra');
fs.ensureDirSync(path.resolve(contentDir, 'settings'));
return Promise.resolve();
}
}

module.exports = [{
Expand Down
55 changes: 43 additions & 12 deletions test/unit/migrations-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,57 @@

const expect = require('chai').expect;
const sinon = require('sinon');
const createConfig = require('../utils/config-stub');

const fs = require('fs-extra');
const ghostUser = require('../../lib/utils/use-ghost-user');

const migrations = require('../../lib/migrations');

describe('Unit: Migrations', function () {
const sandbox = sinon.sandbox.create();

afterEach(() => {
sandbox.restore();
});

describe('ensureSettingsFolder', function () {
const setupEnv = require('../utils/env');
const path = require('path');
const fs = require('fs');
it('if ghost user owns directory, runs `sudo mkdir` as ghost user', function () {
const ghostUserStub = sandbox.stub(ghostUser, 'shouldUseGhostUser').returns(true);
const sudoStub = sandbox.stub().resolves();
const config = createConfig();
config.get.withArgs('paths.contentPath').returns('/var/www/ghost/content');

const context = {
instance: {config: config},
ui: {sudo: sudoStub}
};

return migrations[0].task(context).then(() => {
expect(ghostUserStub.calledOnce).to.be.true;
expect(ghostUserStub.calledWithExactly('/var/www/ghost/content')).to.be.true;
expect(sudoStub.calledOnce).to.be.true;
expect(sudoStub.calledWithExactly(
'mkdir /var/www/ghost/content/settings',
{sudoArgs: '-E -u ghost'}
)).to.be.true;
});
});

it('creates settings folder if not existent', function () {
const env = setupEnv();
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
const ensureSettingsFolder = migrations[0].task;
it('if ghost user doesn\'t own directory, runs basic mkdir', function () {
const ghostUserStub = sandbox.stub(ghostUser, 'shouldUseGhostUser').returns(false);
const fsStub = sandbox.stub(fs, 'ensureDirSync');
const config = createConfig();
config.get.withArgs('paths.contentPath').returns('/var/www/ghost/content');

ensureSettingsFolder();
expect(cwdStub.calledOnce).to.be.true;
expect(fs.existsSync(path.join(env.dir, 'content/settings'))).to.be.true;
const context = {instance: {config: config}};

cwdStub.restore();
env.cleanup();
return migrations[0].task(context).then(() => {
expect(ghostUserStub.calledOnce).to.be.true;
expect(ghostUserStub.calledWithExactly('/var/www/ghost/content')).to.be.true;
expect(fsStub.calledOnce).to.be.true;
expect(fsStub.calledWithExactly('/var/www/ghost/content/settings')).to.be.true;
});
});
});
});

0 comments on commit 9b0b0c5

Please sign in to comment.