Skip to content

Commit d1b4307

Browse files
committed
feat: added transform to handle urls without proto
closes TryGhost#812 Update url handling behaviour: - When entering a URL, add a protocol if one is missing - Follow the rules laid out in TryGhost#812, so that we use https by default where possible - Also handle domains entered with // at the start Swap to using url util - moved all old usages of validate-instance-url
1 parent c0ac426 commit d1b4307

File tree

5 files changed

+23
-53
lines changed

5 files changed

+23
-53
lines changed

lib/commands/config/advanced.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const portfinder = require('portfinder');
44
const toString = require('lodash/toString');
55
const validator = require('validator');
6-
const validateInstanceURL = require('../../utils/validate-instance-url');
6+
const urlUtils = require('../../utils/url');
77
const url = require('url');
88

99
const BASE_PORT = 2368;
@@ -22,8 +22,8 @@ const knownMailServices = [
2222
module.exports = {
2323
url: {
2424
description: 'Blog URL with protocol E.g. http://loveghost.com',
25-
validate: validateInstanceURL,
26-
transform: value => value.toLowerCase(),
25+
validate: urlUtils.validate,
26+
transform: urlUtils.ensureProtocol,
2727
type: 'string',
2828
group: 'Ghost Options:'
2929
},
@@ -32,7 +32,7 @@ module.exports = {
3232
configPath: 'admin.url',
3333
validate: value => validator.isURL(value, {require_protocol: true}) ||
3434
'Invalid URL. Your URL should include a protocol, E.g. http://my-ghost-blog.com',
35-
transform: value => value.toLowerCase(),
35+
transform: urlUtils.ensureProtocol,
3636
type: 'string',
3737
group: 'Ghost Options:'
3838
},

lib/commands/config/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class ConfigCommand extends Command {
9191
}
9292

9393
getConfigPrompts(argv) {
94-
const validateInstanceURL = require('../../utils/validate-instance-url');
94+
const urlUtils = require('../../utils/url');
9595
const path = require('path');
9696

9797
const prompts = [];
@@ -103,7 +103,8 @@ class ConfigCommand extends Command {
103103
name: 'url',
104104
message: 'Enter your blog URL:',
105105
default: argv.auto ? null : this.instance.config.get('url', 'http://localhost:2368'),
106-
validate: validateInstanceURL
106+
validate: urlUtils.validate,
107+
filter: urlUtils.ensureProtocol
107108
});
108109
}
109110

lib/utils/validate-instance-url.js

-13
This file was deleted.

test/unit/commands/config-spec.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ describe('Unit: Command > Config', function () {
128128
});
129129
});
130130

131+
it('transforms domain without protocol', function () {
132+
const ConfigCommand = require(modulePath);
133+
const instanceConfig = new Config('config.json');
134+
const saveStub = sinon.stub(instanceConfig, 'save');
135+
const getInstanceStub = sinon.stub().returns({config: instanceConfig});
136+
const config = new ConfigCommand({}, {getInstance: getInstanceStub});
137+
138+
return config.handleAdvancedOptions({url: 'myWebsite.com'}).then(() => {
139+
expect(instanceConfig.get('url')).to.equal('https://mywebsite.com');
140+
expect(saveStub.calledOnce).to.be.true;
141+
});
142+
});
143+
131144
it('throws config error if validate function is defined and doesn\'t return true', function () {
132145
const ConfigCommand = proxyquire(modulePath, {'./advanced': {
133146
url: {
@@ -183,7 +196,7 @@ describe('Unit: Command > Config', function () {
183196
});
184197

185198
it('returns url prompt with validator if url is not provided and db is sqlite3', function () {
186-
const expectedValidator = require('../../../lib/utils/validate-instance-url');
199+
const expectedValidator = require('../../../lib/utils/url').validate;
187200
const argv = {db: 'sqlite3'};
188201
const result = config.getConfigPrompts(argv);
189202

@@ -194,7 +207,7 @@ describe('Unit: Command > Config', function () {
194207
});
195208

196209
it('returns url prompt with correct defaults', function () {
197-
const expectedValidator = require('../../../lib/utils/validate-instance-url');
210+
const expectedValidator = require('../../../lib/utils/url').validate;
198211
const argv = {db: 'sqlite3', auto: true};
199212
const result = config.getConfigPrompts(argv);
200213

@@ -478,7 +491,7 @@ describe('Unit: Command > Config', function () {
478491
describe('advanced options', function () {
479492
it('url', function () {
480493
const advancedOptions = require(advancedModulePath);
481-
const expectedValidator = require('../../../lib/utils/validate-instance-url');
494+
const expectedValidator = require('../../../lib/utils/url').validate;
482495
expect(advancedOptions.url).to.exist;
483496

484497
// Check validate function

test/unit/utils/validate-instance-url.js

-31
This file was deleted.

0 commit comments

Comments
 (0)