Skip to content

Commit

Permalink
feat: throw if config.keys not exists when access app.keys (eggjs#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Feb 24, 2017
1 parent 234ceac commit 4eaf3fb
Show file tree
Hide file tree
Showing 48 changed files with 150 additions and 10 deletions.
9 changes: 4 additions & 5 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,11 @@ class Application extends EggApplication {
if (!this[KEYS]) {
if (!this.config.keys) {
if (this.config.env === 'local' || this.config.env === 'unittest') {
console.warn('Please set config.keys first, now using mock keys for dev env (%s)',
this.config.baseDir);
this.config.keys = 'foo, keys, you need to set your app keys';
} else {
throw new Error('Please set config.keys first');
const configPath = path.join(this.config.baseDir, 'config/config.default.js');
console.error('Cookie need secret key to sign and encrypt.');
console.error('Please add `config.keys` in %s', configPath);
}
throw new Error('Please set config.keys first');
}

this[KEYS] = this.config.keys.split(',').map(s => s.trim());
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/apps/agent-app-sync/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/agent-app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/agent-client-app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/agent-die/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/agent-restart/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/aliyun-egg/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-router/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-throw/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
1 change: 1 addition & 0 deletions test/fixtures/apps/custom-logger/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = info => {
formatter: meta => meta.message,
},
},
keys: 'test key',
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports.httpclient = {
enableDNSCache: true,
};

exports.keys = 'test key';
2 changes: 2 additions & 0 deletions test/fixtures/apps/dumpconfig/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exports.dynamic = 0;

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/empty/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
2 changes: 2 additions & 0 deletions test/fixtures/apps/get-logger/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ module.exports = appInfo => {
file: path.join(appInfo.baseDir, 'logs', appInfo.name, 'a.log'),
},
},

keys: 'secret key',
};
};
2 changes: 2 additions & 0 deletions test/fixtures/apps/helper/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ exports.helpers = {
}
},
};

exports.keys = 'test key';
2 changes: 2 additions & 0 deletions test/fixtures/apps/i18n/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports.view = {
defaultViewEngine: 'nunjucks',
};

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/loader-plugin-dep/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
2 changes: 2 additions & 0 deletions test/fixtures/apps/loader-plugin/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
exports.plugin = 'override plugin';

exports.middleware = [];

exports.keys = 'test key';
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports.logger = {
level: 'DEBUG',
};

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/logger-reload/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
1 change: 1 addition & 0 deletions test/fixtures/apps/logger/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = info => {
file: path.join(info.baseDir, 'logs/custom.log'),
},
},
keys: 'test key',
};
};
2 changes: 2 additions & 0 deletions test/fixtures/apps/logrotator-app/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ exports.logrotator = {
maxFiles: 2,
rotateDuration: 30000
};

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/messenger-random/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/messenger/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ module.exports = appInfo => {
'.html': 'html',
},
},

keys: 'test key',
}
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/nobuffer-logger/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/notready/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/reload-worker/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/schedule/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/service-app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
2 changes: 2 additions & 0 deletions test/fixtures/apps/singleton-demo/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ exports.dataService = {
foo: 'bar',
}
};

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/subdir-services/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
2 changes: 2 additions & 0 deletions test/fixtures/apps/userservice/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ exports.userservice = {
},
},
};

exports.keys = 'test key';
2 changes: 2 additions & 0 deletions test/fixtures/apps/view-render/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ exports.security = {
exports.view = {
defaultViewEngine: 'nunjucks',
};

exports.keys = 'test key';
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ exports.env = 'local';
exports.watcher = {
type: 'development',
};

exports.keys = 'test key';
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports.watcher = {
type: 'default',
};

exports.keys = 'test key';
3 changes: 3 additions & 0 deletions test/fixtures/apps/worker-die/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'test key';
31 changes: 27 additions & 4 deletions test/lib/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('test/lib/application.test.js', () => {
mm.env('test');
app = utils.app('apps/keys-missing');
yield app.ready();
mm(app.config, 'keys', null);

try {
app.keys;
Expand All @@ -65,15 +66,37 @@ describe('test/lib/application.test.js', () => {
yield app.close();
});

it('should auto set keys on unittest', function* () {
it('should throw when config.keys missing on unittest env', function* () {
mm.env('unittest');
app = utils.app('apps/keys-missing');
yield app.ready();
mm(app.config, 'keys', null);

assert(app.keys);
assert(app.keys);
assert(app.config.keys === 'foo, keys, you need to set your app keys');
try {
app.keys;
throw new Error('should not run this');
} catch (err) {
assert(err.message === 'Please set config.keys first');
}

// make sure app close
yield app.close();
});

it('should throw when config.keys missing on local env', function* () {
mm.env('local');
app = utils.app('apps/keys-missing');
yield app.ready();
mm(app.config, 'keys', null);

try {
app.keys;
throw new Error('should not run this');
} catch (err) {
assert(err.message === 'Please set config.keys first');
}

// make sure app close
yield app.close();
});

Expand Down
3 changes: 2 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const eggPath = path.join(__dirname, '..');

exports.app = (name, options) => {
options = formatOptions(name, options);
return mm.app(options);
const app = mm.app(options);
return app;
};

/**
Expand Down

0 comments on commit 4eaf3fb

Please sign in to comment.