Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: config options like --port or --config are ignored #2113

Merged
merged 7 commits into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions Parse-Dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,41 @@ program.option('--createUser', 'helper tool to allow you to generate secure user
program.option('--createMFA', 'helper tool to allow you to generate multi-factor authentication secrets.');

program.parse(process.argv);
const options = program.opts();

for (const key in program) {
for (const key in options) {
const func = CLIHelper[key];
if (func && typeof func === 'function') {
func();
return;
}
}

const host = program.host || process.env.HOST || '0.0.0.0';
const port = program.port || process.env.PORT || 4040;
const mountPath = program.mountPath || process.env.MOUNT_PATH || '/';
const allowInsecureHTTP = program.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;
const cookieSessionSecret = program.cookieSessionSecret || process.env.PARSE_DASHBOARD_COOKIE_SESSION_SECRET;
const trustProxy = program.trustProxy || process.env.PARSE_DASHBOARD_TRUST_PROXY;
const dev = program.dev;
const host = options.host || process.env.HOST || '0.0.0.0';
const port = options.port || process.env.PORT || 4040;
const mountPath = options.mountPath || process.env.MOUNT_PATH || '/';
const allowInsecureHTTP = options.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;
const cookieSessionSecret = options.cookieSessionSecret || process.env.PARSE_DASHBOARD_COOKIE_SESSION_SECRET;
const trustProxy = options.trustProxy || process.env.PARSE_DASHBOARD_TRUST_PROXY;
const dev = options.dev;

if (trustProxy && allowInsecureHTTP) {
console.log('Set only trustProxy *or* allowInsecureHTTP, not both. Only one is needed to handle being behind a proxy.');
process.exit(-1);
}

let explicitConfigFileProvided = !!program.config;
let explicitConfigFileProvided = !!options.config;
let configFile = null;
let configFromCLI = null;
let configServerURL = program.serverURL || process.env.PARSE_DASHBOARD_SERVER_URL;
let configGraphQLServerURL = program.graphQLServerURL || process.env.PARSE_DASHBOARD_GRAPHQL_SERVER_URL;
let configMasterKey = program.masterKey || process.env.PARSE_DASHBOARD_MASTER_KEY;
let configAppId = program.appId || process.env.PARSE_DASHBOARD_APP_ID;
let configAppName = program.appName || process.env.PARSE_DASHBOARD_APP_NAME;
let configUserId = program.userId || process.env.PARSE_DASHBOARD_USER_ID;
let configUserPassword = program.userPassword || process.env.PARSE_DASHBOARD_USER_PASSWORD;
let configSSLKey = program.sslKey || process.env.PARSE_DASHBOARD_SSL_KEY;
let configSSLCert = program.sslCert || process.env.PARSE_DASHBOARD_SSL_CERT;
let configServerURL = options.serverURL || process.env.PARSE_DASHBOARD_SERVER_URL;
let configGraphQLServerURL = options.graphQLServerURL || process.env.PARSE_DASHBOARD_GRAPHQL_SERVER_URL;
let configMasterKey = options.masterKey || process.env.PARSE_DASHBOARD_MASTER_KEY;
let configAppId = options.appId || process.env.PARSE_DASHBOARD_APP_ID;
let configAppName = options.appName || process.env.PARSE_DASHBOARD_APP_NAME;
let configUserId = options.userId || process.env.PARSE_DASHBOARD_USER_ID;
let configUserPassword = options.userPassword || process.env.PARSE_DASHBOARD_USER_PASSWORD;
let configSSLKey = options.sslKey || process.env.PARSE_DASHBOARD_SSL_KEY;
let configSSLCert = options.sslCert || process.env.PARSE_DASHBOARD_SSL_CERT;

function handleSIGs(server) {
const signals = {
Expand All @@ -86,7 +87,7 @@ function handleSIGs(server) {
});
}

if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
if (!options.config && !process.env.PARSE_DASHBOARD_CONFIG) {
if (configServerURL && configMasterKey && configAppId) {
configFromCLI = {
data: {
Expand Down Expand Up @@ -114,13 +115,13 @@ if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
} else if (!configServerURL && !configMasterKey && !configAppName) {
configFile = path.join(__dirname, 'parse-dashboard-config.json');
}
} else if (!program.config && process.env.PARSE_DASHBOARD_CONFIG) {
} else if (!options.config && process.env.PARSE_DASHBOARD_CONFIG) {
configFromCLI = {
data: JSON.parse(process.env.PARSE_DASHBOARD_CONFIG)
};
} else {
configFile = program.config;
if (program.appId || program.serverURL || program.masterKey || program.appName || program.graphQLServerURL) {
configFile = options.config;
if (options.appId || options.serverURL || options.masterKey || options.appName || options.graphQLServerURL) {
console.log('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
process.exit(3);
}
Expand Down
8 changes: 8 additions & 0 deletions Parse-Dashboard/tests/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
bugra9 marked this conversation as resolved.
Show resolved Hide resolved
"plugins": [
"jest"
],
"env": {
"jest/globals": true
}
}
66 changes: 66 additions & 0 deletions Parse-Dashboard/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const path = require('path');
const spawn = require('child_process').spawn;

const TIMEOUT = 1000; // ms

describe('port, config, appId, serverURL, masterKey, appName, graphQLServerURL options should not be ignored.', () => {
it('Should start with port 4041.', async () => {
const result = await startParseDashboardAndGetOutput(['--port', '4041']);

expect(result).toContain('The dashboard is now available at http://0.0.0.0:4041/');
});

it('Should return an error message if config and appId options are provided together.', async () => {
const result = await startParseDashboardAndGetOutput(['--config', 'helloworld', '--appId', 'helloworld']);

expect(result).toContain('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
});

it('Should return an error message if config and serverURL options are provided together.', async () => {
const result = await startParseDashboardAndGetOutput(['--config', 'helloworld', '--serverURL', 'helloworld']);

expect(result).toContain('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
});

it('Should return an error message if config and masterKey options are provided together.', async () => {
const result = await startParseDashboardAndGetOutput(['--config', 'helloworld', '--masterKey', 'helloworld']);

expect(result).toContain('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
});

it('Should return an error message if config and appName options are provided together.', async () => {
const result = await startParseDashboardAndGetOutput(['--config', 'helloworld', '--appName', 'helloworld']);

expect(result).toContain('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
});

it('Should return an error message if config and graphQLServerURL options are provided together.', async () => {
const result = await startParseDashboardAndGetOutput(['--config', 'helloworld', '--graphQLServerURL', 'helloworld']);

expect(result).toContain('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
});
});

function startParseDashboardAndGetOutput(args) {
return new Promise((resolve) => {
const indexFilePath = path.resolve('./Parse-Dashboard/index.js');
const child = spawn('node', [indexFilePath, ...args], { cwd: '.', timeout: TIMEOUT });
setTimeout(() => { child.kill(); }, TIMEOUT); // node.js 12 hack (spawn timeout option is not supported.)

let output = '';
child.on('error', () => { resolve(output); });
child.on('close', () => { resolve(output); });

if (child.stdout) {
child.stdout.on('data', data => {
output += `STDOUT: ${data}\n`;
});
}

if (child.stderr) {
child.stderr.on('data', data => {
output += `STDERROR: ${data}\n`;
});
}
});
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
"main": "Parse-Dashboard/app.js",
"jest": {
"roots": [
"src/lib"
"src/lib",
"Parse-Dashboard"
bugra9 marked this conversation as resolved.
Show resolved Hide resolved
],
"transform": {
".*": "<rootDir>/testing/preprocessor.js"
Expand Down