From c19c18bd2fc7a06162f443ae81de3d3200940e76 Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 15:08:28 +0100 Subject: [PATCH 01/12] fix(bin): update password tool --- bin/password.js | 64 ++++++++++++++++++++++++++----------- test/suites/bin/password.js | 31 ++++++++++++++++++ 2 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 test/suites/bin/password.js diff --git a/bin/password.js b/bin/password.js index d092fd3de..7143f27c0 100755 --- a/bin/password.js +++ b/bin/password.js @@ -1,30 +1,58 @@ #!/usr/bin/env node /* eslint-disable no-console */ -const Redis = require('ioredis').Cluster; +const Redis = require('ioredis'); const assert = require('assert'); const conf = require('../lib/config'); +const { updatePassword } = require('../lib/actions/updatePassword'); const config = conf.get('/', { env: process.env.NODE_ENV }); -const redisConfig = config.redis; -const { updatePassword } = require('../lib/actions/updatePassword'); -const username = process.argv[2]; -const password = process.argv[3]; -assert(username, 'must provide id as argv[2]'); -assert(password, 'must provide password of token as argv[3]'); +const initRedis = (redisConfig) => { + const opts = { + lazyConnect: true, + ...redisConfig.options, + }; + if (redisConfig.sentinels) { + opts.sentinels = redisConfig.sentinels; + return new Redis(opts); + } -const redis = new Redis(redisConfig.hosts, ({ ...redisConfig.options, lazyConnect: true })); + return new Redis.Cluster(redisConfig.hosts, opts); +}; + +const redis = initRedis(config.redis); // connection options -redis - .connect() - .bind({ redis }) - .return([username, password]) - .spread(updatePassword) - .then(() => { - console.info('\nSet password for %s to "%s"\n', username, password); - return redis.disconnect(); - }) - .catch((err) => setImmediate(() => { throw err; })); +const main = async (username, password) => { + assert(username, 'must provide user id'); + assert(password, 'must provide password'); + + try { + await redis.connect(); + await updatePassword({ redis }, username, password); + } catch (err) { + setImmediate(() => { + throw err; + }); + } finally { + await redis.disconnect(); + } +}; + +if (module.parent === null) { + const username = process.argv[2]; + const password = process.argv[3]; + assert(username, 'must provide id as argv[2]'); + assert(password, 'must provide password of token as argv[3]'); + + // eslint-disable-next-line promise/catch-or-return + main(username, password) + .then(() => { + console.info('\nSet password for %s to "%s"\n', username, password); + return null; + }); +} + +export default main; diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js new file mode 100644 index 000000000..2f74873bb --- /dev/null +++ b/test/suites/bin/password.js @@ -0,0 +1,31 @@ +const assert = require('assert'); +const path = require('path'); +const exec = require('../../helpers/exec'); + +describe('binary: password', function suite() { + const binaryPath = path.resolve(__dirname, '../../../bin/password.js'); + const uid = 'test@test.me'; + const newPassword = 'trickynewpassword'; + + before(global.startService); + before(() => global.globalRegisterUser(uid, { + inactive: false, + })); + after(global.clearRedis); + + it('allows updating password from the command-line', async () => { + const stdout = await exec(`${binaryPath} ${uid} ${newPassword}`); + const lines = stdout.split('\n'); + const updated = lines.find((line) => ( + line.indexOf(uid) + )); + + assert(updated); + + // unset jwt token just in case + this.jwt = null; + // eslint-disable-next-line no-undef + await globalAuthUser(uid, newPassword); + assert(this.jwt); + }); +}); From 1b41778e16fc66606b92282f19a21fac736a3cb4 Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 15:30:17 +0100 Subject: [PATCH 02/12] chore: style --- bin/password.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/password.js b/bin/password.js index 7143f27c0..dbf0fd9a5 100755 --- a/bin/password.js +++ b/bin/password.js @@ -55,4 +55,4 @@ if (module.parent === null) { }); } -export default main; +exports.main = main; From 2894a9afc5184d69aab38af58a588aca606d2d5a Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 15:37:35 +0100 Subject: [PATCH 03/12] chore: style --- test/suites/bin/password.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index 2f74873bb..6b4a73e08 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -17,7 +17,7 @@ describe('binary: password', function suite() { const stdout = await exec(`${binaryPath} ${uid} ${newPassword}`); const lines = stdout.split('\n'); const updated = lines.find((line) => ( - line.indexOf(uid) + line.indexOf(uid) >= 0 )); assert(updated); From 6582ff7f3c6a233cb2a486f5b100862ba4fc0e59 Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 15:42:49 +0100 Subject: [PATCH 04/12] chore: style --- test/suites/bin/password.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index 6b4a73e08..1ef9cd049 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -20,6 +20,7 @@ describe('binary: password', function suite() { line.indexOf(uid) >= 0 )); + console.log('output:', lines); assert(updated); // unset jwt token just in case From 846f56b3269ce8f4e94fc192a658ac76163b34ef Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 15:50:58 +0100 Subject: [PATCH 05/12] chore: modify test case --- test/suites/bin/password.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index 1ef9cd049..8f60ea7f2 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -14,15 +14,9 @@ describe('binary: password', function suite() { after(global.clearRedis); it('allows updating password from the command-line', async () => { - const stdout = await exec(`${binaryPath} ${uid} ${newPassword}`); - const lines = stdout.split('\n'); - const updated = lines.find((line) => ( - line.indexOf(uid) >= 0 - )); - - console.log('output:', lines); - assert(updated); + await exec(`${binaryPath} ${uid} ${newPassword}`); + // Verify trying to log in with the new password // unset jwt token just in case this.jwt = null; // eslint-disable-next-line no-undef From 80ea40f9ba67ceb321e41665b7c14bba21ca2402 Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 15:58:25 +0100 Subject: [PATCH 06/12] chore: modify test case --- test/suites/bin/password.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index 8f60ea7f2..041b623ad 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -4,23 +4,27 @@ const exec = require('../../helpers/exec'); describe('binary: password', function suite() { const binaryPath = path.resolve(__dirname, '../../../bin/password.js'); - const uid = 'test@test.me'; + const username = 'test@test.me'; const newPassword = 'trickynewpassword'; before(global.startService); - before(() => global.globalRegisterUser(uid, { + before(() => global.globalRegisterUser(username, { inactive: false, })); after(global.clearRedis); it('allows updating password from the command-line', async () => { - await exec(`${binaryPath} ${uid} ${newPassword}`); + // eslint-disable-next-line no-undef + await globalAuthUser(username); + assert(this.userId); + + await exec(`${binaryPath} ${this.userId} ${newPassword}`); // Verify trying to log in with the new password // unset jwt token just in case this.jwt = null; // eslint-disable-next-line no-undef - await globalAuthUser(uid, newPassword); + await globalAuthUser(username, newPassword); assert(this.jwt); }); }); From 07e484ef2e658d58acb9ee85b521d9033871c0ec Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 16:12:44 +0100 Subject: [PATCH 07/12] chore: modify test case --- test/suites/bin/password.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index 041b623ad..eaa9de646 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -6,25 +6,23 @@ describe('binary: password', function suite() { const binaryPath = path.resolve(__dirname, '../../../bin/password.js'); const username = 'test@test.me'; const newPassword = 'trickynewpassword'; + const checkAuth = globalAuthUser(username, newPassword); before(global.startService); before(() => global.globalRegisterUser(username, { inactive: false, })); + // eslint-disable-next-line no-undef + before(globalAuthUser(username)); after(global.clearRedis); it('allows updating password from the command-line', async () => { - // eslint-disable-next-line no-undef - await globalAuthUser(username); - assert(this.userId); - await exec(`${binaryPath} ${this.userId} ${newPassword}`); - // Verify trying to log in with the new password // unset jwt token just in case this.jwt = null; // eslint-disable-next-line no-undef - await globalAuthUser(username, newPassword); + await checkAuth(); assert(this.jwt); }); }); From 419d765753b45829036f97f7febf616dcfcc92c1 Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Sun, 26 Jul 2020 16:12:54 +0100 Subject: [PATCH 08/12] chore: modify test case --- test/suites/bin/password.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index eaa9de646..f0168b328 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -6,6 +6,7 @@ describe('binary: password', function suite() { const binaryPath = path.resolve(__dirname, '../../../bin/password.js'); const username = 'test@test.me'; const newPassword = 'trickynewpassword'; + // eslint-disable-next-line no-undef const checkAuth = globalAuthUser(username, newPassword); before(global.startService); From 1c3c56d34c5b460193f318e5d6b8a6b77d2b2f2d Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Mon, 27 Jul 2020 10:53:44 +0100 Subject: [PATCH 09/12] chore: modify test case --- test/suites/bin/password.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index f0168b328..634267c84 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -10,7 +10,7 @@ describe('binary: password', function suite() { const checkAuth = globalAuthUser(username, newPassword); before(global.startService); - before(() => global.globalRegisterUser(username, { + before(global.globalRegisterUser(username, { inactive: false, })); // eslint-disable-next-line no-undef From 58753cd28a92caac0898e41def10fead12f18d92 Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Mon, 27 Jul 2020 11:03:34 +0100 Subject: [PATCH 10/12] chore: modify test case --- test/suites/bin/password.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index 634267c84..4a70b6588 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -23,7 +23,7 @@ describe('binary: password', function suite() { // unset jwt token just in case this.jwt = null; // eslint-disable-next-line no-undef - await checkAuth(); + await checkAuth.call(this); assert(this.jwt); }); }); From f077cd3a8279faaadbafec1009e06ec9905ed95e Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Mon, 27 Jul 2020 15:25:44 +0100 Subject: [PATCH 11/12] chore: modify test case --- test/suites/bin/password.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/suites/bin/password.js b/test/suites/bin/password.js index 4a70b6588..3467abba0 100644 --- a/test/suites/bin/password.js +++ b/test/suites/bin/password.js @@ -17,7 +17,7 @@ describe('binary: password', function suite() { before(globalAuthUser(username)); after(global.clearRedis); - it('allows updating password from the command-line', async () => { + it('allows updating password from the command-line', async function test() { await exec(`${binaryPath} ${this.userId} ${newPassword}`); // Verify trying to log in with the new password // unset jwt token just in case From 976d283600f73f05778a2686850016ee04bc83a8 Mon Sep 17 00:00:00 2001 From: Eugene Poyarkov Date: Mon, 27 Jul 2020 15:53:33 +0100 Subject: [PATCH 12/12] fix: misc --- bin/password.js | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/password.js b/bin/password.js index dbf0fd9a5..9bb4cffb5 100755 --- a/bin/password.js +++ b/bin/password.js @@ -15,6 +15,7 @@ const initRedis = (redisConfig) => { }; if (redisConfig.sentinels) { + opts.name = redisConfig.name; opts.sentinels = redisConfig.sentinels; return new Redis(opts); }