Skip to content

Commit

Permalink
fix: permision function (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh authored Mar 21, 2024
1 parent 8b94683 commit b6d5ab0
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 89 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ jobs:

- name: Build
run: yarn build

- name: Unit tests
run: yarn test:unit
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@
"hooks:uninstall": "husky uninstall",
"hooks:install": "husky install",
"cypress:open": "env-cmd -f ./.env.test cypress open --browser chrome",
"test": "yarn build:test && concurrently -k -s first \"yarn preview:test\" \"yarn cypress:run\"",
"test": "yarn test:unit && yarn build:test && concurrently -k -s first \"yarn preview:test\" \"yarn cypress:run\"",
"cypress:run": "env-cmd -f ./.env.test cypress run --browser chrome",
"postinstall": "husky install"
"postinstall": "husky install",
"test:unit": "yarn vitest"
},
"browserslist": {
"production": [
Expand Down Expand Up @@ -148,7 +149,8 @@
"typescript": "5.3.3",
"vite": "5.1.4",
"vite-plugin-checker": "0.6.4",
"vite-plugin-istanbul": "5.0.0"
"vite-plugin-istanbul": "5.0.0",
"vitest": "1.4.0"
},
"packageManager": "[email protected]"
}
84 changes: 0 additions & 84 deletions src/utils/item.test.js

This file was deleted.

181 changes: 181 additions & 0 deletions src/utils/item.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { DiscriminatedItem, PermissionLevel } from '@graasp/sdk';

import { describe, expect, it } from 'vitest';

import {
getHighestPermissionForMemberFromMemberships,
getParentsIdsFromPath,
isUrlValid,
transformIdForPath,
} from './item';

describe('item utils', () => {
it('isUrlValid', () => {
expect(isUrlValid(null)).toBeFalsy();
expect(isUrlValid()).toBeFalsy();
expect(isUrlValid('somelink')).toBeFalsy();
expect(isUrlValid('graasp.eu')).toBeTruthy();
expect(isUrlValid('https://graasp')).toBeFalsy();

expect(isUrlValid('https://graasp.eu')).toBeTruthy();
expect(isUrlValid('http://graasp.eu')).toBeTruthy();
expect(isUrlValid('https://www.youtube.com/')).toBeTruthy();
});

it('transformIdForPath', () => {
expect(transformIdForPath('someid')).toEqual('someid');
expect(transformIdForPath('some-id')).toEqual('some_id');
const id = 'ecafbd2a-5688-11eb-ae93-0242ac130002';
const path = 'ecafbd2a_5688_11eb_ae93_0242ac130002';
expect(transformIdForPath(id)).toEqual(path);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(() => transformIdForPath(null)).toThrow();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(() => transformIdForPath(undefined)).toThrow();
});

describe('getParentsIdsFromPath', () => {
it('default', () => {
expect(getParentsIdsFromPath('someid')).toEqual(['someid']);
expect(getParentsIdsFromPath('parent.child')).toEqual([
'parent',
'child',
]);
expect(
getParentsIdsFromPath(
'ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001',
),
).toEqual([
'ecafbd2a-5688-11eb-ae93-0242ac130002',
'ecafbd2a-5688-11eb-ae93-0242ac130001',
]);
expect(
getParentsIdsFromPath(
'ecafbd2a_5688_11eb_ae93_0242ac130003.ecafbd2a_5688_11eb_ae93_0242ac130004.ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001',
),
).toEqual([
'ecafbd2a-5688-11eb-ae93-0242ac130003',
'ecafbd2a-5688-11eb-ae93-0242ac130004',
'ecafbd2a-5688-11eb-ae93-0242ac130002',
'ecafbd2a-5688-11eb-ae93-0242ac130001',
]);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(getParentsIdsFromPath(null)).toEqual([]);
expect(getParentsIdsFromPath(undefined)).toEqual([]);
});
it('ignoreSelf = true', () => {
expect(getParentsIdsFromPath('someid', { ignoreSelf: true })).toEqual([]);
expect(
getParentsIdsFromPath('parent.child', { ignoreSelf: true }),
).toEqual(['parent']);
expect(
getParentsIdsFromPath(
'ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001',
{ ignoreSelf: true },
),
).toEqual(['ecafbd2a-5688-11eb-ae93-0242ac130002']);
expect(
getParentsIdsFromPath(
'ecafbd2a_5688_11eb_ae93_0242ac130003.ecafbd2a_5688_11eb_ae93_0242ac130004.ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001',
{ ignoreSelf: true },
),
).toEqual([
'ecafbd2a-5688-11eb-ae93-0242ac130003',
'ecafbd2a-5688-11eb-ae93-0242ac130004',
'ecafbd2a-5688-11eb-ae93-0242ac130002',
]);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(getParentsIdsFromPath(null, { ignoreSelf: true })).toEqual([]);
expect(getParentsIdsFromPath(undefined, { ignoreSelf: true })).toEqual(
[],
);
});
});

describe('getHighestPermission', () => {
it('returns null when no memberId', () => {
expect(
getHighestPermissionForMemberFromMemberships({
memberId: undefined,
itemPath: '1234',
}),
).toEqual(null);
});

it('returns null when no memberships', () => {
expect(
getHighestPermissionForMemberFromMemberships({
memberId: '1234',
itemPath: '1234',
memberships: undefined,
}),
).toEqual(null);
expect(
getHighestPermissionForMemberFromMemberships({
memberId: '1234',
itemPath: '1234',
memberships: [],
}),
).toEqual(null);
});

it('returns closest permission when only one permission', () => {
const member = { id: '1234', name: 'bob', email: '[email protected]' };
const item = { path: '1234' } as DiscriminatedItem;
const membership = {
id: 'membership-123',
member,
item,
permission: PermissionLevel.Read,
creator: null,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
expect(
getHighestPermissionForMemberFromMemberships({
memberId: member.id,
itemPath: item.path,
memberships: [membership],
}),
).toEqual(membership);
});

it('returns closest permission when multiple permissions', () => {
const member = { id: '1234', name: 'bob', email: '[email protected]' };
const item1 = { path: '1234' } as DiscriminatedItem;
const item2 = { path: '1234.5678' } as DiscriminatedItem;
const membership1 = {
id: 'membership-123',
member,
item: item1,
permission: PermissionLevel.Read,
creator: null,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
const membership2 = {
id: 'membership-123',
member,
item: item2,
permission: PermissionLevel.Read,
creator: null,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
expect(
getHighestPermissionForMemberFromMemberships({
memberId: member.id,
itemPath: item2.path,
memberships: [membership1, membership2],
}),
).toEqual(membership2);
});
});
});
4 changes: 2 additions & 2 deletions src/utils/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ export const getHighestPermissionForMemberFromMemberships = ({
({ item: { path: mPath }, member: { id: mId } }) =>
mId === memberId && itemPath.includes(mPath),
);
if (!itemMemberships) {
if (!itemMemberships || itemMemberships.length === 0) {
return null;
}

const sorted = [...itemMemberships];
sorted?.sort((a, b) => (a.item.path.length > b.item.path.length ? 1 : -1));
sorted?.sort((a, b) => (a.item.path.length > b.item.path.length ? -1 : 1));

return sorted[0];
};
Loading

0 comments on commit b6d5ab0

Please sign in to comment.