Skip to content

Commit 9b0b0c5

Browse files
e-nomemaileen
authored andcommitted
fix(migrations): Use proper permissions to create content/settings folder (#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.
1 parent 3a77ec8 commit 9b0b0c5

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

lib/migrations.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
'use strict';
22

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

8-
fs.ensureDirSync(path.resolve(cwd, 'content', 'settings'));
7+
const contentDir = context.instance.config.get('paths.contentPath');
8+
9+
if (ghostUser.shouldUseGhostUser(contentDir)) {
10+
return context.ui.sudo(`mkdir ${path.resolve(contentDir, 'settings')}`, {sudoArgs: '-E -u ghost'});
11+
} else {
12+
const fs = require('fs-extra');
13+
fs.ensureDirSync(path.resolve(contentDir, 'settings'));
14+
return Promise.resolve();
15+
}
916
}
1017

1118
module.exports = [{

test/unit/migrations-spec.js

+43-12
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,57 @@
22

33
const expect = require('chai').expect;
44
const sinon = require('sinon');
5+
const createConfig = require('../utils/config-stub');
6+
7+
const fs = require('fs-extra');
8+
const ghostUser = require('../../lib/utils/use-ghost-user');
59

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

812
describe('Unit: Migrations', function () {
13+
const sandbox = sinon.sandbox.create();
14+
15+
afterEach(() => {
16+
sandbox.restore();
17+
});
18+
919
describe('ensureSettingsFolder', function () {
10-
const setupEnv = require('../utils/env');
11-
const path = require('path');
12-
const fs = require('fs');
20+
it('if ghost user owns directory, runs `sudo mkdir` as ghost user', function () {
21+
const ghostUserStub = sandbox.stub(ghostUser, 'shouldUseGhostUser').returns(true);
22+
const sudoStub = sandbox.stub().resolves();
23+
const config = createConfig();
24+
config.get.withArgs('paths.contentPath').returns('/var/www/ghost/content');
25+
26+
const context = {
27+
instance: {config: config},
28+
ui: {sudo: sudoStub}
29+
};
30+
31+
return migrations[0].task(context).then(() => {
32+
expect(ghostUserStub.calledOnce).to.be.true;
33+
expect(ghostUserStub.calledWithExactly('/var/www/ghost/content')).to.be.true;
34+
expect(sudoStub.calledOnce).to.be.true;
35+
expect(sudoStub.calledWithExactly(
36+
'mkdir /var/www/ghost/content/settings',
37+
{sudoArgs: '-E -u ghost'}
38+
)).to.be.true;
39+
});
40+
});
1341

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

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

23-
cwdStub.restore();
24-
env.cleanup();
50+
return migrations[0].task(context).then(() => {
51+
expect(ghostUserStub.calledOnce).to.be.true;
52+
expect(ghostUserStub.calledWithExactly('/var/www/ghost/content')).to.be.true;
53+
expect(fsStub.calledOnce).to.be.true;
54+
expect(fsStub.calledWithExactly('/var/www/ghost/content/settings')).to.be.true;
55+
});
2556
});
2657
});
2758
});

0 commit comments

Comments
 (0)