Skip to content

Commit

Permalink
SDK-1096: Update DynamicPolicy and DynamicPolicyBuilder to re-hide th…
Browse files Browse the repository at this point in the history
…e internal constants, as well as refactor the withAuth functions
  • Loading branch information
MrBurtyyy authored and davidgrayston committed Aug 6, 2019
1 parent c08e2cb commit 8993c3c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 48 deletions.
37 changes: 11 additions & 26 deletions src/dynamic_sharing_service/policy/dynamic.policy.builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ const WantedAttribute = require('./wanted.attribute');
const constants = require('../../yoti_common/constants');
const Validation = require('../../yoti_common/validation');

/**
* Remove all matching elements from an array.
* @param {number} element
* @param {Array} arr
*/
const removeMatchingElements = (element, arr) => arr.filter(value => value !== element);
const SELFIE_AUTH_TYPE = 1;
const PIN_AUTH_TYPE = 2;

/**
* Builder for DynamicPolicy.
Expand Down Expand Up @@ -124,37 +120,26 @@ module.exports = class DynamicPolicyBuilder {
* @param {boolean} enabled
*/
withSelfieAuthentication(enabled = true) {
if (enabled) {
return this.withWantedAuthType(DynamicPolicy.SELFIE_AUTH_TYPE);
}

this.wantedAuthTypes = removeMatchingElements(
DynamicPolicy.SELFIE_AUTH_TYPE,
this.wantedAuthTypes
);
return this;
return this.withWantedAuthType(SELFIE_AUTH_TYPE, enabled);
}

/**
* @param {boolean} enabled
*/
withPinAuthentication(enabled = true) {
if (enabled) {
return this.withWantedAuthType(DynamicPolicy.PIN_AUTH_TYPE);
}

this.wantedAuthTypes = removeMatchingElements(
DynamicPolicy.PIN_AUTH_TYPE,
this.wantedAuthTypes
);
return this;
return this.withWantedAuthType(PIN_AUTH_TYPE, enabled);
}

/**
* @param {integer} wantedAuthType
*/
withWantedAuthType(wantedAuthType) {
this.wantedAuthTypes.push(wantedAuthType);
withWantedAuthType(wantedAuthType, enabled = true) {
if (enabled) {
this.wantedAuthTypes.push(wantedAuthType);
} else {
this.wantedAuthTypes = this.wantedAuthTypes.filter(value => value !== wantedAuthType);
}

return this;
}

Expand Down
14 changes: 0 additions & 14 deletions src/dynamic_sharing_service/policy/dynamic.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ module.exports = class DynamicPolicy {
this.wantedRememberMe = wantedRememberMe;
}

/**
* Constant for selfie authorisation type.
*/
static get SELFIE_AUTH_TYPE() {
return 1;
}

/**
* Constant for pin authorisation type.
*/
static get PIN_AUTH_TYPE() {
return 2;
}

/**
* @returns {WantedAttribute[]} array of attributes to be requested.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const expectDynamicPolicyAttributes = (dynamicPolicy, expectedWantedAttributeDat
};

describe('DynamicPolicyBuilder', () => {
const EXPECTED_SELFIE_AUTH_TYPE = 1;
const EXPECTED_PIN_AUTH_TYPE = 2;

it('should build with attributes', () => {
const dynamicPolicy = new DynamicPolicyBuilder()
.withFamilyName()
Expand Down Expand Up @@ -210,7 +213,7 @@ describe('DynamicPolicyBuilder', () => {
.withWantedAuthType(99)
.build();

const expectedAuthTypes = [DynamicPolicy.SELFIE_AUTH_TYPE, DynamicPolicy.PIN_AUTH_TYPE, 99];
const expectedAuthTypes = [EXPECTED_SELFIE_AUTH_TYPE, EXPECTED_PIN_AUTH_TYPE, 99];

expect(dynamicPolicy.getWantedAuthTypes()).to.deep.equal(expectedAuthTypes);

Expand All @@ -229,7 +232,7 @@ describe('DynamicPolicyBuilder', () => {
.withWantedAuthType(99)
.build();

const expectedAuthTypes = [DynamicPolicy.SELFIE_AUTH_TYPE, DynamicPolicy.PIN_AUTH_TYPE, 99];
const expectedAuthTypes = [EXPECTED_SELFIE_AUTH_TYPE, EXPECTED_PIN_AUTH_TYPE, 99];

expect(dynamicPolicy.getWantedAuthTypes()).to.deep.equal(expectedAuthTypes);

Expand Down Expand Up @@ -283,8 +286,8 @@ describe('DynamicPolicyBuilder', () => {
.build();

const authTypes = dynamicPolicy.getWantedAuthTypes();
expect(authTypes).to.not.contain(DynamicPolicy.SELFIE_AUTH_TYPE);
expect(authTypes).to.not.contain(DynamicPolicy.PIN_AUTH_TYPE);
expect(authTypes).to.not.contain(EXPECTED_SELFIE_AUTH_TYPE);
expect(authTypes).to.not.contain(EXPECTED_PIN_AUTH_TYPE);
});

it('should build with no more than one auth type', () => {
Expand All @@ -295,7 +298,7 @@ describe('DynamicPolicyBuilder', () => {
.build();

const authTypesLength = dynamicPolicy.getWantedAuthTypes().length;
expect(authTypesLength).to.equal(DynamicPolicy.SELFIE_AUTH_TYPE);
expect(authTypesLength).to.equal(1);
});

it('should build with only two auth types', () => {
Expand All @@ -305,7 +308,7 @@ describe('DynamicPolicyBuilder', () => {
.build();

const authTypesLength = dynamicPolicy.getWantedAuthTypes().length;
expect(authTypesLength).to.equal(DynamicPolicy.PIN_AUTH_TYPE);
expect(authTypesLength).to.equal(2);
});

it('should build with no selfie authentication after having it added then removed', () => {
Expand All @@ -314,7 +317,7 @@ describe('DynamicPolicyBuilder', () => {
.withSelfieAuthentication(false)
.build();

expect(dynamicPolicy.wantedAuthTypes).to.not.contain(DynamicPolicy.SELFIE_AUTH_TYPE);
expect(dynamicPolicy.wantedAuthTypes).to.not.contain(EXPECTED_SELFIE_AUTH_TYPE);
});

it('should build with no pin authentication after having it added then removed', () => {
Expand All @@ -323,6 +326,6 @@ describe('DynamicPolicyBuilder', () => {
.withPinAuthentication(false)
.build();

expect(dynamicPolicy.wantedAuthTypes).to.not.contain(DynamicPolicy.PIN_AUTH_TYPE);
expect(dynamicPolicy.wantedAuthTypes).to.not.contain(EXPECTED_PIN_AUTH_TYPE);
});
});

0 comments on commit 8993c3c

Please sign in to comment.