Skip to content

Commit 5e7d80b

Browse files
committed
chore: update tests, disable reconfiguring service after start
1 parent 6c58157 commit 5e7d80b

File tree

21 files changed

+308
-343
lines changed

21 files changed

+308
-343
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"flake-idgen": "^1.1.0",
4444
"get-stdin": "^7.0.0",
4545
"get-value": "^3.0.1",
46-
"handlebars": "^4.3.2",
46+
"handlebars": "^4.3.3",
4747
"ioredis": "^4.14.1",
4848
"is": "^3.3.0",
4949
"jsonwebtoken": "^8.5.1",

rfcs/password_validation_limits.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Enabling password validator is Breaking change and disabled by default.
2020
In the nearest future, when everything will be ready, a validator must be enabled.
2121

2222
## Validator Keyword
23-
Service validation algorithm based on the AJV validator and all validation requirements are inside schemas. All incoming requests checked. We can add additional custom validator `password` keyword for the `password` field, which performs required checks.
23+
Service validation algorithm based on the AJV validator and all validation requirements are inside schemas. All incoming requests checked. We can add additional custom validator `password` keyword for the `password` field, which performs required checks.
2424

2525
Current schema:
2626

@@ -51,7 +51,7 @@ The parameter accepts the required strength in the range [0-4] that describes th
5151

5252
According to the registration process, some passwords generated by service, so custom validator should omit password check if `skipPassword` request param provided.
5353

54-
When executed:
54+
When executed:
5555
- checks username does not match provided password
5656
- calls `zxcvbn` to obtain a complexity of passwords and returns whether the given password matches policy
5757

@@ -63,9 +63,9 @@ Also, we can pass user-provided data into `zxcvbn` to check whether some sensiti
6363
const config = {
6464
enabled: false,
6565
minStrength: 3, // Desired strength
66-
forceCheckFieldName: 'checkPassword', // force enable password to check if the object field value set..
66+
forceCheckFieldName: ['checkPassword'], // force enable password to check if the object field value set..
6767
inputFieldNames: [ // Linked fields list, allow filter case sensitive data in the password from the parent object.
68-
'username',
68+
'username',
6969
'otherfield'
7070
]
7171
}

schemas/config.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,17 @@
291291
},
292292
"minStrength": {
293293
"type": "integer",
294-
"default" : 4
294+
"default" : 4,
295+
"minimum": 0,
296+
"maximum": 4
295297
},
296298
"forceCheckFieldName": {
297-
"type": "string"
299+
"type": "array",
300+
"items": {
301+
"type": "string",
302+
"minLength": 1
303+
},
304+
"default": []
298305
},
299306
"inputFieldNames": {
300307
"type": "array",

src/configs/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ exports.logger = {
7676
exports.passwordValidator = {
7777
enabled: false,
7878
minStrength: 4, // 0..4
79-
forceCheckFieldName: 'checkPassword',
79+
forceCheckFieldName: ['checkPassword'],
8080
inputFieldNames: [
8181
'username',
8282
],

src/users.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const RedisCluster = require('ioredis').Cluster;
99
const Flakeless = require('ms-flakeless');
1010
const conf = require('./config');
1111
const get = require('./utils/get-value');
12+
const attachPasswordKeyword = require('./utils/passwordValidator.js');
1213

1314
/**
1415
* @namespace Users
@@ -125,9 +126,8 @@ module.exports = class Users extends Microfleet {
125126
return this.dlock;
126127
});
127128

128-
this.addConnector(ConnectorsTypes.application, () => {
129-
const pValidator = require('./utils/passwordValidator.js');
130-
pValidator(this);
129+
this.addConnector(ConnectorsTypes.essential, () => {
130+
attachPasswordKeyword(this);
131131
});
132132

133133
// init account seed

src/utils/passwordValidator.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
const zxcvbn = require('zxcvbn');
22
const assert = require('assert');
3-
const {
4-
NotFoundError,
5-
} = require('common-errors');
3+
const { NotFoundError } = require('common-errors');
64

75
const regKeyword = 'password';
8-
9-
const has = Object.prototype.hasOwnProperty;
6+
const { hasOwnProperty } = Object.prototype;
7+
const { isArray } = Array;
108

119
/**
1210
* Generates readable error including some Suggestions and Reasons
@@ -22,7 +20,8 @@ function validatorError(result, dataPath) {
2220
if (warning !== '') {
2321
message = `${message}. ${warning}`;
2422
}
25-
if (suggestions instanceof Array && suggestions.length > 0) {
23+
24+
if (isArray(suggestions) && suggestions.length > 0) {
2625
message = `${message}. ${suggestions.join(' ')}`;
2726
}
2827
}
@@ -35,21 +34,29 @@ function validatorError(result, dataPath) {
3534
* @param {validatorConfig} config
3635
*/
3736
function getValidatorFn(config) {
38-
const { forceCheckFieldName, inputFieldNames } = config;
37+
// service may not have its config changed after start
38+
const { forceCheckFieldName, inputFieldNames, minStrength, enabled } = config;
3939

4040
return function validate(schema, data, parentSchema, currentPath, parentObject) {
41-
const { minStrength, enabled } = config;
42-
const forceValidate = has.call(parentObject, forceCheckFieldName);
43-
const validatorEnabled = forceValidate || enabled;
41+
let forceValidate;
42+
if (Array.isArray(forceCheckFieldName) && forceCheckFieldName.length > 0) {
43+
forceValidate = forceCheckFieldName.reduce((prev, fieldName) => {
44+
if (prev) return prev;
45+
if (parentObject[fieldName]) return true;
46+
return false;
47+
}, false);
48+
}
4449

50+
const validatorEnabled = forceValidate || enabled;
4551
if (!validatorEnabled) {
4652
return true;
4753
}
4854

4955
const userInput = inputFieldNames.reduce((prev, fieldName) => {
50-
if (has.call(parentObject, fieldName)) {
51-
return [...prev, parentObject[fieldName]];
56+
if (hasOwnProperty.call(parentObject, fieldName)) {
57+
prev.push(parentObject[fieldName]);
5258
}
59+
5360
return prev;
5461
}, []);
5562

test/helpers/organization.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const faker = require('faker');
22
const Promise = require('bluebird');
33
const times = require('lodash/times');
4-
const { inspectPromise } = require('@makeomatic/deploy');
54

65
async function createMembers(totalUsers = 1, register = false) {
76
this.userNames = [];
@@ -36,9 +35,8 @@ exports.createOrganization = async function createOrganization(customOpts = {},
3635
members: this.userNames.slice(0, totalUsers),
3736
...customOpts,
3837
};
39-
const organization = await this.dispatch('users.organization.create', params)
40-
.reflect()
41-
.then(inspectPromise(true));
38+
39+
const organization = await this.dispatch('users.organization.create', params);
4240

4341
this.organization = {
4442
...organization.data.attributes,

test/suites/organization/create.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,14 @@ describe('#create organization', function registerSuite() {
3838
members: this.userNames.slice(0, 2),
3939
};
4040

41-
await this.dispatch('users.organization.create', params)
42-
.reflect()
43-
.then(inspectPromise(true))
44-
.then((response) => {
45-
const createdOrganization = response.data.attributes;
46-
assert(createdOrganization.name === params.name);
47-
assert(createdOrganization.metadata.description === params.metadata.description);
48-
assert(createdOrganization.members.length === 1);
49-
assert.ok(createdOrganization.id);
50-
assert(createdOrganization.active === false);
51-
});
41+
const response = await this.dispatch('users.organization.create', params);
42+
43+
const createdOrganization = response.data.attributes;
44+
assert(createdOrganization.name === params.name);
45+
assert(createdOrganization.metadata.description === params.metadata.description);
46+
assert(createdOrganization.members.length === 1);
47+
assert.ok(createdOrganization.id);
48+
assert(createdOrganization.active === false);
5249

5350
const [registeredMember] = await sendInviteMailSpy.returnValues[0];
5451
const registeredMemberPassword = generatePasswordSpy.firstCall.args[0];

test/suites/organization/get.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ describe('#get organization', function registerSuite() {
2626
it('must be able to get organization', async function test() {
2727
const { invites, ...organization } = this.organization;
2828
return this.dispatch('users.organization.get', { organizationId: this.organization.id })
29-
.reflect()
30-
.then(inspectPromise(true))
3129
.then((response) => {
3230
assert.deepEqual(response.data.attributes, organization);
3331
});

test/suites/organization/getMetadata.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ describe('#organization members metadata', function registerSuite() {
2929
};
3030

3131
return this.dispatch('users.organization.getMetadata', opts)
32-
.reflect()
33-
.then(inspectPromise(true))
3432
.then((response) => {
3533
assert.ok(response.data.attributes);
3634
assert.deepEqual(response.data.attributes, this.organization.metadata);

0 commit comments

Comments
 (0)