Skip to content

Commit

Permalink
Merge branch 'main' into pr/dbauszus-glx/1132
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAndrewHurst committed Mar 22, 2024
2 parents 3a5e606 + c7c0baf commit 5135380
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Jest unit tests
name: Codi unit tests

on:
push:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"node": ">=18.x"
},
"scripts": {
"test": "jest",
"test": "codi tests",
"_build": "npx esbuild ./lib/mapp.mjs ./lib/ui.mjs --bundle --minify --tree-shaking=false --sourcemap --format=iife --outdir=./public/js/lib",
"version": "node version.js",
"generate-docs-mapp": "jsdoc --configure jsdoc_mapp.json --verbose",
Expand All @@ -29,6 +29,7 @@
},
"devDependencies": {
"clean-jsdoc-theme": "^4.2.17",
"codi-test-framework": "^0.0.8",
"cookie-parser": "^1.4.5",
"dotenv": "^16.4.5",
"esbuild": "^0.19.11",
Expand All @@ -38,7 +39,6 @@
"eslint-plugin-n": "^15.6.0",
"eslint-plugin-promise": "^6.1.1",
"express": "^4.18.3",
"jest": "^29.5.0",
"jsdocs": "^0.0.1",
"uhtml": "^3.1.0"
}
Expand Down
81 changes: 81 additions & 0 deletions tests/mod/utils/merge.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, it, assertDeepEqual } from 'codi-test-framework';
import mergeDeep from '../../../mod/utils/merge.js'

describe('mergeDeep', () => {

it('should merge objects deeply', () => {
const target = {
name: 'Rob',
age: 28,
address: {
street: '6 fourteenth street',
city: 'Johannesburg',
},
hobbies: ['squash', 'guitar'],
};

const source = {
name: 'Rob',
age: 28,
address: {
street: '6 fourteenth street',
city: 'Johannesburg',
},
hobbies: ['cooking'],
};

const expected = {
name: 'Rob',
age: 28,
address: {
street: '6 fourteenth street',
city: 'Johannesburg',
},
hobbies: ['squash', 'guitar', 'cooking'],
};

const mergedObj = mergeDeep(target, source);

assertDeepEqual(mergedObj, expected)
});

it('should merge arrays deeply', () => {
const target = {
fruits: ['apple', 'banana'],
};

const source = {
fruits: ['banana', 'orange'],
};

const expected = {
fruits: ['apple', 'banana','banana', 'orange'],
};

const mergedObj = mergeDeep(target, source);

assertDeepEqual(mergedObj, expected)
});

it('should handle merging with null or undefined values', () => {
const target = {
name: 'John',
age: 30,
};

const source1 = null;
const source2 = undefined;

const expected = {
name: 'John',
age: 30,
};

const mergedObj1 = mergeDeep(target, source1);
const mergedObj2 = mergeDeep(target, source2);

assertDeepEqual(mergedObj1, expected);
assertDeepEqual(mergedObj2, expected);
});

});
105 changes: 105 additions & 0 deletions tests/mod/utils/roles.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { describe, it, assertEqual, assertDeepEqual } from 'codi-test-framework';
import { check, filter, get } from '../../../mod/utils/roles.js';

describe('Roles Module', () => {
describe('check()', () => {
it('should return the object if no roles are assigned', () => {
const obj = { name: 'John' };
const result = check(obj, ['user']);
assertEqual(result, obj);
});

it('should return the object if the asterisk role is present', () => {
const obj = { name: 'John', roles: { '*': true } };
const result = check(obj, ['user']);
assertEqual(result, obj);
});

it('should return false if user roles are not provided or not an array', () => {
const obj = { name: 'John', roles: { user: true } };
const result1 = check(obj, null);
const result2 = check(obj, 'user');
assertEqual(result1, false);
assertEqual(result2, false);
});

it('should return false if a negated role is included in user roles', () => {
const obj = { name: 'John', roles: { '!admin': true } };
const result = check(obj, ['user', 'admin']);
assertEqual(result, false);
});

it('should return the object if all roles are negated and not included in user roles', () => {
const obj = { name: 'John', roles: { '!admin': true, '!superuser': true } };
const result = check(obj, ['user']);
assertEqual(result, obj);
});

it('should return the object if a positive role is included in user roles', () => {
const obj = { name: 'John', roles: { user: true, admin: true } };
const result = check(obj, ['user']);
assertEqual(result, obj);
});

it('should return false if no positive role is included in user roles', () => {
const obj = { name: 'John', roles: { admin: true } };
const result = check(obj, ['user']);
assertEqual(result, false);
});
});

describe('filter()', () => {
it('should return undefined if the layer has no roles', () => {
const layer = { name: 'Layer' };
const result = filter(layer, ['user']);
assertEqual(result, undefined);
});

it('should return undefined if user roles are not an array', () => {
const layer = { name: 'Layer', roles: { user: { filter: { status: 'active' } } } };
const result = filter(layer, 'user');
assertEqual(result, undefined);
});

it('should return the role filter for matching roles', () => {
const layer = { name: 'Layer', roles: { user: { filter: { status: 'active' } } } };
const result = filter(layer, ['user']);
assertDeepEqual(result, { user: { status: 'active' } });
});

it('should return the role filter for negated roles not included in user roles', () => {
const layer = { name: 'Layer', roles: { '!admin': { filter: { status: 'active' } } } };
const result = filter(layer, ['user']);
assertDeepEqual(result, { '!admin': { status: 'active' } });
});
});

describe('get()', () => {
it('should return an array of unique roles from the object tree', () => {
const obj = {
roles: { user: true, admin: true },
foo: { roles: { superuser: true } },
};
const result = get(obj);
assertDeepEqual(result, ['user', 'admin', 'superuser']);
});

it('should remove the negation prefix from roles', () => {
const obj = {
roles: { '!user': true, admin: true },
foo: { roles: { '!superuser': true } },
};
const result = get(obj);
assertDeepEqual(result, ['user', 'admin', 'superuser']);
});

it('should exclude the restricted asterisk role', () => {
const obj = {
roles: { '*': true, user: true },
foo: { roles: { admin: true } },
};
const result = get(obj);
assertDeepEqual(result, ['user', 'admin']);
});
});
});

0 comments on commit 5135380

Please sign in to comment.