Skip to content

Commit a03f99a

Browse files
committed
Continued covergae
1 parent 8a7dca3 commit a03f99a

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/HDSModel.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ module.exports = HDSModel;
224224
function authorizationOverride (level1, level2) {
225225
if (level1 === level2) return true;
226226
if (level1 === 'manage') return true;
227+
if (level1 === 'contribute' && level2 !== 'manage') return true;
227228
return false;
228229
}
229230

@@ -240,7 +241,7 @@ function mixAuthorizationLevels (level1, level2) {
240241
if (levels.includes('manage')) return 'manage'; // any & manage
241242
if (levels[0] === 'contribute') return 'contribute'; // read ore writeOnly & contribute
242243
if (levels[1] === 'writeOnly') return 'contribute'; // mix read & writeOnly
243-
// error if there .. 'read' & 'read' should have already be found
244+
/* c8 ignore next */ // error if there .. 'read' & 'read' should have already be found
244245
throw new Error(`Invalid level found level1: ${level1}, level2 ${level2}`);
245246
}
246247

@@ -282,7 +283,10 @@ function loadModelDataByStreamIdEventTypes (model, map) {
282283
}
283284
for (const eventType of eventTypes) {
284285
const keyStreamIdEventType = item.streamId + ':' + eventType;
285-
if (map[keyStreamIdEventType]) throw new Error(`Duplicate streamId + eventType "${keyStreamIdEventType}" for item ${JSON.stringify(item)}`);
286+
if (map[keyStreamIdEventType]) {
287+
// should be tested with a faulty model
288+
throw new Error(`Duplicate streamId + eventType "${keyStreamIdEventType}" for item ${JSON.stringify(item)}`);
289+
}
286290
map[keyStreamIdEventType] = item;
287291
}
288292
}
@@ -295,7 +299,10 @@ function loadModelDataByStreamIdEventTypes (model, map) {
295299
function loadModelStreamsById (streams, map) {
296300
if (!streams) return;
297301
for (const stream of streams) {
298-
if (map[stream.id]) throw new Error(`Duplicate streamId "${stream.id}" for strean ${JSON.stringify(stream)}`);
302+
if (map[stream.id]) {
303+
// should be tested with a faulty model
304+
throw new Error(`Duplicate streamId "${stream.id}" for strean ${JSON.stringify(stream)}`);
305+
}
299306
map[stream.id] = stream;
300307
loadModelStreamsById(stream.children, map);
301308
}

tests/hdsModel.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,45 @@ describe('[MODX] Model', () => {
176176
assert.deepEqual(authorizationSet, expected);
177177
});
178178

179+
it('[MOAL] Get Authorizations from items override correctly authorized level', async () => {
180+
const itemKeys = ['profile-name'];
181+
const options = { preRequest: [{ streamId: 'profile', level: 'contribute' }] };
182+
const authorizationSet = model.authorizationForItemKeys(itemKeys, options);
183+
const expected = [
184+
{ streamId: 'profile', level: 'contribute', defaultName: 'Profile' }
185+
];
186+
assert.deepEqual(authorizationSet, expected);
187+
});
188+
189+
it('[MOAV] Get Authorizations from items override correctly authorized level', async () => {
190+
const itemKeys = ['profile-name'];
191+
const options = {
192+
defaultLevel: 'manage',
193+
preRequest: [{ streamId: 'profile', level: 'read' }]
194+
};
195+
const authorizationSet = model.authorizationForItemKeys(itemKeys, options);
196+
const expected = [
197+
{ streamId: 'profile', level: 'read', defaultName: 'Profile' },
198+
{ streamId: 'profile-name', defaultName: 'Name', level: 'manage' }
199+
];
200+
assert.deepEqual(authorizationSet, expected);
201+
});
202+
203+
it('[MOAM] Get Authorizations from items mix correctly authorized level', async () => {
204+
const levels = [{ request: 'manage', expect: 'manage' }, { request: 'contribute', expect: 'contribute' }, { request: 'writeOnly', expect: 'contribute' }];
205+
for (const level of levels) {
206+
const itemKeys = ['profile-name'];
207+
const options = {
208+
preRequest: [{ streamId: 'profile-name', level: level.request }]
209+
};
210+
const authorizationSet = model.authorizationForItemKeys(itemKeys, options);
211+
const expected = [
212+
{ streamId: 'profile-name', level: level.expect, defaultName: 'Name' }
213+
];
214+
assert.deepEqual(authorizationSet, expected);
215+
}
216+
});
217+
179218
it('[MOAO] Get Authorizations from items with overrides', async () => {
180219
const itemKeys = [
181220
'body-vulva-mucus-inspect',

0 commit comments

Comments
 (0)