Skip to content

Commit 893372e

Browse files
committed
SDK-2359 Digital Identity - support wanted attributes extra options (#510)
* Added in digital_identity_service the wanted attribute extra options (alternative name, optional) * Added tests for WantedAttributeBuilder methods .withAlternativeName(), .withAlternativeNames() and .withOptional() * Added extra cases
1 parent 713f125 commit 893372e

File tree

5 files changed

+172
-3
lines changed

5 files changed

+172
-3
lines changed

src/digital_identity_service/policy/wanted.attribute.builder.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@ module.exports = class WantedAttributeBuilder {
4545
return this;
4646
}
4747

48+
/**
49+
* @param {string} alternativeName
50+
* @returns this
51+
*/
52+
withAlternativeName(alternativeName) {
53+
this.alternativeNames = [...(this.alternativeNames || []), alternativeName];
54+
return this;
55+
}
56+
57+
/**
58+
* @param {string[]} alternativeNames
59+
* @returns this
60+
*/
61+
withAlternativeNames(alternativeNames) {
62+
this.alternativeNames = alternativeNames;
63+
return this;
64+
}
65+
66+
/**
67+
* @param {boolean} [optional=true]
68+
* @returns this
69+
*/
70+
withOptional(optional = true) {
71+
this.optional = optional;
72+
return this;
73+
}
74+
4875
/**
4976
* @returns {WantedAttribute}
5077
*/
@@ -53,7 +80,9 @@ module.exports = class WantedAttributeBuilder {
5380
this.name,
5481
this.derivation,
5582
this.acceptSelfAsserted,
56-
this.constraints
83+
this.constraints,
84+
this.alternativeNames,
85+
this.optional
5786
);
5887
}
5988
};

src/digital_identity_service/policy/wanted.attribute.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ module.exports = class WantedAttribute {
1414
* @param {string|null} derivation
1515
* @param {boolean|null} acceptSelfAsserted
1616
* @param {Constraints|null} constraints
17+
* @param {string[]|null} alternativeNames
18+
* @param {boolean|null} optional
1719
*/
18-
constructor(name, derivation = null, acceptSelfAsserted = null, constraints = null) {
20+
// eslint-disable-next-line max-len
21+
constructor(name, derivation = null, acceptSelfAsserted = null, constraints = null, alternativeNames = null, optional = null) {
1922
Validation.isString(name, 'name');
2023
Validation.notNullOrEmpty(name, 'name');
2124
/** @private */
@@ -38,6 +41,18 @@ module.exports = class WantedAttribute {
3841
}
3942
/** @private */
4043
this.constraints = constraints;
44+
45+
if (alternativeNames !== null) {
46+
Validation.isArrayOfStrings(alternativeNames, 'alternativeNames');
47+
}
48+
/** @private */
49+
this.alternativeNames = alternativeNames;
50+
51+
if (optional !== null) {
52+
Validation.isBoolean(optional, 'optional');
53+
}
54+
/** @private */
55+
this.optional = optional;
4156
}
4257

4358
/**
@@ -81,6 +96,26 @@ module.exports = class WantedAttribute {
8196
return this.acceptSelfAsserted;
8297
}
8398

99+
/**
100+
* Accept alternative names.
101+
*
102+
* These are names of attributes that can be used as fallback
103+
*
104+
* @returns {string[]}
105+
*/
106+
getAlternativeNames() {
107+
return this.alternativeNames;
108+
}
109+
110+
/**
111+
* Whether the attribute is wanted optionally
112+
*
113+
* @returns {boolean}
114+
*/
115+
getOptional() {
116+
return this.optional;
117+
}
118+
84119
/**
85120
* Returns serialized data for JSON.stringify()
86121
*/
@@ -102,6 +137,14 @@ module.exports = class WantedAttribute {
102137
json.accept_self_asserted = this.getAcceptSelfAsserted();
103138
}
104139

140+
if (this.getAlternativeNames() !== null) {
141+
json.alternative_names = this.getAlternativeNames();
142+
}
143+
144+
if (this.getOptional() !== null) {
145+
json.optional = this.getOptional();
146+
}
147+
105148
return json;
106149
}
107150
};

tests/digital_identity_service/policy/wanted.attribute.builder.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,64 @@ describe('WantedAttributeBuilder', () => {
131131
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
132132
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
133133
});
134+
135+
it('should build a wanted attribute with alternative name', () => {
136+
const wantedAttribute = new WantedAttributeBuilder()
137+
.withName(TEST_NAME)
138+
.withAlternativeName(`alt-${TEST_NAME}`)
139+
.build();
140+
141+
const expectedJson = JSON.stringify({
142+
name: TEST_NAME,
143+
optional: false,
144+
alternative_names: [`alt-${TEST_NAME}`],
145+
});
146+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
147+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
148+
});
149+
150+
it('should build a wanted attribute with alternative names', () => {
151+
const wantedAttribute = new WantedAttributeBuilder()
152+
.withName(TEST_NAME)
153+
.withAlternativeNames([`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`])
154+
.build();
155+
156+
const expectedJson = JSON.stringify({
157+
name: TEST_NAME,
158+
optional: false,
159+
alternative_names: [`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`],
160+
});
161+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
162+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
163+
});
164+
165+
it('should build a wanted attribute with alternative names using both methods', () => {
166+
const wantedAttribute = new WantedAttributeBuilder()
167+
.withName(TEST_NAME)
168+
.withAlternativeNames([`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`])
169+
.withAlternativeName(`${TEST_NAME}-extra`)
170+
.build();
171+
172+
const expectedJson = JSON.stringify({
173+
name: TEST_NAME,
174+
optional: false,
175+
alternative_names: [`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`, `${TEST_NAME}-extra`],
176+
});
177+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
178+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
179+
});
180+
181+
it('should build a wanted attribute with optional behaviour', () => {
182+
const wantedAttribute = new WantedAttributeBuilder()
183+
.withName(TEST_NAME)
184+
.withOptional(true)
185+
.build();
186+
187+
const expectedJson = JSON.stringify({
188+
name: TEST_NAME,
189+
optional: true,
190+
});
191+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
192+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
193+
});
134194
});

types/src/digital_identity_service/policy/wanted.attribute.builder.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ declare class WantedAttributeBuilder {
2525
*/
2626
withAcceptSelfAsserted(acceptSelfAsserted?: boolean): this;
2727
acceptSelfAsserted: boolean;
28+
/**
29+
* @param {string} alternativeName
30+
* @returns this
31+
*/
32+
withAlternativeName(alternativeName: string): this;
33+
alternativeNames: any;
34+
/**
35+
* @param {string[]} alternativeNames
36+
* @returns this
37+
*/
38+
withAlternativeNames(alternativeNames: string[]): this;
39+
/**
40+
* @param {boolean} [optional=true]
41+
* @returns this
42+
*/
43+
withOptional(optional?: boolean): this;
44+
optional: boolean;
2845
/**
2946
* @returns {WantedAttribute}
3047
*/

types/src/digital_identity_service/policy/wanted.attribute.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ declare class WantedAttribute {
55
* @param {string|null} derivation
66
* @param {boolean|null} acceptSelfAsserted
77
* @param {Constraints|null} constraints
8+
* @param {string[]|null} alternativeNames
9+
* @param {boolean|null} optional
810
*/
9-
constructor(name: string, derivation?: string | null, acceptSelfAsserted?: boolean | null, constraints?: Constraints | null);
11+
constructor(name: string, derivation?: string | null, acceptSelfAsserted?: boolean | null, constraints?: Constraints | null, alternativeNames?: string[] | null, optional?: boolean | null);
1012
/** @private */
1113
private name;
1214
/** @private */
@@ -15,6 +17,10 @@ declare class WantedAttribute {
1517
private acceptSelfAsserted;
1618
/** @private */
1719
private constraints;
20+
/** @private */
21+
private alternativeNames;
22+
/** @private */
23+
private optional;
1824
/**
1925
* Name identifying the WantedAttribute
2026
*
@@ -44,6 +50,20 @@ declare class WantedAttribute {
4450
* @returns {boolean}
4551
*/
4652
getAcceptSelfAsserted(): boolean;
53+
/**
54+
* Accept alternative names.
55+
*
56+
* These are names of attributes that can be used as fallback
57+
*
58+
* @returns {string[]}
59+
*/
60+
getAlternativeNames(): string[];
61+
/**
62+
* Whether the attribute is wanted optionally
63+
*
64+
* @returns {boolean}
65+
*/
66+
getOptional(): boolean;
4767
/**
4868
* Returns serialized data for JSON.stringify()
4969
*/

0 commit comments

Comments
 (0)